json.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**************************************************************************/
  2. /* json.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef JSON_H
  31. #define JSON_H
  32. #include "core/io/resource.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/io/resource_saver.h"
  35. #include "core/variant/variant.h"
  36. class JSON : public Resource {
  37. GDCLASS(JSON, Resource);
  38. enum TokenType {
  39. TK_CURLY_BRACKET_OPEN,
  40. TK_CURLY_BRACKET_CLOSE,
  41. TK_BRACKET_OPEN,
  42. TK_BRACKET_CLOSE,
  43. TK_IDENTIFIER,
  44. TK_STRING,
  45. TK_NUMBER,
  46. TK_COLON,
  47. TK_COMMA,
  48. TK_EOF,
  49. TK_MAX
  50. };
  51. enum Expecting {
  52. EXPECT_OBJECT,
  53. EXPECT_OBJECT_KEY,
  54. EXPECT_COLON,
  55. EXPECT_OBJECT_VALUE,
  56. };
  57. struct Token {
  58. TokenType type;
  59. Variant value;
  60. };
  61. String text;
  62. Variant data;
  63. String err_str;
  64. int err_line = 0;
  65. static const char *tk_name[];
  66. static String _make_indent(const String &p_indent, int p_size);
  67. static String _stringify(const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, HashSet<const void *> &p_markers, bool p_full_precision = false);
  68. static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
  69. static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str);
  70. static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str);
  71. static Error _parse_object(Dictionary &object, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str);
  72. static Error _parse_string(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line);
  73. static Variant _from_native(const Variant &p_variant, bool p_full_objects, int p_depth);
  74. static Variant _to_native(const Variant &p_json, bool p_allow_objects, int p_depth);
  75. protected:
  76. static void _bind_methods();
  77. public:
  78. Error parse(const String &p_json_string, bool p_keep_text = false);
  79. String get_parsed_text() const;
  80. static String stringify(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true, bool p_full_precision = false);
  81. static Variant parse_string(const String &p_json_string);
  82. _FORCE_INLINE_ static Variant from_native(const Variant &p_variant, bool p_full_objects = false) {
  83. return _from_native(p_variant, p_full_objects, 0);
  84. }
  85. _FORCE_INLINE_ static Variant to_native(const Variant &p_json, bool p_allow_objects = false) {
  86. return _to_native(p_json, p_allow_objects, 0);
  87. }
  88. void set_data(const Variant &p_data);
  89. _FORCE_INLINE_ Variant get_data() const { return data; }
  90. _FORCE_INLINE_ int get_error_line() const { return err_line; }
  91. _FORCE_INLINE_ String get_error_message() const { return err_str; }
  92. };
  93. class ResourceFormatLoaderJSON : public ResourceFormatLoader {
  94. public:
  95. virtual Ref<Resource> load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, CacheMode p_cache_mode = CACHE_MODE_REUSE) override;
  96. virtual void get_recognized_extensions(List<String> *p_extensions) const override;
  97. virtual bool handles_type(const String &p_type) const override;
  98. virtual String get_resource_type(const String &p_path) const override;
  99. // Treat JSON as a text file, do not generate a `*.json.uid` file.
  100. virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; }
  101. virtual bool has_custom_uid_support() const override { return true; }
  102. };
  103. class ResourceFormatSaverJSON : public ResourceFormatSaver {
  104. public:
  105. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
  106. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
  107. virtual bool recognize(const Ref<Resource> &p_resource) const override;
  108. };
  109. #endif // JSON_H