Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
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 // Win32 incompatibilities
29 #if defined(WIN32) && !defined(__GNUC__)
30  #define wcsncasecmp _wcsnicmp
31  static inline bool isnan(double x) { return x != x; }
32  static inline bool isinf(double x) { return !isnan(x) && isnan(x - x); }
33 #endif
34 
35 #include <vector>
36 #include <string>
37 #include <map>
38 
39 // Linux compile fix - from quaker66
40 #ifdef __GNUC__
41  #include <cstring>
42  #include <cstdlib>
43 #endif
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 // Custom types
89 class JSONValue;
90 typedef std::vector<JSONValue*> JSONArray;
91 typedef std::map<std::wstring, JSONValue*> JSONObject;
92 
93 #include "JSONValue.h"
94 
95 class JSON
96 {
97  friend class JSONValue;
98 
99  public:
100  static JSONValue* Parse(const char *data);
101  static JSONValue* Parse(const wchar_t *data);
102  static std::wstring Stringify(const JSONValue *value);
103  static std::wstring s2ws(const std::string& str);
104  static std::string ws2s(const std::wstring& wstr);
105  protected:
106  static bool SkipWhitespace(const wchar_t **data);
107  static bool ExtractString(const wchar_t **data, std::wstring &str);
108  static double ParseInt(const wchar_t **data);
109  static double ParseDecimal(const wchar_t **data);
110  private:
111  JSON();
112 };
113 
114 #endif
Definition: JSON.h:95
Definition: JSONValue.h:37