satyr  0.26
json.h
Go to the documentation of this file.
1 /*
2  json.h
3 
4  Copyright (C) 2012 James McLaughlin et al.
5  https://github.com/udp/json-parser
6 
7  Redistribution and use in source and binary forms, with or without
8  modification, are permitted provided that the following conditions
9  are met:
10 
11  1. Redistributions of source code must retain the above copyright
12  notice, this list of conditions and the following disclaimer.
13 
14  2. Redistributions in binary form must reproduce the above
15  copyright notice, this list of conditions and the following
16  disclaimer in the documentation and/or other materials provided
17  with the distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
20  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
23  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  POSSIBILITY OF SUCH DAMAGE.
31 */
32 #ifndef SATYR_JSON_H
33 #define SATYR_JSON_H
34 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 struct sr_location;
45 struct sr_strbuf;
46 
48 {
49  unsigned long max_memory;
50  int settings;
51 };
52 
53 #define SR_JSON_RELAXED_COMMAS 1
54 
55 enum sr_json_type
56 {
57  SR_JSON_NONE,
58  SR_JSON_OBJECT,
59  SR_JSON_ARRAY,
60  SR_JSON_INTEGER,
61  SR_JSON_DOUBLE,
62  SR_JSON_STRING,
63  SR_JSON_BOOLEAN,
64  SR_JSON_NULL
65 };
66 
67 extern const struct sr_json_value
68 sr_json_value_none;
69 
71 {
72  struct sr_json_value *parent;
73  enum sr_json_type type;
74 
75  union
76  {
77  int boolean;
78  long long integer;
79  double dbl;
80 
81  struct
82  {
83  unsigned length;
84 
85  /* null terminated */
86  char *ptr;
87  } string;
88 
89  struct
90  {
91  unsigned length;
92 
93  struct
94  {
95  char *name;
96  struct sr_json_value *value;
97  } *values;
98 
99  } object;
100 
101  struct
102  {
103  unsigned length;
104  struct sr_json_value **values;
105  } array;
106  } u;
107 
108  union
109  {
110  struct sr_json_value *next_alloc;
111  void *object_mem;
112  } _reserved;
113 };
114 
115 struct sr_json_value *
116 sr_json_parse(const char *json, char **error_message);
117 
118 struct sr_json_value *
119 sr_json_parse_ex(struct sr_json_settings *settings,
120  const char *json,
121  struct sr_location *location);
122 
123 void
124 sr_json_value_free(struct sr_json_value *value);
125 
126 char *
127 sr_json_escape(const char *text);
128 
129 struct sr_strbuf*
130 sr_json_append_escaped(struct sr_strbuf *strbuf, const char *str);
131 
132 #ifdef __cplusplus
133 } /* extern "C" */
134 #endif
135 
136 #endif
A resizable string buffer.
Definition: strbuf.h:38
A location of a parser in the input stream.
Definition: location.h:42