Atoms Crowd  7.0.0
JSON.h
1 /*
2  * File JSON.h part of the SimpleJSON Library - http://mjpa.in/json
3  *
4  * Copyright (C) 2010 Mike Anchor
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #ifndef _JSON_H_
26 #define _JSON_H_
27 
28 #include <AtomsUtils/Globals.h>
29 
30 #include <float.h>
31 
32 #include <vector>
33 #include <string>
34 #include <map>
35 
36 // Linux compile fix - from quaker66
37 #ifdef __GNUC__
38  #include <cstring>
39  #include <cstdlib>
40 #endif
41 
42 namespace AtomsUtils
43 {
44 
45  // Mac compile fixes - from quaker66, Lion fix by dabrahams
46 #if defined(__APPLE__) && __DARWIN_C_LEVEL < 200809L || (defined(WIN32) && defined(__GNUC__)) || defined(ANDROID)
47 #include <wctype.h>
48 #include <wchar.h>
49 
50  static inline int wcsncasecmp(const wchar_t *s1, const wchar_t *s2, size_t n)
51  {
52  int lc1 = 0;
53  int lc2 = 0;
54 
55  while (n--)
56  {
57  lc1 = towlower(*s1);
58  lc2 = towlower(*s2);
59 
60  if (lc1 != lc2)
61  return (lc1 - lc2);
62 
63  if (!lc1)
64  return 0;
65 
66  ++s1;
67  ++s2;
68  }
69 
70  return 0;
71  }
72 #endif
73 
74  // Simple function to check a string 's' has at least 'n' characters
75  static inline bool simplejson_wcsnlen(const wchar_t *s, size_t n) {
76  if (s == 0)
77  return false;
78 
79  const wchar_t *save = s;
80  while (n-- > 0)
81  {
82  if (*(save++) == 0) return false;
83  }
84 
85  return true;
86  }
87 
88  class ATOMSUTILS_EXPORT JSON;
89  class ATOMSUTILS_EXPORT JSONValue;
90  typedef std::vector<JSONValue*> JSONArray;
91  typedef std::map<std::wstring, JSONValue*> JSONObject;
92 
93  // Custom types
94  enum JSONType { JSONType_Null, JSONType_String, JSONType_Bool, JSONType_Number, JSONType_Array, JSONType_Object };
95 
96  class ATOMSUTILS_EXPORT JSONValue
97  {
98  friend class JSON;
99 
100  public:
101  JSONValue(/*NULL*/);
102  JSONValue(const wchar_t *m_char_value);
103  JSONValue(const std::wstring &m_string_value);
104  JSONValue(bool m_bool_value);
105  JSONValue(double m_number_value);
106  JSONValue(const JSONArray &m_array_value);
107  JSONValue(const JSONObject &m_object_value);
108  JSONValue(const JSONValue &m_source);
109  ~JSONValue();
110 
111  bool IsNull() const;
112  bool IsString() const;
113  bool IsBool() const;
114  bool IsNumber() const;
115  bool IsArray() const;
116  bool IsObject() const;
117 
118  const std::wstring &AsString() const;
119  bool AsBool() const;
120  double AsNumber() const;
121  const JSONArray &AsArray() const;
122  const JSONObject &AsObject() const;
123 
124  std::size_t CountChildren() const;
125  bool HasChild(std::size_t index) const;
126  JSONValue *Child(std::size_t index);
127  bool HasChild(const wchar_t* name) const;
128  JSONValue *Child(const wchar_t* name);
129  std::vector<std::wstring> ObjectKeys() const;
130 
131  std::wstring Stringify(bool const prettyprint = false) const;
132  protected:
133  static JSONValue *Parse(const wchar_t **data);
134 
135  private:
136  static std::wstring StringifyString(const std::wstring &str);
137  std::wstring StringifyImpl(size_t const indentDepth) const;
138  static std::wstring Indent(size_t depth);
139 
140  JSONType type;
141 
142  union
143  {
144  bool bool_value;
145  double number_value;
146  std::wstring *string_value;
147  JSONArray *array_value;
148  JSONObject *object_value;
149  };
150 
151  };
152 
153  class ATOMSUTILS_EXPORT JSON
154  {
155  friend class JSONValue;
156 
157  public:
158  static JSONValue* Parse(const char *data);
159  static JSONValue* Parse(const wchar_t *data);
160  static std::wstring Stringify(const JSONValue *value);
161  protected:
162  static bool SkipWhitespace(const wchar_t **data);
163  static bool ExtractString(const wchar_t **data, std::wstring &str);
164  static double ParseInt(const wchar_t **data);
165  static double ParseDecimal(const wchar_t **data);
166  private:
167  JSON();
168  };
169 
170 }
171 
172 #endif
Definition: JSON.h:154
Definition: JSON.h:97
AtomsCore namespace.
Definition: Base64.h:13