das2C
das core C utilities (v3)
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 */
29 
35 /* das2 developer modification note:
36  *
37  * Neil Hennings's JSON library is quite good and I can imagine many others
38  * would pick it up (I know I adopted it instead of json-c), so I've changed
39  * the names of all the visible objects. Structure names have been changed to
40  * blend in with the rest of the libdas2 and functions have had their prefixes
41  * changed from "json_" to "das_j". If I could be sure that libdas2 users
42  * would never need to access raw json objects, this change would not be
43  * necessary. However preventing access to the raw objects limits libdas2's
44  * usefulness, hence this trivial token transformation. Original "json.h"
45  * logic has not been altered in any way, though some new functions have been
46  * added at the end of the C file.
47  *
48  * Thanks to Neil, for helping out space physics research.
49  * -cwp 2018-10-14
50  */
51 
52 #ifndef _das_json_h_
53 #define _das_json_h_
54 
55 #if defined(_MSC_VER)
56 #pragma warning(push)
57 
58 /* disable 'bytes padding added after construct' warning */
59 #pragma warning(disable : 4820)
60 #endif
61 
62 #include <das2/defs.h>
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 
76  das_json_type_str,
77  das_json_type_num,
78  das_json_type_dict,
79  das_json_type_ary,
80  das_json_type_true,
81  das_json_type_false,
82  das_json_type_null
83 };
84 
86 typedef struct das_json_str_s {
88  const char *string;
90  size_t string_size;
91 } das_json_str;
92 
94 typedef struct das_json_str_ex_s {
96  struct das_json_str_s string;
97 
99  size_t offset;
100 
102  size_t line_no;
103 
105  size_t row_no;
107 
109 typedef struct das_json_num_s {
111  const char *number;
113  size_t number_size;
114 } das_json_num;
115 
117 typedef struct das_json_dict_el_s {
119  struct das_json_str_s *name;
121  struct das_json_obj_s *value;
123  struct das_json_dict_el_s *next;
125 
127 typedef struct das_json_dict_s {
129  struct das_json_dict_el_s *start;
131  size_t length;
132 } das_json_dict;
133 
135 typedef struct das_json_ary_el_s {
137  struct das_json_obj_s *value;
139  struct das_json_ary_el_s *next;
141 
143 typedef struct das_json_ary_s {
145  struct das_json_ary_el_s *start;
147  size_t length;
148 } das_json_ary;
149 
150 
151 
153 typedef struct das_json_obj_s {
158  void* value;
159 
162  size_t type;
163 
164 } DasJdo;
165 
169 typedef struct das_json_val_ex_s {
172 
174  size_t offset;
175 
177  size_t line_no;
178 
180  size_t row_no;
182 
183 
184 
187  das_jparse_flags_default = 0,
188 
193 
198 
203 
208 
213 
218 
219  // deprecated flag, unused
220  das_jparse_flags_deprecated = 0x40,
221 
224 
227 
230 
233 
236 
239 
242 
252 
266 };
267 
272 
277 
280 
283 
286 
289 
292 
295 
298 
301 
306 
309 };
310 
316  size_t error;
317 
319  size_t error_offset;
320 
323 
325  size_t error_row_no;
326 };
327 
329 DAS_API const char* json_parse_error_info(
330  const struct das_json_parse_result_s* pRes, char* sTmp, size_t uLen
331 );
332 
333 
359  const void *src, size_t src_size, size_t flags_bitset,
360  void *(*alloc_func_ptr)(void *, size_t), void *user_data,
361  struct das_json_parse_result_s *result
362 );
363 
364 
370 DAS_API DasJdo* das_json_parse(const void *src, size_t src_size);
371 
372 
391 DAS_API const DasJdo* DasJdo_get(const DasJdo* pThis, const char* sRelPath);
392 
394 DAS_API const das_json_dict_el* DasJdo_dictFirst(const DasJdo* pThis);
395 
397 DAS_API const das_json_ary_el* DasJdo_aryFirst(const DasJdo* pThis);
398 
399 
400 
407 DAS_API const char* DasJdo_string(const DasJdo* pThis);
408 
409 
410 DAS_API const das_json_dict* DasJdo_dict(const DasJdo* pThis);
411 
412 
413 
429 DAS_API void* DasJdo_writeMinified(const DasJdo* pThis, size_t *out_size);
430 
431 
446 DAS_API void* DasJdo_writePretty(const DasJdo* pThis, const char *indent,
447  const char *newline, size_t *out_size);
448 
449 
452 #ifdef __cplusplus
453 } // extern "C"
454 #endif
455 
456 #if defined(_MSC_VER)
457 #pragma warning(pop)
458 #endif
459 
460 #endif // _das_json_h_
Minimal definitions for das2 utilities that can safely be run without calling das_init().
DAS_API void * DasJdo_writeMinified(const DasJdo *pThis, size_t *out_size)
Write out a minified JSON utf-8 string.
DAS_API const char * DasJdo_string(const DasJdo *pThis)
Get a string value from a JSON DOM element.
DAS_API 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.
DAS_API const DasJdo * DasJdo_get(const DasJdo *pThis, const char *sRelPath)
Given a DOM path retrieve a JSON element.
DAS_API DasJdo * das_json_parse(const void *src, size_t src_size)
Parse a JSON text file with default options and without detailed error reporting.
DAS_API const das_json_dict_el * DasJdo_dictFirst(const DasJdo *pThis)
Get the first dictionary element from a JSON dictionary.
DAS_API 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.
DAS_API const das_json_ary_el * DasJdo_aryFirst(const DasJdo *pThis)
Get the first array element from a JSON array.
DAS_API void * DasJdo_writePretty(const DasJdo *pThis, const char *indent, const char *newline, size_t *out_size)
Write out a pretty JSON utf-8 string.
das_json_parse_flags_e
Flag useed by dasj_parse() and dasj_parse_ex() to alter parsing behavior.
Definition: json.h:186
das_jparse_error_e
JSON parsing error codes.
Definition: json.h:269
das_json_type_e
The various types JSON values can be.
Definition: json.h:75
@ das_jparse_flags_allow_trailing_comma
allow trailing commas in objects and arrays.
Definition: json.h:192
@ das_jparse_flags_allow_equals_in_object
allow objects to use '=' instead of ':' between key/value pairs.
Definition: json.h:207
@ das_jparse_flags_allow_multi_line_strings
allow multi line string values
Definition: json.h:241
@ das_jparse_flags_allow_c_style_comments
allow c-style comments (// or /* *\/) to be ignored in the input JSON file.
Definition: json.h:217
@ das_jparse_flags_allow_json5
allow JSON5 to be parsed.
Definition: json.h:256
@ das_jparse_flags_allow_no_commas
allow that objects don't have to have comma separators between key/value pairs.
Definition: json.h:212
@ das_jparse_flags_allow_inf_and_nan
allow Infinity, -Infinity, NaN, -NaN
Definition: json.h:238
@ das_jparse_flags_allow_single_quoted_strings
allow strings to be 'single quoted'
Definition: json.h:226
@ das_jparse_flags_allow_unquoted_keys
allow unquoted keys for objects.
Definition: json.h:197
@ das_jparse_flags_allow_global_object
allow a global unbracketed object.
Definition: json.h:202
@ das_jparse_flags_allow_simplified_json
allow simplified JSON to be parsed.
Definition: json.h:246
@ das_jparse_flags_allow_leading_plus_sign
allow numbers like +123 to be parsed
Definition: json.h:232
@ das_jparse_flags_allow_leading_or_trailing_decimal_point
allow numbers like .0123 or 123.
Definition: json.h:235
@ das_jparse_flags_allow_location_information
record location information for each value.
Definition: json.h:223
@ das_jparse_flags_allow_hexadecimal_numbers
allow numbers to be hexadecimal
Definition: json.h:229
@ das_jparse_error_expected_comma_or_closing_bracket
expected either a comma or a closing '}' or ']' to close an object or array!
Definition: json.h:276
@ das_jparse_error_invalid_string
string was malformed!
Definition: json.h:297
@ das_jparse_error_unexpected_trailing_characters
the JSON input had unexpected trailing characters that weren't part of the JSON value
Definition: json.h:305
@ das_jparse_error_invalid_number_format
invalid number format!
Definition: json.h:288
@ das_jparse_error_expected_opening_quote
expected string to begin with '"'!
Definition: json.h:282
@ das_jparse_error_unknown
catch-all error for everything else that exploded (real bad chi!)
Definition: json.h:308
@ das_jparse_error_invalid_string_escape_sequence
invalid escaped sequence in string!
Definition: json.h:285
@ das_jparse_error_premature_end_of_buffer
reached end of buffer before object/array was complete!
Definition: json.h:294
@ das_jparse_error_allocator_failed
a call to malloc, or a user provider allocator, failed
Definition: json.h:300
@ das_jparse_error_invalid_value
invalid value!
Definition: json.h:291
@ das_jparse_error_expected_colon
colon separating name/value pair was missing!
Definition: json.h:279
@ das_jparse_error_none
no error occurred (huzzah!)
Definition: json.h:271
JSON Dom Element.
Definition: json.h:153
void * value
a pointer to either a das_json_str, das_json_num, das_json_dict, or das_json_ary.
Definition: json.h:158
size_t type
must be one of das_json_type_e.
Definition: json.h:162
an element of a JSON array
Definition: json.h:135
struct das_json_ary_el_s * next
the next array element (can be NULL if the last element in the array)
Definition: json.h:139
struct das_json_obj_s * value
the value of this element
Definition: json.h:137
a JSON array value
Definition: json.h:143
struct das_json_ary_el_s * start
a linked list of the elements in the array
Definition: json.h:145
size_t length
the number of elements in the array
Definition: json.h:147
An element of a JSON dictionary.
Definition: json.h:117
struct das_json_str_s * name
the name of this element
Definition: json.h:119
struct das_json_dict_el_s * next
the next object element (can be NULL if the last element in the object)
Definition: json.h:123
struct das_json_obj_s * value
the value of this element
Definition: json.h:121
a JSON dictionary payload
Definition: json.h:127
struct das_json_dict_el_s * start
a linked list of the elements in the object
Definition: json.h:129
size_t length
the number of elements in the object
Definition: json.h:131
A JSON number value.
Definition: json.h:109
size_t number_size
the size (in bytes) of the number
Definition: json.h:113
const char * number
ASCII string containing representation of the number.
Definition: json.h:111
error report from json_parse_ex()
Definition: json.h:312
size_t error_line_no
the line number for the error in the JSON input
Definition: json.h:322
size_t error_offset
the character offset for the error in the JSON input
Definition: json.h:319
size_t error_row_no
the row number for the error, in bytes
Definition: json.h:325
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:316
A JSON string value (extended)
Definition: json.h:94
size_t line_no
the line number for the value in the JSON input
Definition: json.h:102
size_t row_no
the row number for the value in the JSON input, in bytes
Definition: json.h:105
size_t offset
the character offset for the value in the JSON input
Definition: json.h:99
A JSON string value.
Definition: json.h:86
const char * string
utf-8 string
Definition: json.h:88
a JSON value (extended)
Definition: json.h:169
size_t line_no
the line number for the value in the JSON input
Definition: json.h:177
DasJdo value
the JSON value this extends.
Definition: json.h:171
size_t row_no
the row number for the value in the JSON input, in bytes
Definition: json.h:180
size_t offset
the character offset for the value in the JSON input
Definition: json.h:174