Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/io/json.h
9973 views
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
31
#pragma once
32
33
#include "core/io/resource.h"
34
#include "core/io/resource_loader.h"
35
#include "core/io/resource_saver.h"
36
#include "core/variant/variant.h"
37
38
class JSON : public Resource {
39
GDCLASS(JSON, Resource);
40
41
enum TokenType {
42
TK_CURLY_BRACKET_OPEN,
43
TK_CURLY_BRACKET_CLOSE,
44
TK_BRACKET_OPEN,
45
TK_BRACKET_CLOSE,
46
TK_IDENTIFIER,
47
TK_STRING,
48
TK_NUMBER,
49
TK_COLON,
50
TK_COMMA,
51
TK_EOF,
52
TK_MAX
53
};
54
55
enum Expecting {
56
EXPECT_OBJECT,
57
EXPECT_OBJECT_KEY,
58
EXPECT_COLON,
59
EXPECT_OBJECT_VALUE,
60
};
61
62
struct Token {
63
TokenType type;
64
Variant value;
65
};
66
67
String text;
68
Variant data;
69
String err_str;
70
int err_line = 0;
71
72
static const char *tk_name[];
73
74
static void _add_indent(String &r_result, const String &p_indent, int p_size);
75
static void _stringify(String &r_result, 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);
76
static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
77
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);
78
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);
79
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);
80
static Error _parse_string(const String &p_json, Variant &r_ret, String &r_err_str, int &r_err_line);
81
82
static Variant _from_native(const Variant &p_variant, bool p_full_objects, int p_depth);
83
static Variant _to_native(const Variant &p_json, bool p_allow_objects, int p_depth);
84
85
protected:
86
static void _bind_methods();
87
88
public:
89
Error parse(const String &p_json_string, bool p_keep_text = false);
90
String get_parsed_text() const;
91
92
static String stringify(const Variant &p_var, const String &p_indent = "", bool p_sort_keys = true, bool p_full_precision = false);
93
static Variant parse_string(const String &p_json_string);
94
95
_FORCE_INLINE_ static Variant from_native(const Variant &p_variant, bool p_full_objects = false) {
96
return _from_native(p_variant, p_full_objects, 0);
97
}
98
_FORCE_INLINE_ static Variant to_native(const Variant &p_json, bool p_allow_objects = false) {
99
return _to_native(p_json, p_allow_objects, 0);
100
}
101
102
void set_data(const Variant &p_data);
103
_FORCE_INLINE_ Variant get_data() const { return data; }
104
105
_FORCE_INLINE_ int get_error_line() const { return err_line; }
106
_FORCE_INLINE_ String get_error_message() const { return err_str; }
107
};
108
109
class ResourceFormatLoaderJSON : public ResourceFormatLoader {
110
public:
111
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;
112
virtual void get_recognized_extensions(List<String> *p_extensions) const override;
113
virtual bool handles_type(const String &p_type) const override;
114
virtual String get_resource_type(const String &p_path) const override;
115
116
// Treat JSON as a text file, do not generate a `*.json.uid` file.
117
virtual ResourceUID::ID get_resource_uid(const String &p_path) const override { return ResourceUID::INVALID_ID; }
118
virtual bool has_custom_uid_support() const override { return true; }
119
};
120
121
class ResourceFormatSaverJSON : public ResourceFormatSaver {
122
public:
123
virtual Error save(const Ref<Resource> &p_resource, const String &p_path, uint32_t p_flags = 0) override;
124
virtual void get_recognized_extensions(const Ref<Resource> &p_resource, List<String> *p_extensions) const override;
125
virtual bool recognize(const Ref<Resource> &p_resource) const override;
126
};
127
128