Robotran C Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 EXTERNC char* json_encode(const JsonNode* node);
87 char* json_encode_string(const char* str);
88 EXTERNC 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 
116 
117 
118 
119 
120 /*** Construction and manipulation ***/
121 
122 EXTERNC JsonNode *json_mkbool(bool b);
123 EXTERNC JsonNode *json_mkstring(const char *s);
124 EXTERNC JsonNode *json_mknumber(double n);
127 
128 EXTERNC void json_append_element(JsonNode *array, JsonNode *element);
129 EXTERNC void json_prepend_element(JsonNode *array, JsonNode *element);
130 EXTERNC void json_append_member(JsonNode *object, const char *key, JsonNode *value);
131 EXTERNC void json_prepend_member(JsonNode *object, const char *key, JsonNode *value);
132 
134 
135 #undef EXTERNC
136 
137 
138 #endif
json_mkbool
JsonNode * json_mkbool(bool b)
Definition: json.c:500
json_find_member
EXTERNC JsonNode * json_find_member(JsonNode *object, const char *key)
Definition: json.c:465
sb_need
#define sb_need(sb, need)
Definition: json.c:66
json_encode_string
char * json_encode_string(const char *str)
Definition: json.c:384
json_mkobject
JsonNode * json_mkobject(void)
Definition: json.c:531
sb_put
static void sb_put(SB *sb, const char *bytes, int count)
Definition: json.c:87
json_mkobject
EXTERNC JsonNode * json_mkobject(void)
Definition: json.c:531
parse_string
static bool parse_string(const char **sp, char **out)
Definition: json.c:793
utf8_validate
static bool utf8_validate(const char *s)
Definition: json.c:206
JsonNode::children
struct JsonNode::@8::@10 children
out_of_memory
#define out_of_memory()
Definition: json.c:32
json_mknull
EXTERNC JsonNode * json_mknull(void)
Definition: json.c:495
parse_hex16
static bool parse_hex16(const char **sp, uint16_t *out)
Definition: json.c:1268
emit_string
static void emit_string(SB *out, const char *str)
Definition: json.c:1106
json_mkstring
EXTERNC JsonNode * json_mkstring(const char *s)
Definition: json.c:514
parse_array
static bool parse_array(const char **sp, JsonNode **out)
Definition: json.c:693
json_validate
EXTERNC bool json_validate(const char *json)
Definition: json.c:433
json_first_child
EXTERNC JsonNode * json_first_child(const JsonNode *node)
Definition: json.c:479
sb_init
static void sb_init(SB *sb)
Definition: json.c:56
json_first_child
JsonNode * json_first_child(const JsonNode *node)
Definition: json.c:479
json_prepend_element
EXTERNC void json_prepend_element(JsonNode *array, JsonNode *element)
Definition: json.c:576
JsonNode::bool_
bool bool_
Definition: json.h:53
json_encode
EXTERNC char * json_encode(const JsonNode *node)
Definition: json.c:379
json_prepend_member
EXTERNC void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.c:592
json_foreach
#define json_foreach(i, object_or_array)
Definition: json.h:100
from_surrogate_pair
static bool from_surrogate_pair(uint16_t uc, uint16_t lc, uchar_t *unicode)
Definition: json.c:302
sb_grow
static void sb_grow(SB *sb, int need)
Definition: json.c:71
SB
Definition: json.c:49
to_surrogate_pair
static void to_surrogate_pair(uchar_t unicode, uint16_t *uc, uint16_t *lc)
Definition: json.c:317
json_validate
bool json_validate(const char *json)
Definition: json.c:433
JsonTag
JsonTag
Definition: json.h:30
JsonNode::prev
JsonNode * prev
Definition: json.h:45
JSON_OBJECT
@ JSON_OBJECT
Definition: json.h:36
json_find_element
EXTERNC JsonNode * json_find_element(JsonNode *array, int index)
Definition: json.c:448
json_remove_from_parent
void json_remove_from_parent(JsonNode *node)
Definition: json.c:601
emit_number
static void emit_number(SB *out, double num)
Definition: json.c:1225
JsonNode::next
JsonNode * next
Definition: json.h:45
tag_is_valid
static bool tag_is_valid(unsigned int tag)
Definition: json.c:1242
json_mkbool
EXTERNC JsonNode * json_mkbool(bool b)
Definition: json.c:500
json_mkarray
JsonNode * json_mkarray(void)
Definition: json.c:526
json_mkarray
EXTERNC JsonNode * json_mkarray(void)
Definition: json.c:526
number_is_valid
static bool number_is_valid(const char *num)
Definition: json.c:1247
json_append_member
EXTERNC void json_append_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.c:584
json_mkstring
JsonNode * json_mkstring(const char *s)
Definition: json.c:514
SB::start
char * start
Definition: json.c:53
emit_array
static void emit_array(SB *out, const JsonNode *array)
Definition: json.c:1028
json_delete
void json_delete(JsonNode *node)
Definition: json.c:407
json_check
bool json_check(const JsonNode *node, char errmsg[256])
Definition: json.c:1313
json_find_element
JsonNode * json_find_element(JsonNode *array, int index)
Definition: json.c:448
json_prepend_element
void json_prepend_element(JsonNode *array, JsonNode *element)
Definition: json.c:576
problem
#define problem(...)
json_encode_string
char * json_encode_string(const char *str)
Definition: json.c:384
append_member
static void append_member(JsonNode *object, char *key, JsonNode *value)
Definition: json.c:562
JsonNode
Definition: json.h:41
JSON_NUMBER
@ JSON_NUMBER
Definition: json.h:34
utf8_validate_cz
static int utf8_validate_cz(const char *s)
Definition: json.c:151
json_remove_from_parent
EXTERNC void json_remove_from_parent(JsonNode *node)
Definition: json.c:601
emit_array_indented
static void emit_array_indented(SB *out, const JsonNode *array, const char *space, int indent_level)
Definition: json.c:1041
JsonNode::tail
JsonNode * tail
Definition: json.h:64
append_node
static void append_node(JsonNode *parent, JsonNode *child)
Definition: json.c:536
JsonNode::key
char * key
Definition: json.h:48
sb_putc
#define sb_putc(sb, c)
Definition: json.c:94
mkstring
static JsonNode * mkstring(char *s)
Definition: json.c:507
uchar_t
uint32_t uchar_t
Definition: json.c:129
mknode
static JsonNode * mknode(JsonTag tag)
Definition: json.c:486
EXTERNC
#define EXTERNC
Definition: json.h:73
parse_number
static bool parse_number(const char **sp, double *out)
Definition: json.c:918
utf8_read_char
static int utf8_read_char(const char *s, uchar_t *out)
Definition: json.c:226
json_encode
char * json_encode(const JsonNode *node)
Definition: json.c:379
json_append_element
EXTERNC void json_append_element(JsonNode *array, JsonNode *element)
Definition: json.c:568
json_stringify
EXTERNC char * json_stringify(const JsonNode *node, const char *space)
Definition: json.c:394
expect_literal
static bool expect_literal(const char **sp, const char *str)
Definition: json.c:1252
JSON_STRING
@ JSON_STRING
Definition: json.h:33
emit_object
static void emit_object(SB *out, const JsonNode *object)
Definition: json.c:1065
SB::cur
char * cur
Definition: json.c:51
json_strdup
static char * json_strdup(const char *str)
Definition: json.c:38
JsonNode::head
JsonNode * head
Definition: json.h:64
emit_value_indented
static void emit_value_indented(SB *out, const JsonNode *node, const char *space, int indent_level)
Definition: json.c:1001
json_find_member
JsonNode * json_find_member(JsonNode *object, const char *name)
Definition: json.c:465
is_space
#define is_space(c)
Definition: json.c:328
parse_object
static bool parse_object(const char **sp, JsonNode **out)
Definition: json.c:737
SB::end
char * end
Definition: json.c:52
json_mknull
JsonNode * json_mknull(void)
Definition: json.c:495
sb_free
static void sb_free(SB *sb)
Definition: json.c:112
JsonNode::tag
JsonTag tag
Definition: json.h:50
utf8_write_char
static int utf8_write_char(uchar_t unicode, char *out)
Definition: json.c:265
json_prepend_member
void json_prepend_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.c:592
write_hex16
static int write_hex16(char *out, uint16_t val)
Definition: json.c:1301
json_append_member
void json_append_member(JsonNode *object, const char *key, JsonNode *value)
Definition: json.c:584
JSON_NULL
@ JSON_NULL
Definition: json.h:31
json.h
sb_puts
static void sb_puts(SB *sb, const char *str)
Definition: json.c:100
sb_finish
static char * sb_finish(SB *sb)
Definition: json.c:105
json_check
EXTERNC bool json_check(const JsonNode *node, char errmsg[256])
Definition: json.c:1313
json_stringify
char * json_stringify(const JsonNode *node, const char *space)
Definition: json.c:394
emit_value
static void emit_value(SB *out, const JsonNode *node)
Definition: json.c:974
json_decode
EXTERNC JsonNode * json_decode(const char *json)
Definition: json.c:361
prepend_node
static void prepend_node(JsonNode *parent, JsonNode *child)
Definition: json.c:549
parse_value
static bool parse_value(const char **sp, JsonNode **out)
Definition: json.c:623
JsonNode::string_
char * string_
Definition: json.h:56
json_append_element
void json_append_element(JsonNode *array, JsonNode *element)
Definition: json.c:568
JSON_ARRAY
@ JSON_ARRAY
Definition: json.h:35
JsonNode::parent
JsonNode * parent
Definition: json.h:44
json_delete
EXTERNC void json_delete(JsonNode *node)
Definition: json.c:407
is_digit
#define is_digit(c)
Definition: json.c:329
JSON_BOOL
@ JSON_BOOL
Definition: json.h:32
JsonNode::number_
double number_
Definition: json.h:59
json_mknumber
JsonNode * json_mknumber(double n)
Definition: json.c:519
emit_object_indented
static void emit_object_indented(SB *out, const JsonNode *object, const char *space, int indent_level)
Definition: json.c:1080
json_decode
JsonNode * json_decode(const char *json)
Definition: json.c:361
json_mknumber
EXTERNC JsonNode * json_mknumber(double n)
Definition: json.c:519
skip_space
static void skip_space(const char **sp)
Definition: json.c:966