ScummVM API documentation
json.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 /*
23  * Files JSON.h and JSONValue.h part of the SimpleJSON Library - https://github.com/MJPA/SimpleJSON
24  *
25  * Copyright (C) 2010 Mike Anchor
26  *
27  * Permission is hereby granted, free of charge, to any person obtaining a copy
28  * of this software and associated documentation files (the "Software"), to deal
29  * in the Software without restriction, including without limitation the rights
30  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31  * copies of the Software, and to permit persons to whom the Software is
32  * furnished to do so, subject to the following conditions:
33  *
34  * The above copyright notice and this permission notice shall be included in
35  * all copies or substantial portions of the Software.
36  *
37  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43  * THE SOFTWARE.
44  */
45 
46 #ifndef COMMON_JSON_H
47 #define COMMON_JSON_H
48 
49 #include "common/array.h"
50 #include "common/hashmap.h"
51 #include "common/hash-str.h"
52 #include "common/memstream.h"
53 #include "common/str.h"
54 
55 // Win32 incompatibilities
56 #if (defined(WIN32) && !defined(__GNUC__))
57 static inline bool isnan(double x) {
58  return x != x;
59 }
60 
61 static inline bool isinf(double x) {
62  return !isnan(x) && isnan(x - x);
63 }
64 #endif
65 
66 // Simple function to check a string 's' has at least 'n' characters
67 static inline bool simplejson_wcsnlen(const char *s, size_t n) {
68  if (s == nullptr)
69  return false;
70 
71  const char *save = s;
72  while (n-- > 0) {
73  if (*(save++) == 0) return false;
74  }
75 
76  return true;
77 }
78 
79 namespace Common {
80 
81 // Custom types
82 class JSONValue;
83 typedef Array<JSONValue*> JSONArray;
84 typedef HashMap<String, JSONValue*> JSONObject;
85 
86 class JSON;
87 
88 enum JSONType { JSONType_Null, JSONType_String, JSONType_Bool, JSONType_Number, JSONType_IntegerNumber, JSONType_Array, JSONType_Object };
89 
90 class JSONValue {
91  friend class JSON;
92 
93 public:
94  JSONValue(/*NULL*/);
95  JSONValue(const char *charValue);
96  JSONValue(const String &stringValue);
97  JSONValue(bool boolValue);
98  JSONValue(double numberValue);
99  JSONValue(long long int numberValue);
100  JSONValue(const JSONArray &arrayValue);
101  JSONValue(const JSONObject &objectValue);
102  JSONValue(const JSONValue &source);
103  ~JSONValue();
104 
105  bool isNull() const;
106  bool isString() const;
107  bool isBool() const;
108  bool isNumber() const;
109  bool isIntegerNumber() const;
110  bool isArray() const;
111  bool isObject() const;
112 
113  const String &asString() const;
114  bool asBool() const;
115  double asNumber() const;
116  long long int asIntegerNumber() const;
117  const JSONArray &asArray() const;
118  const JSONObject &asObject() const;
119 
120  size_t countChildren() const;
121  bool hasChild(size_t index) const;
122  JSONValue *child(size_t index);
123  bool hasChild(const char *name) const;
124  JSONValue *child(const char *name);
125  Array<String> objectKeys() const;
126 
127  String stringify(bool const prettyprint = false) const;
128 protected:
129  static JSONValue *parse(const char **data);
130 
131 private:
132  static String stringifyString(const String &str);
133  static uint32 decodeUtf8Char(String::const_iterator &begin, const String::const_iterator &end);
134  static uint8 decodeUtf8Byte(uint8 state, uint32 &codepoint, uint8 byte);
135  String stringifyImpl(size_t const indentDepth) const;
136  static String indent(size_t depth);
137 
138  JSONType _type;
139 
140  union {
141  bool _boolValue;
142  double _numberValue;
143  long long int _integerValue;
144  String *_stringValue;
145  JSONArray *_arrayValue;
146  JSONObject *_objectValue;
147  };
148 
149 };
150 
151 class JSON {
152  friend class JSONValue;
153 
154 public:
156  static char *untaintContents(Common::MemoryWriteStreamDynamic &stream);
157 
158  static JSONValue *parse(const char *data);
159  static String stringify(const JSONValue *value);
160 protected:
161  static bool skipWhitespace(const char **data);
162  static bool extractString(const char **data, String &str);
163  static uint32 parseUnicode(const char **data);
164  static double parseInt(const char **data);
165  static double parseDecimal(const char **data);
166 private:
167  JSON();
168 };
169 
170 } // End of namespace Common
171 
172 #endif
Definition: str.h:59
Definition: memstream.h:194
Definition: json.h:90
Definition: algorithm.h:29
Definition: json.h:151