Documentation
¶
Overview ¶
Package JSON provides methods for working with JSON object instances.
Index ¶
- func FromNative(v any, full_objects bool) any
- func FromNativeOptions(v any, full_objects bool) any
- func ParseString(json_string string) any
- func Stringify(data any, indent string, full_precision bool) string
- func StringifyOptions(data any, indent string, sort_keys bool, full_precision bool) string
- func ToNative(json any, allow_objects bool) any
- func ToNativeOptions(json any, allow_objects bool) any
- type Advanced
- type Any
- type Expanded
- type Extension
- type ID
- type Instance
- func (self Instance) AsJSON() Instance
- func (self Instance) AsObject() [1]gd.Object
- func (self Instance) AsRefCounted() [1]gd.RefCounted
- func (self Instance) AsResource() Resource.Instance
- func (self Instance) Data() any
- func (self Instance) GetErrorLine() int
- func (self Instance) GetErrorMessage() string
- func (self Instance) GetParsedText() string
- func (self Instance) ID() ID
- func (self Instance) Parse(json_text string) error
- func (self Instance) SetData(value any)
- func (self *Instance) SetObject(obj [1]gd.Object) bool
- func (self Instance) Virtual(name string) reflect.Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func FromNative ¶
Converts a native engine type to a JSON-compliant value. By default, objects are ignored for security reasons, unless [param full_objects] is [code]true[/code]. You can convert a native value to a JSON string like this: [codeblock] func encode_data(value, full_objects = false):
return JSON.stringify(JSON.from_native(value, full_objects))
[/codeblock]
func FromNativeOptions ¶
Converts a native engine type to a JSON-compliant value. By default, objects are ignored for security reasons, unless [param full_objects] is [code]true[/code]. You can convert a native value to a JSON string like this: [codeblock] func encode_data(value, full_objects = false):
return JSON.stringify(JSON.from_native(value, full_objects))
[/codeblock]
func ParseString ¶
Attempts to parse the [param json_string] provided and returns the parsed data. Returns [code]null[/code] if parse failed.
func Stringify ¶
Converts a [Variant] var to JSON text and returns the result. Useful for serializing data to store or send over the network. [b]Note:[/b] The JSON specification does not define integer or float types, but only a [i]number[/i] type. Therefore, converting a Variant to JSON text will convert all numerical values to [float] types. [b]Note:[/b] If [param full_precision] is [code]true[/code], when stringifying floats, the unreliable digits are stringified in addition to the reliable digits to guarantee exact decoding. The [param 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 [code]" "[/code] will work. [code]\t[/code] and [code]\n[/code] can also be used for a tab indent, or to make a newline for each indent respectively. [b]Example output:[/b] [codeblock] ## JSON.stringify(my_dictionary) {"name":"my_dictionary","version":"1.0.0","entities":[{"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"}]}
## JSON.stringify(my_dictionary, "\t")
{ "name": "my_dictionary", "version": "1.0.0", "entities": [ { "name": "entity_0", "value": "value_0" }, { "name": "entity_1", "value": "value_1" } ] }
## JSON.stringify(my_dictionary, "...") { ..."name": "my_dictionary", ..."version": "1.0.0", ..."entities": [ ......{ ........."name": "entity_0", ........."value": "value_0" ......}, ......{ ........."name": "entity_1", ........."value": "value_1" ......} ...] } [/codeblock]
func StringifyOptions ¶
Converts a [Variant] var to JSON text and returns the result. Useful for serializing data to store or send over the network. [b]Note:[/b] The JSON specification does not define integer or float types, but only a [i]number[/i] type. Therefore, converting a Variant to JSON text will convert all numerical values to [float] types. [b]Note:[/b] If [param full_precision] is [code]true[/code], when stringifying floats, the unreliable digits are stringified in addition to the reliable digits to guarantee exact decoding. The [param 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 [code]" "[/code] will work. [code]\t[/code] and [code]\n[/code] can also be used for a tab indent, or to make a newline for each indent respectively. [b]Example output:[/b] [codeblock] ## JSON.stringify(my_dictionary) {"name":"my_dictionary","version":"1.0.0","entities":[{"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"}]}
## JSON.stringify(my_dictionary, "\t")
{ "name": "my_dictionary", "version": "1.0.0", "entities": [ { "name": "entity_0", "value": "value_0" }, { "name": "entity_1", "value": "value_1" } ] }
## JSON.stringify(my_dictionary, "...") { ..."name": "my_dictionary", ..."version": "1.0.0", ..."entities": [ ......{ ........."name": "entity_0", ........."value": "value_0" ......}, ......{ ........."name": "entity_1", ........."value": "value_1" ......} ...] } [/codeblock]
func ToNative ¶
Converts a JSON-compliant value that was created with [method from_native] back to native engine types. By default, objects are ignored for security reasons, unless [param allow_objects] is [code]true[/code]. You can convert a JSON string back to a native value like this: [codeblock] func decode_data(string, allow_objects = false):
return JSON.to_native(JSON.parse_string(string), allow_objects)
[/codeblock]
func ToNativeOptions ¶
Converts a JSON-compliant value that was created with [method from_native] back to native engine types. By default, objects are ignored for security reasons, unless [param allow_objects] is [code]true[/code]. You can convert a JSON string back to a native value like this: [codeblock] func decode_data(string, allow_objects = false):
return JSON.to_native(JSON.parse_string(string), allow_objects)
[/codeblock]
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 Expanded ¶
func (Expanded) Parse ¶
Attempts to parse the [param json_text] provided. Returns an [enum Error]. If the parse was successful, it returns [constant OK] and the result can be retrieved using [member data]. If unsuccessful, use [method get_error_line] and [method get_error_message] to identify the source of the failure. Non-static variant of [method parse_string], if you want custom error handling. The optional [param keep_text] argument instructs the parser to keep a copy of the original text. This text can be obtained later by using the [method get_parsed_text] function and is used when saving the resource (instead of generating new text from [member data]).
type Extension ¶
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]) AsRefCounted ¶
func (self *Extension[T]) AsRefCounted() [1]gd.RefCounted
func (*Extension[T]) AsResource ¶
type 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.
type Instance ¶
The [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. [method stringify] is used to convert any data type into a JSON string. [method parse] is used to convert any existing JSON data into a [Variant] that can be used within Godot. If successfully parsed, use [member data] to retrieve the [Variant], and use [method @GlobalScope.typeof] to check if the Variant's type is what you expect. JSON Objects are converted into a [Dictionary], but JSON data can be used to store [Array]s, numbers, [String]s and even just a boolean. [codeblock] var data_to_send = ["a", "b", "c"] var json_string = JSON.stringify(data_to_send) # Save data # ... # Retrieve data var json = JSON.new() var error = json.parse(json_string) if error == OK:
var data_received = json.data if typeof(data_received) == TYPE_ARRAY: print(data_received) # Prints the array. else: print("Unexpected data")
else:
print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
[/codeblock] Alternatively, you can parse strings using the static [method parse_string] method, but it doesn't handle errors. [codeblock] var data = JSON.parse_string(json_string) # Returns null if parsing failed. [/codeblock] [b]Note:[/b] 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 [code]\n[/code] and [code]\t[/code]. - Numbers are parsed using [method String.to_float] 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.
var Nil Instance
Nil is a nil/null instance of the class. Equivalent to the zero value.
func (Instance) AsRefCounted ¶
func (self Instance) AsRefCounted() [1]gd.RefCounted
func (Instance) AsResource ¶
func (Instance) GetErrorLine ¶
Returns [code]0[/code] if the last call to [method parse] was successful, or the line number where the parse failed.
func (Instance) GetErrorMessage ¶
Returns an empty string if the last call to [method parse] was successful, or the error message if it failed.
func (Instance) GetParsedText ¶
Return the text parsed by [method parse] (requires passing [code]keep_text[/code] to [method parse]).
func (Instance) Parse ¶
Attempts to parse the [param json_text] provided. Returns an [enum Error]. If the parse was successful, it returns [constant OK] and the result can be retrieved using [member data]. If unsuccessful, use [method get_error_line] and [method get_error_message] to identify the source of the failure. Non-static variant of [method parse_string], if you want custom error handling. The optional [param keep_text] argument instructs the parser to keep a copy of the original text. This text can be obtained later by using the [method get_parsed_text] function and is used when saving the resource (instead of generating new text from [member data]).