Robotran C Documentation
json.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 Joseph A. Adams (joeyadams3.14159@gmail.com)
3  All rights reserved.
4 
5  Permission is hereby granted, free of charge, to any person obtaining a copy
6  of this software and associated documentation files (the "Software"), to deal
7  in the Software without restriction, including without limitation the rights
8  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9  copies of the Software, and to permit persons to whom the Software is
10  furnished to do so, subject to the following conditions:
11 
12  The above copyright notice and this permission notice shall be included in
13  all copies or substantial portions of the Software.
14 
15  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  THE SOFTWARE.
22 */
23 
24 #ifndef CCAN_JSON_H
25 #define CCAN_JSON_H
26 
27 #include <stdbool.h>
28 #include <stddef.h>
29 
30 typedef enum {
37 } JsonTag;
38 
39 typedef struct JsonNode JsonNode;
40 
41 struct JsonNode
42 {
43  /* only if parent is an object or array (NULL otherwise) */
46 
47  /* only if parent is an object (NULL otherwise) */
48  char *key; /* Must be valid UTF-8. */
49 
51  union {
52  /* JSON_BOOL */
53  bool bool_;
54 
55  /* JSON_STRING */
56  char *string_; /* Must be valid UTF-8. */
57 
58  /* JSON_NUMBER */
59  double number_;
60 
61  /* JSON_ARRAY */
62  /* JSON_OBJECT */
63  struct {
65  } children;
66  };
67 };
68 
69 
70 #ifdef __cplusplus
71 #define EXTERNC extern "C"
72 #else
73 #define EXTERNC
74 #endif
75 
76 /*** Construction and manipulation ***/
77 
79 
80 /*** Encoding, decoding, and validation ***/
81 
82 EXTERNC JsonNode *json_decode (const char *json);
83 EXTERNC bool json_validate(const char* json);
84 
85 
86 char* json_encode(const JsonNode* node);
87 char* json_encode_string(const char* str);
88 char* json_stringify(const JsonNode* node, const char* space);
89 EXTERNC void json_delete(JsonNode* node);
90 
91 /*** Lookup and traversal ***/
92 EXTERNC JsonNode* json_find_element(JsonNode* array, int index);
93 EXTERNC JsonNode* json_find_member(JsonNode* object, const char* key);
94 
96 
97 
98 
99 
100 #define json_foreach(i, object_or_array) \
101  for ((i) = json_first_child(object_or_array); \
102  (i) != NULL; \
103  (i) = (i)->next)
104 
105 /*** Debugging ***/
106 
107 /*
108  * Look for structure and encoding problems in a JsonNode or its descendents.
109  *
110  * If a problem is detected, return false, writing a description of the problem
111  * to errmsg (unless errmsg is NULL).
112  */
113 EXTERNC bool json_check(const JsonNode* node, char errmsg[256]);
114 
115 #undef EXTERNC
116 
117 
118 
119 
120 
121 /*** Construction and manipulation ***/
122 
123 JsonNode *json_mkbool(bool b);
124 JsonNode *json_mkstring(const char *s);
125 JsonNode *json_mknumber(double n);
126 JsonNode *json_mkarray(void);
127 JsonNode *json_mkobject(void);
128 
129 void json_append_element(JsonNode *array, JsonNode *element);
130 void json_prepend_element(JsonNode *array, JsonNode *element);
131 void json_append_member(JsonNode *object, const char *key, JsonNode *value);
132 void json_prepend_member(JsonNode *object, const char *key, JsonNode *value);
133 
135 
136 
137 
138 #endif
json_stringify
char * json_stringify(const JsonNode *node, const char *space)
Definition: json.cc:393
sb_free
static void sb_free(SB *sb)
Definition: json.cc:112
json_find_member
EXTERNC JsonNode * json_find_member(JsonNode *object, const char *key)
Definition: json.cc:464
expect_literal
static bool expect_literal(const char **sp, const char *str)
Definition: json.cc:1254
json_encode_string
char * json_encode_string(const char *str)
Definition: json.cc:383
emit_value_indented
static void emit_value_indented(SB *out, const JsonNode *node, const char *space, int indent_level)
Definition: json.cc:1003
json_mkbool
JsonNode * json_mkbool(bool b)
Definition: json.cc:499
json_decode
JsonNode * json_decode(const char *json)
Definition: json.cc:361
sb_grow
static void sb_grow(SB *sb, int need)
Definition: json.cc:71
parse_array
static bool parse_array(const char **sp, JsonNode **out)
Definition: json.cc:691
mknode
static JsonNode * mknode(JsonTag tag)
Definition: json.cc:485
to_surrogate_pair
static void to_surrogate_pair(uchar_t unicode, uint16_t *uc, uint16_t *lc)
Definition: json.cc:317
json_mknull
EXTERNC JsonNode * json_mknull(void)
Definition: json.cc:494
parse_number
static bool parse_number(const char **sp, double *out)
Definition: json.cc:920
json_validate
EXTERNC bool json_validate(const char *json)
Definition: json.cc:432
json_first_child
EXTERNC JsonNode * json_first_child(const JsonNode *node)
Definition: json.cc:478
JsonNode::children
struct JsonNode::@0::@2 children
json_encode_string
char * json_encode_string(const char *str)
Definition: json.cc:383
json_encode
char * json_encode(const JsonNode *node)
Definition: json.cc:378
emit_array_indented
static void emit_array_indented(SB *out, const JsonNode *array, const char *space, int indent_level)
Definition: json.cc:1043
sb_puts
static void sb_puts(SB *sb, const char *str)
Definition: json.cc:100
json_mkobject
JsonNode * json_mkobject(void)
Definition: json.cc:530
JsonNode::bool_
bool bool_
Definition: json.h:53
sb_finish
static char * sb_finish(SB *sb)
Definition: json.cc:105
uchar_t
uint32_t uchar_t
Definition: json.cc:129
json_encode
char * json_encode(const JsonNode *node)
Definition: json.cc:378
json_foreach
#define json_foreach(i, object_or_array)
Definition: json.h:100
json_mknumber
JsonNode * json_mknumber(double n)
Definition: json.cc:518
SB
Definition: json.cc:49
JsonTag
JsonTag
Definition: json.h:30
JsonNode::prev
JsonNode * prev
Definition: json.h:45
parse_string
static bool parse_string(const char **sp, char **out)
Definition: json.cc:795
JSON_OBJECT
@ JSON_OBJECT
Definition: json.h:36
write_hex16
static int write_hex16(char *out, uint16_t val)
Definition: json.cc:1303
json_find_element
EXTERNC JsonNode * json_find_element(JsonNode *array, int index)
Definition: json.cc:447
JsonNode::next
JsonNode * next
Definition: json.h:45
emit_number
static void emit_number(SB *out, double num)
Definition: json.cc:1227
json_remove_from_parent
void json_remove_from_parent(JsonNode *node)
Definition: json.cc:600
json_append_member
void json_append_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.cc:583
utf8_validate
static bool utf8_validate(const char *s)
Definition: json.cc:206
json_append_element
void json_append_element(JsonNode *array, JsonNode *element)
Definition: json.cc:567
SB::start
char * start
Definition: json.cc:53
from_surrogate_pair
static bool from_surrogate_pair(uint16_t uc, uint16_t lc, uchar_t *unicode)
Definition: json.cc:302
json_validate
bool json_validate(const char *json)
Definition: json.cc:432
json_prepend_member
void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.cc:591
json_mkobject
JsonNode * json_mkobject(void)
Definition: json.cc:530
json_mkstring
JsonNode * json_mkstring(const char *s)
Definition: json.cc:513
json_stringify
char * json_stringify(const JsonNode *node, const char *space)
Definition: json.cc:393
JsonNode
Definition: json.h:41
JSON_NUMBER
@ JSON_NUMBER
Definition: json.h:34
emit_object
static void emit_object(SB *out, const JsonNode *object)
Definition: json.cc:1067
JsonNode::tail
JsonNode * tail
Definition: json.h:64
sb_need
#define sb_need(sb, need)
Definition: json.cc:66
JsonNode::key
char * key
Definition: json.h:48
json_strdup
static char * json_strdup(const char *str)
Definition: json.cc:38
json_remove_from_parent
void json_remove_from_parent(JsonNode *node)
Definition: json.cc:600
sb_init
static void sb_init(SB *sb)
Definition: json.cc:56
json_prepend_element
void json_prepend_element(JsonNode *array, JsonNode *element)
Definition: json.cc:575
json_find_element
JsonNode * json_find_element(JsonNode *array, int index)
Definition: json.cc:447
parse_value
static bool parse_value(const char **sp, JsonNode **out)
Definition: json.cc:622
utf8_read_char
static int utf8_read_char(const char *s, uchar_t *out)
Definition: json.cc:226
EXTERNC
#define EXTERNC
Definition: json.h:73
is_digit
#define is_digit(c)
Definition: json.cc:329
emit_string
static void emit_string(SB *out, const char *str)
Definition: json.cc:1108
JSON_STRING
@ JSON_STRING
Definition: json.h:33
SB::cur
char * cur
Definition: json.cc:51
skip_space
static void skip_space(const char **sp)
Definition: json.cc:968
sb_put
static void sb_put(SB *sb, const char *bytes, int count)
Definition: json.cc:87
json_mknumber
JsonNode * json_mknumber(double n)
Definition: json.cc:518
JsonNode::head
JsonNode * head
Definition: json.h:64
parse_hex16
static bool parse_hex16(const char **sp, uint16_t *out)
Definition: json.cc:1270
emit_object_indented
static void emit_object_indented(SB *out, const JsonNode *object, const char *space, int indent_level)
Definition: json.cc:1082
sb_putc
#define sb_putc(sb, c)
Definition: json.cc:94
json_append_member
void json_append_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.cc:583
json_prepend_member
void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.cc:591
json_mkbool
JsonNode * json_mkbool(bool b)
Definition: json.cc:499
SB::end
char * end
Definition: json.cc:52
prepend_node
static void prepend_node(JsonNode *parent, JsonNode *child)
Definition: json.cc:548
out_of_memory
#define out_of_memory()
Definition: json.cc:32
append_member
static void append_member(JsonNode *object, char *key, JsonNode *value)
Definition: json.cc:561
JsonNode::tag
JsonTag tag
Definition: json.h:50
problem
#define problem(...)
emit_array
static void emit_array(SB *out, const JsonNode *array)
Definition: json.cc:1030
json_mknull
JsonNode * json_mknull(void)
Definition: json.cc:494
JSON_NULL
@ JSON_NULL
Definition: json.h:31
json.h
json_delete
void json_delete(JsonNode *node)
Definition: json.cc:406
tag_is_valid
static bool tag_is_valid(unsigned int tag)
Definition: json.cc:1244
emit_value
static void emit_value(SB *out, const JsonNode *node)
Definition: json.cc:976
json_check
EXTERNC bool json_check(const JsonNode *node, char errmsg[256])
Definition: json.cc:1315
append_node
static void append_node(JsonNode *parent, JsonNode *child)
Definition: json.cc:535
json_decode
EXTERNC JsonNode * json_decode(const char *json)
Definition: json.cc:361
json_first_child
JsonNode * json_first_child(const JsonNode *node)
Definition: json.cc:478
parse_object
static bool parse_object(const char **sp, JsonNode **out)
Definition: json.cc:735
JsonNode::string_
char * string_
Definition: json.h:56
JSON_ARRAY
@ JSON_ARRAY
Definition: json.h:35
number_is_valid
static bool number_is_valid(const char *num)
Definition: json.cc:1249
JsonNode::parent
JsonNode * parent
Definition: json.h:44
json_delete
EXTERNC void json_delete(JsonNode *node)
Definition: json.cc:406
JSON_BOOL
@ JSON_BOOL
Definition: json.h:32
mkstring
static JsonNode * mkstring(char *s)
Definition: json.cc:506
json_mkstring
JsonNode * json_mkstring(const char *s)
Definition: json.cc:513
JsonNode::number_
double number_
Definition: json.h:59
json_append_element
void json_append_element(JsonNode *array, JsonNode *element)
Definition: json.cc:567
json_mkarray
JsonNode * json_mkarray(void)
Definition: json.cc:525
json_prepend_element
void json_prepend_element(JsonNode *array, JsonNode *element)
Definition: json.cc:575
utf8_validate_cz
static int utf8_validate_cz(const char *s)
Definition: json.cc:151
json_mkarray
JsonNode * json_mkarray(void)
Definition: json.cc:525
utf8_write_char
static int utf8_write_char(uchar_t unicode, char *out)
Definition: json.cc:265
is_space
#define is_space(c)
Definition: json.cc:328
json_check
bool json_check(const JsonNode *node, char errmsg[256])
Definition: json.cc:1315
json_find_member
JsonNode * json_find_member(JsonNode *object, const char *name)
Definition: json.cc:464