JSON

package
v0.0.0-...-c858641 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 14, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Overview

The graphics.gd/classdb/JSON class enables all data types to be converted to and from a JSON string. This is useful for serializing data, e.g. to save to a file or send over the network.

Stringify is used to convert any data type into a JSON string.

Instance.Parse is used to convert any existing JSON data into a any that can be used within Godot. If successfully parsed, use Instance.Data to retrieve the any, and use [graphics.gd/classdb/@GlobalScope.Instance.Typeof] to check if the Variant's type is what you expect. JSON Objects are converted into a data structure, but JSON data can be used to store slices, numbers, strings and even just a boolean.

package main

import (
	"graphics.gd/classdb/JSON"
)

func ExampleJSON() {
	var data_to_send = []string{"a", "b", "c"}
	var json_string = JSON.Stringify(data_to_send, "", false)
	// Save data
	// ...
	// Retrieve data
	var json = JSON.New()
	var error = json.Parse(json_string)
	if error == nil {
		var data_received = json.Data()
		if _, ok := data_received.([]any); ok {
			print(data_received) // Prints the array.
		} else {
			print("Unexpected data")
		}
	} else {
		print("JSON Parse Error: ", json.GetErrorMessage(), " in ", json_string, " at line ", json.GetErrorLine())
	}
}

Alternatively, you can parse strings using the static ParseString method, but it doesn't handle errors.

package main

import "graphics.gd/classdb/JSON"

func ExampleParseString(json_string string) {
	var data = JSON.ParseString(json_string) // Returns null if parsing failed.
	_ = data
}

Note: Both parse methods do not fully comply with the JSON specification:

- Trailing commas in arrays or objects are ignored, instead of causing a parser error.

- New line and tab characters are accepted in string literals, and are treated like their corresponding escape sequences \n and \t.

- Numbers are parsed using graphics.gd/classdb/String.Instance.ToFloat which is generally more lax than the JSON specification.

- Certain errors, such as invalid Unicode sequences, do not cause a parser error. Instead, the string is cleaned up and an error is logged to the console.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromNative

func FromNative(v any, full_objects bool) any

Converts a native engine type to a JSON-compliant value.

By default, objects are ignored for security reasons, unless 'full_objects' is true.

You can convert a native value to a JSON string like this:

func FromNativeOptions

func FromNativeOptions(v any, full_objects bool) any

Converts a native engine type to a JSON-compliant value.

By default, objects are ignored for security reasons, unless 'full_objects' is true.

You can convert a native value to a JSON string like this:

func ParseString

func ParseString(json_string string) any

Attempts to parse the 'json_string' provided and returns the parsed data. Returns null if parse failed.

func Stringify

func Stringify(data any, indent string, full_precision bool) string

Converts a any var to JSON text and returns the result. Useful for serializing data to store or send over the network.

Note: The JSON specification does not define integer or float types, but only a number type. Therefore, converting a Variant to JSON text will convert all numerical values to [Float.X] types.

Note: If 'full_precision' is true, when stringifying floats, the unreliable digits are stringified in addition to the reliable digits to guarantee exact decoding.

The 'indent' parameter controls if and how something is indented; its contents will be used where there should be an indent in the output. Even spaces like " " will work. \t and \n can also be used for a tab indent, or to make a newline for each indent respectively.

Example output:

func StringifyOptions

func StringifyOptions(data any, indent string, sort_keys bool, full_precision bool) string

Converts a any var to JSON text and returns the result. Useful for serializing data to store or send over the network.

Note: The JSON specification does not define integer or float types, but only a number type. Therefore, converting a Variant to JSON text will convert all numerical values to [Float.X] types.

Note: If 'full_precision' is true, when stringifying floats, the unreliable digits are stringified in addition to the reliable digits to guarantee exact decoding.

The 'indent' parameter controls if and how something is indented; its contents will be used where there should be an indent in the output. Even spaces like " " will work. \t and \n can also be used for a tab indent, or to make a newline for each indent respectively.

Example output:

func ToNative

func ToNative(json any, allow_objects bool) any

Converts a JSON-compliant value that was created with FromNative back to native engine types.

By default, objects are ignored for security reasons, unless 'allow_objects' is true.

You can convert a JSON string back to a native value like this:

func ToNativeOptions

func ToNativeOptions(json any, allow_objects bool) any

Converts a JSON-compliant value that was created with FromNative back to native engine types.

By default, objects are ignored for security reasons, unless 'allow_objects' is true.

You can convert a JSON string back to a native value like this:

Types

type Advanced

type Advanced = class

Advanced exposes a 1:1 low-level instance of the class, undocumented, for those who know what they are doing.

type Any

type Any interface {
	gd.IsClass
	AsJSON() Instance
}

type Expanded

type Expanded [1]gdclass.JSON

func (Expanded) Parse

func (self Expanded) Parse(json_text string, keep_text bool) error

Attempts to parse the 'json_text' provided.

Returns an [Error]. If the parse was successful, it returns [Ok] and the result can be retrieved using Instance.Data. If unsuccessful, use Instance.GetErrorLine and Instance.GetErrorMessage to identify the source of the failure.

Non-static variant of ParseString, if you want custom error handling.

The optional 'keep_text' argument instructs the parser to keep a copy of the original text. This text can be obtained later by using the Instance.GetParsedText function and is used when saving the resource (instead of generating new text from Instance.Data).

type Extension

type Extension[T gdclass.Interface] struct{ gdclass.Extension[T, Instance] }

Extension can be embedded in a new struct to create an extension of this class. T should be the type that is embedding this Extension

func (*Extension[T]) AsJSON

func (self *Extension[T]) AsJSON() Instance

func (*Extension[T]) AsObject

func (self *Extension[T]) AsObject() [1]gd.Object

func (*Extension[T]) AsRefCounted

func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted

func (*Extension[T]) AsResource

func (self *Extension[T]) AsResource() Resource.Instance

type ID

type ID Object.ID

ID is a typed object ID (reference) to an instance of this class, use it to store references to objects with unknown lifetimes, as an ID will not panic on use if the underlying object has been destroyed.

func (ID) Instance

func (id ID) Instance() (Instance, bool)

type Instance

type Instance [1]gdclass.JSON

Instance of the class with convieniently typed arguments and results.

var Nil Instance

Nil is a nil/null instance of the class. Equivalent to the zero value.

func New

func New() Instance

func (Instance) AsJSON

func (self Instance) AsJSON() Instance

func (Instance) AsObject

func (self Instance) AsObject() [1]gd.Object

func (Instance) AsRefCounted

func (self Instance) AsRefCounted() [1]gd.RefCounted

func (Instance) AsResource

func (self Instance) AsResource() Resource.Instance

func (Instance) Data

func (self Instance) Data() any

func (Instance) GetErrorLine

func (self Instance) GetErrorLine() int

Returns 0 if the last call to Instance.Parse was successful, or the line number where the parse failed.

func (Instance) GetErrorMessage

func (self Instance) GetErrorMessage() string

Returns an empty string if the last call to Instance.Parse was successful, or the error message if it failed.

func (Instance) GetParsedText

func (self Instance) GetParsedText() string

Return the text parsed by Instance.Parse (requires passing keep_text to Instance.Parse).

func (Instance) ID

func (self Instance) ID() ID

func (Instance) Parse

func (self Instance) Parse(json_text string) error

Attempts to parse the 'json_text' provided.

Returns an [Error]. If the parse was successful, it returns [Ok] and the result can be retrieved using Instance.Data. If unsuccessful, use Instance.GetErrorLine and Instance.GetErrorMessage to identify the source of the failure.

Non-static variant of ParseString, if you want custom error handling.

The optional 'keep_text' argument instructs the parser to keep a copy of the original text. This text can be obtained later by using the Instance.GetParsedText function and is used when saving the resource (instead of generating new text from Instance.Data).

func (Instance) SetData

func (self Instance) SetData(value any)

func (*Instance) SetObject

func (self *Instance) SetObject(obj [1]gd.Object) bool

func (Instance) Virtual

func (self Instance) Virtual(name string) reflect.Value

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL