libdas2
das2 core C utilities
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
json.h
Go to the documentation of this file.
1 // The latest version of this library is available on GitHub;
2 // https://github.com/sheredom/json.h
3 
4 // This is free and unencumbered software released into the public domain.
5 //
6 // Anyone is free to copy, modify, publish, use, compile, sell, or
7 // distribute this software, either in source code form or as a compiled
8 // binary, for any purpose, commercial or non-commercial, and by any
9 // means.
10 //
11 // In jurisdictions that recognize copyright laws, the author or authors
12 // of this software dedicate any and all copyright interest in the
13 // software to the public domain. We make this dedication for the benefit
14 // of the public at large and to the detriment of our heirs and
15 // successors. We intend this dedication to be an overt act of
16 // relinquishment in perpetuity of all present and future rights to this
17 // software under copyright law.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 // IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 // OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // For more information, please refer to <http://unlicense.org/>
28 
34 /* das2 developer modification note:
35  *
36  * Neil Hennings's JSON library is quite good and I can imagine many others
37  * would pick it up (I know I adopted it instead of json-c), so I've changed
38  * the names of all the visible objects. Structure names have been changed to
39  * blend in with the rest of the libdas2 and functions have had their prefixes
40  * changed from "json_" to "das_j". If I could be sure that libdas2 users
41  * would never need to access raw json objects, this change would not be
42  * necessary. However preventing access to the raw objects limits libdas2's
43  * usefulness, hence this trivial token transformation. Original "json.h"
44  * logic has not been altered in any way, though some new functions have been
45  * added at the end of the C file.
46  *
47  * Thanks to Neil, for helping out space physics research.
48  * -cwp 2018-10-14
49  */
50 
51 #ifndef _das_json_h_
52 #define _das_json_h_
53 
54 #if defined(_MSC_VER)
55 #pragma warning(push)
56 
57 // disable 'bytes padding added after construct' warning
58 #pragma warning(disable : 4820)
59 #endif
60 
61 #include <stddef.h>
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
67 
75  das_json_type_str,
76  das_json_type_num,
77  das_json_type_dict,
78  das_json_type_ary,
79  das_json_type_true,
80  das_json_type_false,
81  das_json_type_null
82 };
83 
85 typedef struct das_json_str_s {
87  const char *string;
89  size_t string_size;
90 } das_json_str;
91 
93 typedef struct das_json_str_ex_s {
95  struct das_json_str_s string;
96 
98  size_t offset;
99 
101  size_t line_no;
102 
104  size_t row_no;
106 
108 typedef struct das_json_num_s {
110  const char *number;
112  size_t number_size;
113 } das_json_num;
114 
116 typedef struct das_json_dict_el_s {
118  struct das_json_str_s *name;
120  struct das_json_obj_s *value;
122  struct das_json_dict_el_s *next;
124 
126 typedef struct das_json_dict_s {
128  struct das_json_dict_el_s *start;
130  size_t length;
131 } das_json_dict;
132 
134 typedef struct das_json_ary_el_s {
136  struct das_json_obj_s *value;
138  struct das_json_ary_el_s *next;
140 
142 typedef struct das_json_ary_s {
144  struct das_json_ary_el_s *start;
146  size_t length;
147 } das_json_ary;
148 
149 
150 
152 typedef struct das_json_obj_s {
157  void* value;
158 
161  size_t type;
162 
163 } DasJdo;
164 
168 typedef struct das_json_val_ex_s {
171 
173  size_t offset;
174 
176  size_t line_no;
177 
179  size_t row_no;
181 
182 
183 
186  das_jparse_flags_default = 0,
187 
192 
197 
202 
207 
212 
217 
218  // deprecated flag, unused
219  das_jparse_flags_deprecated = 0x40,
220 
223 
226 
229 
232 
235 
238 
241 
251 
265 };
266 
271 
276 
279 
282 
285 
288 
291 
294 
297 
300 
305 
308 };
309 
315  size_t error;
316 
318  size_t error_offset;
319 
322 
324  size_t error_row_no;
325 };
326 
328 const char* json_parse_error_info(
329  const struct das_json_parse_result_s* pRes, char* sTmp, size_t uLen
330 );
331 
332 
358  const void *src, size_t src_size, size_t flags_bitset,
359  void *(*alloc_func_ptr)(void *, size_t), void *user_data,
360  struct das_json_parse_result_s *result
361 );
362 
363 
369 DasJdo* das_json_parse(const void *src, size_t src_size);
370 
371 
390 const DasJdo* DasJdo_get(const DasJdo* pThis, const char* sRelPath);
391 
393 const das_json_dict_el* DasJdo_dictFirst(const DasJdo* pThis);
394 
396 const das_json_ary_el* DasJdo_aryFirst(const DasJdo* pThis);
397 
398 
399 
406 const char* DasJdo_string(const DasJdo* pThis);
407 
408 
409 const das_json_dict* DasJdo_dict(const DasJdo* pThis);
410 
411 
412 
428 void* DasJdo_writeMinified(const DasJdo* pThis, size_t *out_size);
429 
430 
445 void* DasJdo_writePretty(const DasJdo* pThis, const char *indent,
446  const char *newline, size_t *out_size);
447 
448 
451 #ifdef __cplusplus
452 } // extern "C"
453 #endif
454 
455 #if defined(_MSC_VER)
456 #pragma warning(pop)
457 #endif
458 
459 #endif // _das_json_h_
A JSON number value.
Definition: json.h:108
allow multi line string values
Definition: json.h:240
allow numbers to be hexadecimal
Definition: json.h:228
catch-all error for everything else that exploded (real bad chi!)
Definition: json.h:307
struct das_json_dict_el_s * start
a linked list of the elements in the object
Definition: json.h:128
a JSON value (extended)
Definition: json.h:168
allow numbers like .0123 or 123.
Definition: json.h:234
size_t string_size
the size (in bytes) of the string
Definition: json.h:89
const char * DasJdo_string(const DasJdo *pThis)
Get a string value from a JSON DOM element.
size_t number_size
the size (in bytes) of the number
Definition: json.h:112
const das_json_ary_el * DasJdo_aryFirst(const DasJdo *pThis)
Get the first array element from a JSON array.
A JSON string value (extended)
Definition: json.h:93
allow JSON5 to be parsed.
Definition: json.h:255
DasJdo * das_json_parse_ex(const void *src, size_t src_size, size_t flags_bitset, void *(*alloc_func_ptr)(void *, size_t), void *user_data, struct das_json_parse_result_s *result)
Parse a JSON text file, returning a pointer to the root of the JSON structure.
void * DasJdo_writeMinified(const DasJdo *pThis, size_t *out_size)
Write out a minified JSON utf-8 string.
const das_json_dict_el * DasJdo_dictFirst(const DasJdo *pThis)
Get the first dictionary element from a JSON dictionary.
das_json_parse_flags_e
Flag useed by dasj_parse() and dasj_parse_ex() to alter parsing behavior.
Definition: json.h:185
string was malformed!
Definition: json.h:296
size_t row_no
the row number for the value in the JSON input, in bytes
Definition: json.h:104
size_t row_no
the row number for the value in the JSON input, in bytes
Definition: json.h:179
das_json_type_e
The various types JSON values can be.
Definition: json.h:74
size_t line_no
the line number for the value in the JSON input
Definition: json.h:101
allow a global unbracketed object.
Definition: json.h:201
size_t error_row_no
the row number for the error, in bytes
Definition: json.h:324
allow strings to be &#39;single quoted&#39;
Definition: json.h:225
the JSON input had unexpected trailing characters that weren&#39;t part of the JSON value ...
Definition: json.h:304
a JSON dictionary payload
Definition: json.h:126
size_t error_offset
the character offset for the error in the JSON input
Definition: json.h:318
DasJdo value
the JSON value this extends.
Definition: json.h:170
allow numbers like +123 to be parsed
Definition: json.h:231
struct das_json_str_s * name
the name of this element
Definition: json.h:118
size_t line_no
the line number for the value in the JSON input
Definition: json.h:176
record location information for each value.
Definition: json.h:222
const char * string
utf-8 string
Definition: json.h:87
allow trailing commas in objects and arrays.
Definition: json.h:191
struct das_json_obj_s * value
the value of this element
Definition: json.h:120
error report from json_parse_ex()
Definition: json.h:311
reached end of buffer before object/array was complete!
Definition: json.h:293
const DasJdo * DasJdo_get(const DasJdo *pThis, const char *sRelPath)
Given a DOM path retrieve a JSON element.
allow that objects don&#39;t have to have comma separators between key/value pairs.
Definition: json.h:211
const char * json_parse_error_info(const struct das_json_parse_result_s *pRes, char *sTmp, size_t uLen)
Provide error string describing a parsing error result.
no error occurred (huzzah!)
Definition: json.h:270
size_t length
the number of elements in the array
Definition: json.h:146
size_t error
the error code (one of json_parse_error_e), use dasj_parse_error_info() To convert the value to an er...
Definition: json.h:315
A JSON string value.
Definition: json.h:85
invalid number format!
Definition: json.h:287
allow simplified JSON to be parsed.
Definition: json.h:245
an element of a JSON array
Definition: json.h:134
allow Infinity, -Infinity, NaN, -NaN
Definition: json.h:237
size_t length
the number of elements in the object
Definition: json.h:130
allow objects to use &#39;=&#39; instead of &#39;:&#39; between key/value pairs.
Definition: json.h:206
invalid value!
Definition: json.h:290
allow unquoted keys for objects.
Definition: json.h:196
size_t error_line_no
the line number for the error in the JSON input
Definition: json.h:321
size_t offset
the character offset for the value in the JSON input
Definition: json.h:173
An element of a JSON dictionary.
Definition: json.h:116
const char * number
ASCII string containing representation of the number.
Definition: json.h:110
size_t type
must be one of das_json_type_e.
Definition: json.h:161
invalid escaped sequence in string!
Definition: json.h:284
expected either a comma or a closing &#39;}&#39; or &#39;]&#39; to close an object or array!
Definition: json.h:275
void * DasJdo_writePretty(const DasJdo *pThis, const char *indent, const char *newline, size_t *out_size)
Write out a pretty JSON utf-8 string.
a call to malloc, or a user provider allocator, failed
Definition: json.h:299
JSON Dom Element.
Definition: json.h:152
a JSON array value
Definition: json.h:142
colon separating name/value pair was missing!
Definition: json.h:278
size_t offset
the character offset for the value in the JSON input
Definition: json.h:98
struct das_json_ary_el_s * start
a linked list of the elements in the array
Definition: json.h:144
struct das_json_obj_s * value
the value of this element
Definition: json.h:136
void * value
a pointer to either a das_json_str, das_json_num, das_json_dict, or das_json_ary. ...
Definition: json.h:157
DasJdo * das_json_parse(const void *src, size_t src_size)
Parse a JSON text file with default options and without detailed error reporting. ...
expected string to begin with &#39;&quot;&#39;!
Definition: json.h:281
struct das_json_ary_el_s * next
the next array element (can be NULL if the last element in the array)
Definition: json.h:138
allow c-style comments (// or /* *\/) to be ignored in the input JSON file.
Definition: json.h:216
struct das_json_dict_el_s * next
the next object element (can be NULL if the last element in the object)
Definition: json.h:122
das_jparse_error_e
JSON parsing error codes.
Definition: json.h:268