mirror of
https://github.com/godotengine/godot.git
synced 2024-12-21 10:25:24 +08:00
122 lines
5.5 KiB
XML
122 lines
5.5 KiB
XML
<?xml version="1.0" encoding="UTF-8" ?>
|
|
<class name="JSON" inherits="RefCounted" version="4.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
|
|
<brief_description>
|
|
Helper class for creating and parsing JSON data.
|
|
</brief_description>
|
|
<description>
|
|
The [JSON] enables all data types to be converted to and from a JSON string. This useful for serializing data 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 [method get_data] to retrieve the [Variant], and use [code]typeof[/code] 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.
|
|
[b]Example[/b]
|
|
[codeblock]
|
|
var data_to_send = ["a", "b", "c"]
|
|
var json_string = JSON.stringify(data_to_send)
|
|
# Save data
|
|
# ...
|
|
# Retrieve data
|
|
var error = json.parse(json_string)
|
|
if error == OK:
|
|
var data_received = json.get_data()
|
|
if typeof(data_received) == TYPE_ARRAY:
|
|
print(data_received) # Prints 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 string using the static [method parse_string] method, but it doesn't allow to handle errors.
|
|
[codeblock]
|
|
var data = JSON.parse_string(json_string) # Returns null if parsing failed.
|
|
[/codeblock]
|
|
</description>
|
|
<tutorials>
|
|
</tutorials>
|
|
<methods>
|
|
<method name="get_data" qualifiers="const">
|
|
<return type="Variant" />
|
|
<description>
|
|
Returns the [Variant] containing the data of a successful [method parse].
|
|
[b]Note:[/b] It will return [code]Null[/code] if the last call to parse was unsuccessful or [method parse] has not yet been called.
|
|
</description>
|
|
</method>
|
|
<method name="get_error_line" qualifiers="const">
|
|
<return type="int" />
|
|
<description>
|
|
Returns [code]0[/code] if the last call to [method parse] was successful, or the line number where the parse failed.
|
|
</description>
|
|
</method>
|
|
<method name="get_error_message" qualifiers="const">
|
|
<return type="String" />
|
|
<description>
|
|
Returns an empty string if the last call to [method parse] was successful, or the error message if it failed.
|
|
</description>
|
|
</method>
|
|
<method name="parse">
|
|
<return type="int" enum="Error" />
|
|
<param index="0" name="json_string" type="String" />
|
|
<description>
|
|
Attempts to parse the [param json_string] provided.
|
|
Returns an [enum Error]. If the parse was successful, it returns [code]OK[/code] and the result can be retrieved using [method get_data]. If unsuccessful, use [method get_error_line] and [method get_error_message] for identifying the source of the failure.
|
|
Non-static variant of [method parse_string], if you want custom error handling.
|
|
</description>
|
|
</method>
|
|
<method name="parse_string" qualifiers="static">
|
|
<return type="Variant" />
|
|
<param index="0" name="json_string" type="String" />
|
|
<description>
|
|
Attempts to parse the [param json_string] provided and returns the parsed data. Returns [code]null[/code] if parse failed.
|
|
</description>
|
|
</method>
|
|
<method name="stringify" qualifiers="static">
|
|
<return type="String" />
|
|
<param index="0" name="data" type="Variant" />
|
|
<param index="1" name="indent" type="String" default="""" />
|
|
<param index="2" name="sort_keys" type="bool" default="true" />
|
|
<param index="3" name="full_precision" type="bool" default="false" />
|
|
<description>
|
|
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, the string used for this parameter 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]
|
|
</description>
|
|
</method>
|
|
</methods>
|
|
</class>
|