1 /*
2  * Copyright (c) 2013 Yaroslav Stavnichiy <yarosla@gmail.com>
3  *
4  * This file is part of NXJSON.
5  *
6  * NXJSON is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * NXJSON is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with NXJSON. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef NXJSON_H
21 #define NXJSON_H
22 
23 #ifdef  __cplusplus
24 extern "C" {
25 #endif
26 
27 
28 typedef enum nx_json_type {
29   NX_JSON_NULL,    // this is null value
30   NX_JSON_OBJECT,  // this is an object; properties can be found in child nodes
31   NX_JSON_ARRAY,   // this is an array; items can be found in child nodes
32   NX_JSON_STRING,  // this is a string; value can be found in text_value field
33   NX_JSON_INTEGER, // this is an integer; value can be found in int_value field
34   NX_JSON_DOUBLE,  // this is a double; value can be found in dbl_value field
35   NX_JSON_BOOL     // this is a boolean; value can be found in int_value field
36 } nx_json_type;
37 
38 typedef struct nx_json {
39   nx_json_type type;       // type of json node, see above
40   const char* key;         // key of the property; for object's children only
41   const char* text_value;  // text value of STRING node
42   long long int_value;     // the value of INTEGER or BOOL node
43   double dbl_value;        // the value of DOUBLE node
44   int length;              // number of children of OBJECT or ARRAY
45   struct nx_json* child;   // points to first child
46   struct nx_json* next;    // points to next child
47   struct nx_json* last_child;
48 } nx_json;
49 
50 typedef int (*nx_json_unicode_encoder)(unsigned int codepoint, char* p, char** endp);
51 
52 extern nx_json_unicode_encoder nx_json_unicode_to_utf8;
53 
54 const nx_json* nx_json_parse(char* text, nx_json_unicode_encoder encoder);
55 const nx_json* nx_json_parse_utf8(char* text);
56 void nx_json_free(const nx_json* js);
57 const nx_json* nx_json_get(const nx_json* json, const char* key); // get object's property by key
58 const nx_json* nx_json_item(const nx_json* json, int idx); // get array element by index
59 
60 
61 #ifdef  __cplusplus
62 }
63 #endif
64 
65 #endif  /* NXJSON_H */
66