class_json.rst 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. :github_url: hide
  2. .. Generated automatically by doc/tools/make_rst.py in Godot's source tree.
  3. .. DO NOT EDIT THIS FILE, but the JSON.xml source instead.
  4. .. The source is found in doc/classes or modules/<name>/doc_classes.
  5. .. _class_JSON:
  6. JSON
  7. ====
  8. **Inherits:** :ref:`RefCounted<class_RefCounted>` **<** :ref:`Object<class_Object>`
  9. Helper class for creating and parsing JSON data.
  10. Description
  11. -----------
  12. 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.
  13. \ :ref:`stringify<class_JSON_method_stringify>` is used to convert any data type into a JSON string.
  14. \ :ref:`parse<class_JSON_method_parse>` is used to convert any existing JSON data into a :ref:`Variant<class_Variant>` that can be used within Godot. If successfully parsed, use :ref:`get_data<class_JSON_method_get_data>` to retrieve the :ref:`Variant<class_Variant>`, and use ``typeof`` to check if the Variant's type is what you expect. JSON Objects are converted into a :ref:`Dictionary<class_Dictionary>`, but JSON data can be used to store :ref:`Array<class_Array>`\ s, numbers, :ref:`String<class_String>`\ s and even just a boolean.
  15. \ **Example**\
  16. ::
  17. var data_to_send = ["a", "b", "c"]
  18. var json = JSON.new()
  19. var json_string = json.stringify(data_to_send)
  20. # Save data
  21. # ...
  22. # Retrieve data
  23. var error = json.parse(json_string)
  24. if error == OK:
  25. var data_received = json.get_data()
  26. if typeof(data_received) == TYPE_ARRAY:
  27. print(data_received) # Prints array
  28. else:
  29. print("Unexpected data")
  30. else:
  31. print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line())
  32. Methods
  33. -------
  34. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  35. | :ref:`Variant<class_Variant>` | :ref:`get_data<class_JSON_method_get_data>` **(** **)** |const| |
  36. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  37. | :ref:`int<class_int>` | :ref:`get_error_line<class_JSON_method_get_error_line>` **(** **)** |const| |
  38. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  39. | :ref:`String<class_String>` | :ref:`get_error_message<class_JSON_method_get_error_message>` **(** **)** |const| |
  40. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  41. | :ref:`Error<enum_@GlobalScope_Error>` | :ref:`parse<class_JSON_method_parse>` **(** :ref:`String<class_String>` json_string **)** |
  42. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  43. | :ref:`String<class_String>` | :ref:`stringify<class_JSON_method_stringify>` **(** :ref:`Variant<class_Variant>` data, :ref:`String<class_String>` indent="", :ref:`bool<class_bool>` sort_keys=true, :ref:`bool<class_bool>` full_precision=false **)** |
  44. +---------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  45. Method Descriptions
  46. -------------------
  47. .. _class_JSON_method_get_data:
  48. - :ref:`Variant<class_Variant>` **get_data** **(** **)** |const|
  49. Returns the :ref:`Variant<class_Variant>` containing the data of a successful :ref:`parse<class_JSON_method_parse>`.
  50. \ **Note:** It will return ``Null`` if the last call to parse was unsuccessful or :ref:`parse<class_JSON_method_parse>` has not yet been called.
  51. ----
  52. .. _class_JSON_method_get_error_line:
  53. - :ref:`int<class_int>` **get_error_line** **(** **)** |const|
  54. Returns ``0`` if the last call to :ref:`parse<class_JSON_method_parse>` was successful, or the line number where the parse failed.
  55. ----
  56. .. _class_JSON_method_get_error_message:
  57. - :ref:`String<class_String>` **get_error_message** **(** **)** |const|
  58. Returns an empty string if the last call to :ref:`parse<class_JSON_method_parse>` was successful, or the error message if it failed.
  59. ----
  60. .. _class_JSON_method_parse:
  61. - :ref:`Error<enum_@GlobalScope_Error>` **parse** **(** :ref:`String<class_String>` json_string **)**
  62. Attempts to parse the ``json_string`` provided.
  63. Returns an :ref:`Error<enum_@GlobalScope_Error>`. If the parse was successful, it returns ``OK`` and the result can be retrieved using :ref:`get_data<class_JSON_method_get_data>`. If unsuccessful, use :ref:`get_error_line<class_JSON_method_get_error_line>` and :ref:`get_error_message<class_JSON_method_get_error_message>` for identifying the source of the failure.
  64. ----
  65. .. _class_JSON_method_stringify:
  66. - :ref:`String<class_String>` **stringify** **(** :ref:`Variant<class_Variant>` data, :ref:`String<class_String>` indent="", :ref:`bool<class_bool>` sort_keys=true, :ref:`bool<class_bool>` full_precision=false **)**
  67. Converts a :ref:`Variant<class_Variant>` var to JSON text and returns the result. Useful for serializing data to store or send over the network.
  68. \ **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 :ref:`float<class_float>` types.
  69. \ **Note:** If ``full_precision`` is true, when stringifying floats, the unreliable digits are stringified in addition to the reliable digits to guarantee exact decoding.
  70. The ``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 ``" "`` will work. ``\t`` and ``\n`` can also be used for a tab indent, or to make a newline for each indent respectively.
  71. \ **Example output:**\
  72. ::
  73. ## JSON.stringify(my_dictionary)
  74. {"name":"my_dictionary","version":"1.0.0","entities":[{"name":"entity_0","value":"value_0"},{"name":"entity_1","value":"value_1"}]}
  75. ## JSON.stringify(my_dictionary, "\t")
  76. {
  77. "name": "my_dictionary",
  78. "version": "1.0.0",
  79. "entities": [
  80. {
  81. "name": "entity_0",
  82. "value": "value_0"
  83. },
  84. {
  85. "name": "entity_1",
  86. "value": "value_1"
  87. }
  88. ]
  89. }
  90. ## JSON.stringify(my_dictionary, "...")
  91. {
  92. ..."name": "my_dictionary",
  93. ..."version": "1.0.0",
  94. ..."entities": [
  95. ......{
  96. ........."name": "entity_0",
  97. ........."value": "value_0"
  98. ......},
  99. ......{
  100. ........."name": "entity_1",
  101. ........."value": "value_1"
  102. ......}
  103. ...]
  104. }
  105. .. |virtual| replace:: :abbr:`virtual (This method should typically be overridden by the user to have any effect.)`
  106. .. |const| replace:: :abbr:`const (This method has no side effects. It doesn't modify any of the instance's member variables.)`
  107. .. |vararg| replace:: :abbr:`vararg (This method accepts any number of arguments after the ones described here.)`
  108. .. |constructor| replace:: :abbr:`constructor (This method is used to construct a type.)`
  109. .. |static| replace:: :abbr:`static (This method doesn't need an instance to be called, so it can be called directly using the class name.)`
  110. .. |operator| replace:: :abbr:`operator (This method describes a valid operator to use with this type as left-hand operand.)`