0.2.0-dev Download ZIP
161 lines ·8.4 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="JSON" inherits="Resource" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Helper class for creating and parsing JSON data. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | 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. |
| 8 | [method stringify] is used to convert any data type into a JSON string. |
| 9 | [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. |
| 10 | [codeblock] |
| 11 | var data_to_send = ["a", "b", "c"] |
| 12 | var json_string = JSON.stringify(data_to_send) |
| 13 | # Save data |
| 14 | # ... |
| 15 | # Retrieve data |
| 16 | var json = JSON.new() |
| 17 | var error = json.parse(json_string) |
| 18 | if error == OK: |
| 19 | var data_received = json.data |
| 20 | if typeof(data_received) == TYPE_ARRAY: |
| 21 | print(data_received) # Prints the array. |
| 22 | else: |
| 23 | print("Unexpected data") |
| 24 | else: |
| 25 | print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) |
| 26 | [/codeblock] |
| 27 | Alternatively, you can parse strings using the static [method parse_string] method, but it doesn't handle errors. |
| 28 | [codeblock] |
| 29 | var data = JSON.parse_string(json_string) # Returns null if parsing failed. |
| 30 | [/codeblock] |
| 31 | [b]Note:[/b] Both parse methods do not fully comply with the JSON specification: |
| 32 | - Trailing commas in arrays or objects are ignored, instead of causing a parser error. |
| 33 | - 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]. |
| 34 | - Numbers are parsed using [method String.to_float] which is generally more lax than the JSON specification. |
| 35 | - 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. |
| 36 | </description> |
| 37 | <tutorials> |
| 38 | </tutorials> |
| 39 | <methods> |
| 40 | <method name="from_native" qualifiers="static"> |
| 41 | <return type="Variant" /> |
| 42 | <param index="0" name="variant" type="Variant" /> |
| 43 | <param index="1" name="full_objects" type="bool" default="false" /> |
| 44 | <description> |
| 45 | Converts a native engine type to a JSON-compliant value. |
| 46 | By default, objects are ignored for security reasons, unless [param full_objects] is [code]true[/code]. |
| 47 | You can convert a native value to a JSON string like this: |
| 48 | [codeblock] |
| 49 | func encode_data(value, full_objects = false): |
| 50 | return JSON.stringify(JSON.from_native(value, full_objects)) |
| 51 | [/codeblock] |
| 52 | </description> |
| 53 | </method> |
| 54 | <method name="get_error_line" qualifiers="const"> |
| 55 | <return type="int" /> |
| 56 | <description> |
| 57 | Returns [code]0[/code] if the last call to [method parse] was successful, or the line number where the parse failed. |
| 58 | </description> |
| 59 | </method> |
| 60 | <method name="get_error_message" qualifiers="const"> |
| 61 | <return type="String" /> |
| 62 | <description> |
| 63 | Returns an empty string if the last call to [method parse] was successful, or the error message if it failed. |
| 64 | </description> |
| 65 | </method> |
| 66 | <method name="get_parsed_text" qualifiers="const"> |
| 67 | <return type="String" /> |
| 68 | <description> |
| 69 | Return the text parsed by [method parse] (requires passing [code]keep_text[/code] to [method parse]). |
| 70 | </description> |
| 71 | </method> |
| 72 | <method name="parse"> |
| 73 | <return type="int" enum="Error" /> |
| 74 | <param index="0" name="json_text" type="String" /> |
| 75 | <param index="1" name="keep_text" type="bool" default="false" /> |
| 76 | <description> |
| 77 | Attempts to parse the [param json_text] provided. |
| 78 | 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. |
| 79 | Non-static variant of [method parse_string], if you want custom error handling. |
| 80 | 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]). |
| 81 | </description> |
| 82 | </method> |
| 83 | <method name="parse_string" qualifiers="static"> |
| 84 | <return type="Variant" /> |
| 85 | <param index="0" name="json_string" type="String" /> |
| 86 | <description> |
| 87 | Attempts to parse the [param json_string] provided and returns the parsed data. Returns [code]null[/code] if parse failed. |
| 88 | </description> |
| 89 | </method> |
| 90 | <method name="stringify" qualifiers="static"> |
| 91 | <return type="String" /> |
| 92 | <param index="0" name="data" type="Variant" /> |
| 93 | <param index="1" name="indent" type="String" default="""" /> |
| 94 | <param index="2" name="sort_keys" type="bool" default="true" /> |
| 95 | <param index="3" name="full_precision" type="bool" default="false" /> |
| 96 | <description> |
| 97 | Converts a [Variant] var to JSON text and returns the result. Useful for serializing data to store or send over the network. |
| 98 | If [param sort_keys] is [code]true[/code], the keys of any [Dictionary]s are sorted alphabetically when converting to JSON. 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. |
| 99 | [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. |
| 100 | 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. If set to an empty string, the JSON will be on a single line with no spacing, which allows for smaller file sizes. |
| 101 | [b]Warning:[/b] Non-finite numbers are not supported in JSON. Any occurrences of [constant @GDScript.INF] will be replaced with [code]1e99999[/code], and negative [constant @GDScript.INF] will be replaced with [code]-1e99999[/code], but they will be interpreted correctly as infinity by most JSON parsers. [constant @GDScript.NAN] will be replaced with [code]null[/code], and it will not be interpreted as NaN in JSON parsers. If you expect non-finite numbers, consider passing your data through [method from_native] first. |
| 102 | [b]Example output:[/b] |
| 103 | [codeblock] |
| 104 | ## JSON.stringify(my_dictionary, "", false) |
| 105 | {"name":"my_dictionary","version":"1.0.0","entities":[{"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"}]} |
| 106 | |
| 107 | ## JSON.stringify(my_dictionary, "\t", false) |
| 108 | { |
| 109 | "name": "my_dictionary", |
| 110 | "version": "1.0.0", |
| 111 | "entities": [ |
| 112 | { |
| 113 | "name": "entity_0", |
| 114 | "value": "value_0" |
| 115 | }, |
| 116 | { |
| 117 | "name": "entity_1", |
| 118 | "value": "value_1" |
| 119 | } |
| 120 | ] |
| 121 | } |
| 122 | |
| 123 | ## JSON.stringify(my_dictionary, "...", false) |
| 124 | { |
| 125 | ..."name": "my_dictionary", |
| 126 | ..."version": "1.0.0", |
| 127 | ..."entities": [ |
| 128 | ......{ |
| 129 | ........."name": "entity_0", |
| 130 | ........."value": "value_0" |
| 131 | ......}, |
| 132 | ......{ |
| 133 | ........."name": "entity_1", |
| 134 | ........."value": "value_1" |
| 135 | ......} |
| 136 | ...] |
| 137 | } |
| 138 | [/codeblock] |
| 139 | </description> |
| 140 | </method> |
| 141 | <method name="to_native" qualifiers="static"> |
| 142 | <return type="Variant" /> |
| 143 | <param index="0" name="json" type="Variant" /> |
| 144 | <param index="1" name="allow_objects" type="bool" default="false" /> |
| 145 | <description> |
| 146 | Converts a JSON-compliant value that was created with [method from_native] back to native engine types. |
| 147 | By default, objects are ignored for security reasons, unless [param allow_objects] is [code]true[/code]. |
| 148 | You can convert a JSON string back to a native value like this: |
| 149 | [codeblock] |
| 150 | func decode_data(string, allow_objects = false): |
| 151 | return JSON.to_native(JSON.parse_string(string), allow_objects) |
| 152 | [/codeblock] |
| 153 | </description> |
| 154 | </method> |
| 155 | </methods> |
| 156 | <members> |
| 157 | <member name="data" type="Variant" setter="set_data" getter="get_data" default="null"> |
| 158 | Contains the parsed JSON data in [Variant] form. |
| 159 | </member> |
| 160 | </members> |
| 161 | </class> |