Changeset 5ebff60 for lib/json.h
- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
lib/json.h
raf359b4 r5ebff60 51 51 #include <string.h> 52 52 53 extern "C" 54 { 55 56 #endif 57 58 typedef struct 53 extern "C" 59 54 { 60 unsigned long max_memory; 61 int settings; 62 63 /* Custom allocator support (leave null to use malloc/free) 64 */ 65 66 void * (* mem_alloc) (size_t, int zero, void * user_data); 67 void (* mem_free) (void *, void * user_data); 68 69 void * user_data; /* will be passed to mem_alloc and mem_free */ 55 56 #endif 57 58 typedef struct { 59 unsigned long max_memory; 60 int settings; 61 62 /* Custom allocator support (leave null to use malloc/free) 63 */ 64 65 void * (*mem_alloc)(size_t, int zero, void * user_data); 66 void (* mem_free)(void *, void * user_data); 67 68 void * user_data; /* will be passed to mem_alloc and mem_free */ 70 69 71 70 } json_settings; … … 73 72 #define json_enable_comments 0x01 74 73 75 typedef enum 76 { 77 json_none, 78 json_object, 79 json_array, 80 json_integer, 81 json_double, 82 json_string, 83 json_boolean, 84 json_null 74 typedef enum { 75 json_none, 76 json_object, 77 json_array, 78 json_integer, 79 json_double, 80 json_string, 81 json_boolean, 82 json_null 85 83 86 84 } json_type; … … 88 86 extern const struct _json_value json_value_none; 89 87 90 typedef struct _json_value 91 { 92 struct _json_value * parent; 93 94 json_type type; 95 96 union 97 { 98 int boolean; 99 json_int_t integer; 100 double dbl; 101 102 struct 103 { 104 unsigned int length; 105 json_char * ptr; /* null terminated */ 106 107 } string; 108 109 struct 110 { 111 unsigned int length; 112 113 struct 114 { 115 json_char * name; 116 unsigned int name_length; 117 118 struct _json_value * value; 119 120 } * values; 121 122 #if defined(__cplusplus) && __cplusplus >= 201103L 123 decltype(values) begin () const 124 { return values; 125 } 126 decltype(values) end () const 127 { return values + length; 128 } 129 #endif 130 131 } object; 132 133 struct 134 { 135 unsigned int length; 136 struct _json_value ** values; 137 138 #if defined(__cplusplus) && __cplusplus >= 201103L 139 decltype(values) begin () const 140 { return values; 141 } 142 decltype(values) end () const 143 { return values + length; 144 } 145 #endif 146 147 } array; 148 149 } u; 150 151 union 152 { 153 struct _json_value * next_alloc; 154 void * object_mem; 155 156 } _reserved; 157 158 159 /* Some C++ operator sugar */ 88 typedef struct _json_value { 89 struct _json_value * parent; 90 91 json_type type; 92 93 union { 94 int boolean; 95 json_int_t integer; 96 double dbl; 97 98 struct { 99 unsigned int length; 100 json_char * ptr; /* null terminated */ 101 102 } string; 103 104 struct { 105 unsigned int length; 106 107 struct { 108 json_char * name; 109 unsigned int name_length; 110 111 struct _json_value * value; 112 113 } * values; 114 115 #if defined(__cplusplus) && __cplusplus >= 201103L 116 decltype(values) begin() const 117 { return values; } 118 decltype(values) end() const 119 { return values + length; } 120 #endif 121 122 } object; 123 124 struct { 125 unsigned int length; 126 struct _json_value ** values; 127 128 #if defined(__cplusplus) && __cplusplus >= 201103L 129 decltype(values) begin() const 130 { return values; } 131 decltype(values) end() const 132 { return values + length; } 133 #endif 134 135 } array; 136 137 } u; 138 139 union { 140 struct _json_value * next_alloc; 141 void * object_mem; 142 143 } _reserved; 144 145 146 /* Some C++ operator sugar */ 160 147 161 148 #ifdef __cplusplus 162 149 163 public: 164 165 inline _json_value () 166 { memset (this, 0, sizeof (_json_value)); 167 } 168 169 inline const struct _json_value &operator [] (int index) const 170 { 171 if (type != json_array || index < 0 172 || ((unsigned int) index) >= u.array.length) 173 { 174 return json_value_none; 175 } 176 177 return *u.array.values [index]; 178 } 179 180 inline const struct _json_value &operator [] (const char * index) const 181 { 182 if (type != json_object) 183 return json_value_none; 184 185 for (unsigned int i = 0; i < u.object.length; ++ i) 186 if (!strcmp (u.object.values [i].name, index)) 187 return *u.object.values [i].value; 188 189 return json_value_none; 190 } 191 192 inline operator const char * () const 193 { 194 switch (type) 195 { 196 case json_string: 197 return u.string.ptr; 198 199 default: 200 return ""; 201 }; 202 } 203 204 inline operator json_int_t () const 205 { 206 switch (type) 207 { 208 case json_integer: 209 return u.integer; 210 211 case json_double: 212 return (json_int_t) u.dbl; 213 214 default: 215 return 0; 216 }; 217 } 218 219 inline operator bool () const 220 { 221 if (type != json_boolean) 222 return false; 223 224 return u.boolean != 0; 225 } 226 227 inline operator double () const 228 { 229 switch (type) 230 { 231 case json_integer: 232 return (double) u.integer; 233 234 case json_double: 235 return u.dbl; 236 237 default: 238 return 0; 239 }; 240 } 150 public: 151 152 inline _json_value () 153 { 154 memset(this, 0, sizeof(_json_value)); 155 } 156 157 inline const struct _json_value &operator [](int index) const 158 { 159 if (type != json_array || index < 0 160 || ((unsigned int)index) >= u.array.length) { 161 return json_value_none; 162 } 163 164 return *u.array.values [index]; 165 } 166 167 inline const struct _json_value &operator [](const char * index) const 168 { 169 if (type != json_object) { 170 return json_value_none; 171 } 172 173 for (unsigned int i = 0; i < u.object.length; ++i) { 174 if (!strcmp(u.object.values [i].name, index)) { 175 return *u.object.values [i].value; 176 } 177 } 178 179 return json_value_none; 180 } 181 182 inline operator const char *() const 183 { 184 switch (type) { 185 case json_string: 186 return u.string.ptr; 187 188 default: 189 return ""; 190 } 191 ; 192 } 193 194 inline operator json_int_t() const 195 { 196 switch (type) { 197 case json_integer: 198 return u.integer; 199 200 case json_double: 201 return (json_int_t)u.dbl; 202 203 default: 204 return 0; 205 } 206 ; 207 } 208 209 inline operator bool() const 210 { 211 if (type != json_boolean) { 212 return false; 213 } 214 215 return u.boolean != 0; 216 } 217 218 inline operator double() const 219 { 220 switch (type) { 221 case json_integer: 222 return (double)u.integer; 223 224 case json_double: 225 return u.dbl; 226 227 default: 228 return 0; 229 } 230 ; 231 } 241 232 242 233 #endif … … 244 235 } json_value; 245 236 246 json_value * json_parse 247 237 json_value * json_parse(const json_char * json, 238 size_t length); 248 239 249 240 #define json_error_max 128 250 json_value * json_parse_ex 251 252 253 254 255 void json_value_free 241 json_value * json_parse_ex(json_settings * settings, 242 const json_char * json, 243 size_t length, 244 char * error); 245 246 void json_value_free(json_value *); 256 247 257 248 … … 259 250 * use a custom mem_free. 260 251 */ 261 void json_value_free_ex 262 252 void json_value_free_ex(json_settings * settings, 253 json_value *); 263 254 264 255 265 256 #ifdef __cplusplus 266 }/* extern "C" */267 #endif 268 269 #endif 270 271 257 } /* extern "C" */ 258 #endif 259 260 #endif 261 262
Note: See TracChangeset
for help on using the changeset viewer.