2
0

JSON.xml 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <class name="JSON" inherits="RefCounted" version="4.0">
  3. <brief_description>
  4. Helper class for creating and parsing JSON data.
  5. </brief_description>
  6. <description>
  7. 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.
  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 [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.
  10. [b]Example[/b]
  11. [codeblock]
  12. var data_to_send = ["a", "b", "c"]
  13. var json = JSON.new()
  14. var json_string = json.stringify(data_to_send)
  15. # Save data
  16. # ...
  17. # Retrieve data
  18. var error = json.parse(json_string)
  19. if error == OK:
  20. var data_received = json.get_data()
  21. if typeof(data_received) == TYPE_ARRAY:
  22. print(data_received) # Prints array
  23. else:
  24. print("Unexpected data")
  25. else:
  26. print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
  27. [/codeblock]
  28. </description>
  29. <tutorials>
  30. </tutorials>
  31. <methods>
  32. <method name="get_data" qualifiers="const">
  33. <return type="Variant" />
  34. <description>
  35. Returns the [Variant] containing the data of a successful [method parse].
  36. [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.
  37. </description>
  38. </method>
  39. <method name="get_error_line" qualifiers="const">
  40. <return type="int" />
  41. <description>
  42. Returns [code]0[/code] if the last call to [method parse] was successful, or the line number where the parse failed.
  43. </description>
  44. </method>
  45. <method name="get_error_message" qualifiers="const">
  46. <return type="String" />
  47. <description>
  48. Returns an empty string if the last call to [method parse] was successful, or the error message if it failed.
  49. </description>
  50. </method>
  51. <method name="parse">
  52. <return type="int" enum="Error" />
  53. <argument index="0" name="json_string" type="String" />
  54. <description>
  55. Attempts to parse the [code]json_string[/code] provided.
  56. 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.
  57. </description>
  58. </method>
  59. <method name="stringify">
  60. <return type="String" />
  61. <argument index="0" name="data" type="Variant" />
  62. <argument index="1" name="indent" type="String" default="&quot;&quot;" />
  63. <argument index="2" name="sort_keys" type="bool" default="true" />
  64. <argument index="3" name="full_precision" type="bool" default="false" />
  65. <description>
  66. Converts a [Variant] var to JSON text and returns the result. Useful for serializing data to store or send over the network.
  67. [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.
  68. [b]Note:[/b] If [code]full_precision[/code] is true, when stringifying floats, the unreliable digits are stringified in addition to the reliable digits to guarantee exact decoding.
  69. Use [code]indent[/code] parameter to pretty stringify the output.
  70. [b]Example output:[/b]
  71. [codeblock]
  72. ## JSON.stringify(my_dictionary)
  73. {"name":"my_dictionary","version":"1.0.0","entities":[{"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"}]}
  74. ## JSON.stringify(my_dictionary, "\t")
  75. {
  76. "name": "my_dictionary",
  77. "version": "1.0.0",
  78. "entities": [
  79. {
  80. "name": "entity_0",
  81. "value": "value_0"
  82. },
  83. {
  84. "name": "entity_1",
  85. "value": "value_1"
  86. }
  87. ]
  88. }
  89. [/codeblock]
  90. </description>
  91. </method>
  92. </methods>
  93. </class>