Mercurial > wgsdk
annotate include/json.h @ 12:dd427b7cc459 default tip
json: replace with nxjson library
more lightweight, reduces the binary size by about 40 kb
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 15 Mar 2024 20:46:18 -0400 |
parents | e6a594f16403 |
children |
rev | line source |
---|---|
11 | 1 /* |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
2 * Copyright (c) 2013 Yaroslav Stavnichiy <yarosla@gmail.com> |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
3 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
4 * This file is part of NXJSON. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
5 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
6 * NXJSON is free software: you can redistribute it and/or modify |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
7 * it under the terms of the GNU Lesser General Public License |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
8 * as published by the Free Software Foundation, either version 3 |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
9 * of the License, or (at your option) any later version. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
10 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
11 * NXJSON is distributed in the hope that it will be useful, |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
14 * GNU Lesser General Public License for more details. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
15 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
16 * You should have received a copy of the GNU Lesser General Public |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
17 * License along with NXJSON. If not, see <http://www.gnu.org/licenses/>. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
18 */ |
11 | 19 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
20 #ifndef NXJSON_H |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
21 #define NXJSON_H |
11 | 22 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
23 #ifdef __cplusplus |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
24 extern "C" { |
11 | 25 #endif |
26 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
27 #ifndef NXJSON_TYPE_U64 |
11 | 28 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
29 #include <stdint.h> |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
30 |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
31 typedef uint64_t nxjson_u64; |
11 | 32 #endif |
33 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
34 #ifndef NXJSON_TYPE_S64 |
11 | 35 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
36 #include <stdint.h> |
11 | 37 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
38 typedef uint64_t nxjson_s64; |
11 | 39 #endif |
40 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
41 typedef enum nx_json_type { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
42 NX_JSON_NULL, // this is null value |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
43 NX_JSON_OBJECT, // this is an object; properties can be found in child nodes |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
44 NX_JSON_ARRAY, // this is an array; items can be found in child nodes |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
45 NX_JSON_STRING, // this is a string; value can be found in text_value field |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
46 NX_JSON_INTEGER, // this is an integer; value can be found in int_value field |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
47 NX_JSON_DOUBLE, // this is a double; value can be found in dbl_value field |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
48 NX_JSON_BOOL // this is a boolean; value can be found in int_value field |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
49 } nx_json_type; |
11 | 50 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
51 typedef struct nx_json { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
52 nx_json_type type; // type of json node, see above |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
53 const char *key; // key of the property; for object's children only |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
54 union { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
55 const char *text_value; // text value of STRING node |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
56 struct { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
57 union { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
58 nxjson_u64 u_value; // the value of INTEGER or BOOL node |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
59 nxjson_s64 s_value; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
60 }; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
61 double dbl_value; // the value of DOUBLE node |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
62 } num; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
63 struct { // children of OBJECT or ARRAY |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
64 int length; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
65 struct nx_json *first; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
66 struct nx_json *last; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
67 } children; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
68 }; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
69 struct nx_json *next; // points to next child |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
70 } nx_json; |
11 | 71 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
72 typedef int (*nx_json_unicode_encoder)(unsigned int codepoint, char *p, char **endp); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
73 |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
74 extern nx_json_unicode_encoder nx_json_unicode_to_utf8; |
11 | 75 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
76 const nx_json *nx_json_parse(char *text, nx_json_unicode_encoder encoder); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
77 |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
78 const nx_json *nx_json_parse_utf8(char *text); |
11 | 79 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
80 void nx_json_free(const nx_json *js); |
11 | 81 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
82 const nx_json *nx_json_get(const nx_json *json, const char *key); // get object's property by key |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
83 const nx_json *nx_json_item(const nx_json *json, int idx); // get array element by index |
11 | 84 |
85 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
86 #ifdef __cplusplus |
11 | 87 } |
88 #endif | |
89 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
90 #endif /* NXJSON_H */ |