json.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. protected:
  74. static void _bind_methods();
  75. public:
  76. Error parse(const String &p_json_string, bool p_keep_text = false);
  77. String get_parsed_text() const;
  78. static String stringify(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true, bool p_full_precision = false);
  79. static Variant parse_string(const String &p_json_string);
  80. inline Variant get_data() const { return data; }
  81. void set_data(const Variant &p_data);
  82. inline int get_error_line() const { return err_line; }
  83. inline String get_error_message() const { return err_str; }
  84. };
  85. class ResourceFormatLoaderJSON : public ResourceFormatLoader {
  86. public:
  87. 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);
  88. virtual void get_recognized_extensions(List<String> *p_extensions) const;
  89. virtual bool handles_type(const String &p_type) const;
  90. virtual String get_resource_type(const String &p_path) const;
  91. };
  92. class ResourceFormatSaverJSON : public ResourceFormatSaver {
  93. public:
  94. virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0);
  95. virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const;
  96. virtual bool recognize(const Ref<Resource> &p_resource) const;
  97. };
  98. #endif // JSON_H