cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

json.h
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #ifndef CFENGINE_JSON_H
26 #define CFENGINE_JSON_H
27 
28 #include <writer.h>
29 #include <inttypes.h> // int64_t
30 
31 /**
32  @brief JSON data-structure.
33 
34  This is a JSON Document Object Model (DOM). Clients deal only with the opaque
35  JsonElement, which may be either a container or a primitive (client should
36  probably not deal much with primitive elements). A JSON container may be
37  either an object or an array. The JSON DOM currently supports copy semantics
38  for primitive values, but not for container types. In practice, this means
39  that clients always just free the parent element, but an element should just
40  have a single parent, or none.
41 
42  JSON primitives as JsonElement are currently not well supported.
43 
44  JSON DOM is currently built upon Sequence.
45  The JSON specification may be found at @link http://www.json.org @endlink.
46 
47  @see Sequence
48 */
49 
50 typedef enum
51 {
55 
56 typedef enum
57 {
61 
62 typedef enum
63 {
70 
71 typedef enum
72 {
80 } JsonType;
81 
82 typedef enum
83 {
85 
88 
99 
103 
112 
119 
122 
123 typedef struct JsonElement_ JsonElement;
124 
125 typedef struct
126 {
128  size_t index;
129 } JsonIterator;
130 
131 
132 //////////////////////////////////////////////////////////////////////////////
133 // String encoding (escaping)
134 //////////////////////////////////////////////////////////////////////////////
135 
136 char *JsonDecodeString(const char *escaped_string);
137 char *JsonEncodeString(const char *const unescaped_string);
138 
139 typedef struct _Slice
140 {
141  // Slice is used to represent a section of memory which may or may not
142  // contain NUL bytes. This is useful for storing the unescaped versions of
143  // JSON(5) strings (which may have NUL bytes).
144 
145  void *data; // Binary data here, not just ascii plain text
146  size_t size; // Allocated size in bytes (or shorter if you shrink later)
148 
149 char *Json5EscapeData(Slice unescaped_data);
150 
151 // Not implemented yet:
152 // Slice Json5UnescapeString(const char *escaped_string);
153 
154 //////////////////////////////////////////////////////////////////////////////
155 // Generic JSONElement functions
156 //////////////////////////////////////////////////////////////////////////////
157 
158 JsonElement *JsonCopy(const JsonElement *json);
159 int JsonCompare(const JsonElement *a, const JsonElement *b);
160 JsonElement *JsonMerge(const JsonElement *a, const JsonElement *b);
161 
162 /**
163  @brief Destroy a JSON element
164  @param element [in] The JSON element to destroy.
165  */
166 void JsonDestroy(JsonElement *element);
167 
168 /**
169  @brief Destroy a JSON element if needed
170  @param element [in] The JSON element to destroy.
171  @param allocated [in] Whether the element was allocated and needs to be
172  destroyed.
173  */
174 void JsonDestroyMaybe(JsonElement *element, bool allocated);
175 
176 /**
177  @brief Get the length of a JsonElement. This is the number of elements or
178  fields in an array or object respectively.
179  @param element [in] The JSON element.
180  */
181 size_t JsonLength(const JsonElement *element);
182 
184 JsonType JsonGetType(const JsonElement *element);
185 const char *JsonElementGetPropertyName(const JsonElement *element);
186 
187 const char *JsonGetPropertyAsString(const JsonElement *element);
188 
189 
190 //////////////////////////////////////////////////////////////////////////////
191 // JSON Primitives
192 //////////////////////////////////////////////////////////////////////////////
193 
196 const char *JsonPrimitiveGetAsString(const JsonElement *primitive);
197 char *JsonPrimitiveToString(const JsonElement *primitive);
198 bool JsonPrimitiveGetAsBool(const JsonElement *primitive);
199 long JsonPrimitiveGetAsInteger(const JsonElement *primitive);
200 int JsonPrimitiveGetAsInt64(const JsonElement *primitive, int64_t *value_out);
201 int64_t JsonPrimitiveGetAsInt64DefaultOnError(const JsonElement *primitive, int64_t default_return);
202 int64_t JsonPrimitiveGetAsInt64ExitOnError(const JsonElement *primitive);
203 double JsonPrimitiveGetAsReal(const JsonElement *primitive);
204 
205 JsonElement *JsonStringCreate(const char *value);
206 JsonElement *JsonIntegerCreate(int value);
207 JsonElement *JsonRealCreate(double value);
208 JsonElement *JsonBoolCreate(bool value);
210 
211 
212 //////////////////////////////////////////////////////////////////////////////
213 // JSON Containers (array or object)
214 //////////////////////////////////////////////////////////////////////////////
215 
216 void JsonContainerReverse(JsonElement *array);
217 
218 typedef int JsonComparator(
219  const JsonElement *, const JsonElement *, void *user_data);
220 
221 void JsonSort(
222  const JsonElement *container, JsonComparator *Compare, void *user_data);
223 JsonElement *JsonAt(const JsonElement *container, size_t index);
225  JsonElement *element, size_t num_indices, char **indices);
226 
228 
229 
230 //////////////////////////////////////////////////////////////////////////////
231 // JSON Object (dictionary)
232 //////////////////////////////////////////////////////////////////////////////
233 
234 /**
235  @brief Create a new JSON object
236  @param initial_capacity [in] The number of fields to preallocate space for.
237  @returns A pointer to the created object.
238  */
239 JsonElement *JsonObjectCreate(size_t initial_capacity);
240 
241 /**
242  @brief Append a string field to an object.
243  @param object [in] The JSON object parent.
244  @param key [in] the key of the field.
245  @param value [in] The value of the field.
246  */
248  JsonElement *object, const char *key, const char *value);
249 
250 /**
251  @brief Append an integer field to an object.
252  @param object [in] The JSON object parent.
253  @param key [in] the key of the field.
254  @param value [in] The value of the field.
255  */
256 void JsonObjectAppendInteger(JsonElement *object, const char *key, int value);
257 
258 /**
259  @brief Append an real number field to an object.
260  @param object [in] The JSON object parent.
261  @param key [in] the key of the field.
262  @param value [in] The value of the field.
263  */
264 void JsonObjectAppendReal(JsonElement *object, const char *key, double value);
265 
266 /**
267  @param object [in] The JSON object parent.
268  @param key [in] the key of the field.
269  @param value [in] The value of the field.
270  */
271 void JsonObjectAppendBool(JsonElement *object, const char *key, _Bool value);
272 
273 /**
274  @brief Append null field to an object.
275  @param object [in] The JSON object parent.
276  @param key [in] the key of the field.
277  */
278 void JsonObjectAppendNull(JsonElement *object, const char *key);
279 
280 /**
281  @brief Append an array field to an object.
282  @param object [in] The JSON object parent.
283  @param key [in] the key of the field.
284  @param value [in] The value of the field.
285  */
287  JsonElement *object, const char *key, JsonElement *array);
288 
289 /**
290  @brief Append an object field to an object.
291  @param object [in] The JSON object parent.
292  @param key [in] the key of the field.
293  @param value [in] The value of the field.
294  */
296  JsonElement *object, const char *key, JsonElement *childObject);
297 
298 /**
299  @brief Append any JSON element to an object.
300  @param object [in] The JSON object parent.
301  @param key [in] the key of the field.
302  @param element [in] The element to append
303 */
305  JsonElement *object, const char *key, JsonElement *element);
306 
307 /**
308  @brief Get the value of a field in an object, as a string.
309  @param object [in] The JSON object parent.
310  @param key [in] the key of the field.
311  @returns A pointer to the string value, or NULL if non-existent.
312  */
313 const char *JsonObjectGetAsString(const JsonElement *object, const char *key);
314 
315 /**
316  @brief Get the value of a field in an object, as an object.
317  @param object [in] The JSON object parent.
318  @param key [in] the key of the field.
319  @returns A pointer to the object value, or NULL if non-existent.
320  */
321 JsonElement *JsonObjectGetAsObject(JsonElement *object, const char *key);
322 
323 /**
324  @brief Get the value of a field in an object, as an array.
325  @param object [in] The JSON object parent.
326  @param key [in] the key of the field.
327  @returns A pointer to the array value, or NULL if non-existent.
328  */
329 JsonElement *JsonObjectGetAsArray(JsonElement *object, const char *key);
330 
331 JsonElement *JsonObjectGet(const JsonElement *object, const char *key);
332 
333 /**
334  @brief Remove key from the object
335  @param object containing the key property
336  @param property name to be removed
337  @return True if key was removed
338  */
339 bool JsonObjectRemoveKey(JsonElement *object, const char *key);
340 
341 /**
342  @brief Detach json element ownership from parent object;
343  @param object containing the key property
344  @param property name to be detached
345  */
346 JsonElement *JsonObjectDetachKey(JsonElement *object, const char *key);
347 
348 
349 //////////////////////////////////////////////////////////////////////////////
350 // JSON Array (list)
351 //////////////////////////////////////////////////////////////////////////////
352 
353 /**
354  @brief Create a new JSON array
355  @param initial_capacity [in] The number of fields to preallocate space for.
356  @returns The pointer to the created array.
357  */
358 JsonElement *JsonArrayCreate(size_t initialCapacity);
359 
360 /**
361  @brief Append a string to an array.
362  @param array [in] The JSON array parent.
363  @param value [in] The string value to append.
364  */
365 void JsonArrayAppendString(JsonElement *array, const char *value);
366 
367 void JsonArrayAppendBool(JsonElement *array, bool value);
368 
369 /**
370  @brief Append an integer to an array.
371  @param array [in] The JSON array parent.
372  @param value [in] The integer value to append.
373  */
374 void JsonArrayAppendInteger(JsonElement *array, int value);
375 
376 /**
377  @brief Append an real to an array.
378  @param array [in] The JSON array parent.
379  @param value [in] The real value to append.
380  */
381 void JsonArrayAppendReal(JsonElement *array, double value);
382 
383 /**
384  @brief Append null to an array.
385  @param array [in] The JSON array parent.
386  */
387 void JsonArrayAppendNull(JsonElement *array);
388 
389 /**
390  @brief Append an array to an array.
391  @param array [in] The JSON array parent.
392  @param child_array [in] The array value to append.
393  */
394 void JsonArrayAppendArray(JsonElement *array, JsonElement *child_array);
395 
396 /**
397  @brief Append an object to an array.
398  @param array [in] The JSON array parent.
399  @param object [in] The object value to append.
400  */
401 void JsonArrayAppendObject(JsonElement *array, JsonElement *object);
402 
403 /**
404  @brief Append any JSON element to an array.
405  @param array [in] The JSON array parent.
406  @param element [in] The object to append.
407  */
408 void JsonArrayAppendElement(JsonElement *array, JsonElement *element);
409 
410 /**
411  @brief Remove an inclusive range from a JSON array.
412  @see SequenceRemoveRange
413  @param array [in] The JSON array parent.
414  @param start [in] Index of the first element to remove.
415  @param end [in] Index of the last element to remove.
416  */
417 void JsonArrayRemoveRange(JsonElement *array, size_t start, size_t end);
418 
419 /**
420  @brief Get a string value from an array
421  @param array [in] The JSON array parent
422  @param index [in] Position of the value to get
423  @returns A pointer to the string value, or NULL if non-existent.
424  */
425 const char *JsonArrayGetAsString(JsonElement *array, size_t index);
426 
427 /**
428  @brief Get an object value from an array
429  @param array [in] The JSON array parent
430  @param index [in] Position of the value to get
431  @returns A pointer to the object value, or NULL if non-existent.
432  */
433 JsonElement *JsonArrayGetAsObject(JsonElement *array, size_t index);
434 
435 JsonElement *JsonArrayGet(const JsonElement *array, size_t index);
436 
437 /**
438  @brief Check if an array contains only primitives
439  @param array [in] The JSON array parent
440  @returns true if the array contains only primitives, false otherwise
441  */
443 
444 
445 //////////////////////////////////////////////////////////////////////////////
446 // JSON Iterator
447 //////////////////////////////////////////////////////////////////////////////
448 
449 JsonIterator JsonIteratorInit(const JsonElement *container);
450 const char *JsonIteratorNextKey(JsonIterator *iter);
453  JsonIterator *iter, JsonElementType type, bool skip_null);
454 const char *JsonIteratorCurrentKey(const JsonIterator *iter);
459 bool JsonIteratorHasMore(const JsonIterator *iter);
460 
461 
462 //////////////////////////////////////////////////////////////////////////////
463 // JSON Parsing
464 //////////////////////////////////////////////////////////////////////////////
465 
466 typedef JsonElement *JsonLookup(void *ctx, const char **data);
467 
468 /**
469  @brief Parse a string to create a JsonElement
470  @param data [in] Pointer to the string to parse
471  @param json_out Resulting JSON object
472  @returns See JsonParseError and JsonParseErrorToString
473  */
474 JsonParseError JsonParse(const char **data, JsonElement **json_out);
475 
476 /**
477  @brief Parse a string to create a JsonElement
478  @param lookup_data [in] Evaluation context for variable lookups
479  @param lookup_function [in] Callback function for variable lookups
480  @param data [in] Pointer to the string to parse
481  @param json_out Resulting JSON object
482  @returns See JsonParseError and JsonParseErrorToString
483 
484  The lookup_context type is void so we don't have to include
485  eval_context.h from libpromises into libutil
486  */
488  void *lookup_data,
489  JsonLookup *lookup_function,
490  const char **data,
491  JsonElement **json_out);
492 
493 /**
494  * @brief Convenience function to parse JSON or YAML from a file
495  * @param path Path to the file
496  * @param size_max Maximum size to read in memory
497  * @param json_out Resulting JSON object
498  * @param yaml_format Whether or not the file is in yaml format
499  * @return See JsonParseError and JsonParseErrorToString
500  */
502  const char *path,
503  size_t size_max,
504  JsonElement **json_out,
505  bool yaml_format);
506 
507 /**
508  * @brief Convenience function to parse JSON from a file.
509  * @param path Path to the file
510  * @param size_max Maximum size to read in memory
511  * @param json_out Resulting JSON object
512  * @return See JsonParseError and JsonParseErrorToString
513  */
515  const char *path, size_t size_max, JsonElement **json_out);
516 
517 const char *JsonParseErrorToString(JsonParseError error);
518 
519 
520 //////////////////////////////////////////////////////////////////////////////
521 // JSON Serialization (Write)
522 //////////////////////////////////////////////////////////////////////////////
523 
524 /**
525  @brief Pretty-print a JsonElement recursively into a Writer. If it's a
526  JsonObject, its children will be sorted to produce canonical JSON output, but
527  the object's contents are not modified so it's still a const.
528  @see Writer
529  @param writer [in] The Writer object to use as a buffer.
530  @param element [in] The JSON element to print.
531  @param indent_level [in] The nesting level with which the printing should be
532  done. This is mainly to allow the function to be called recursively. Clients
533  will normally want to set this to 0.
534  */
535 void JsonWrite(
536  Writer *writer, const JsonElement *element, size_t indent_level);
537 
538 void JsonWriteCompact(Writer *w, const JsonElement *element);
539 
540 #endif
JsonElement * JsonSelect(JsonElement *element, size_t num_indices, char **indices)
Definition: json.c:824
JsonElement * JsonMerge(const JsonElement *a, const JsonElement *b)
Definition: json.c:491
const char * JsonPrimitiveTypeToString(JsonPrimitiveType type)
Definition: json.c:77
JsonElement * JsonIteratorNextValue(JsonIterator *iter)
Definition: json.c:568
JsonElement * JsonObjectGetAsArray(JsonElement *object, const char *key)
Get the value of a field in an object, as an array.
Definition: json.c:1245
struct _Slice Slice
JsonElement * JsonLookup(void *ctx, const char **data)
Definition: json.h:466
const char * JsonObjectGetAsString(const JsonElement *object, const char *key)
Get the value of a field in an object, as a string.
Definition: json.c:1204
void JsonContainerReverse(JsonElement *array)
Definition: json.c:1426
JsonPrimitiveType JsonIteratorCurrentPrimitiveType(const JsonIterator *iter)
Definition: json.c:649
const char * JsonGetPropertyAsString(const JsonElement *element)
Definition: json.c:796
void JsonObjectAppendElement(JsonElement *object, const char *key, JsonElement *element)
Append any JSON element to an object.
Definition: json.c:1111
size_t JsonLength(const JsonElement *element)
Get the length of a JsonElement. This is the number of elements or fields in an array or object respe...
Definition: json.c:531
void JsonObjectAppendBool(JsonElement *object, const char *key, _Bool value)
Definition: json.c:1069
char * JsonEncodeString(const char *const unescaped_string)
Definition: json.c:926
JsonElement * JsonBoolCreate(bool value)
Definition: json.c:1468
char * JsonDecodeString(const char *escaped_string)
Definition: json.c:983
JsonPrimitiveType JsonGetPrimitiveType(const JsonElement *primitive)
Definition: json.c:693
JsonElement * JsonNullCreate()
Definition: json.c:1474
int JsonCompare(const JsonElement *a, const JsonElement *b)
Definition: json.c:358
void JsonArrayAppendInteger(JsonElement *array, int value)
Append an integer to an array.
Definition: json.c:1299
const char * JsonIteratorCurrentKey(const JsonIterator *iter)
Definition: json.c:618
JsonParseError
Definition: json.h:83
@ JSON_PARSE_ERROR_NUMBER_EXPONENT_DUPLICATE
Definition: json.h:94
@ JSON_PARSE_ERROR_NUMBER_BAD_SYMBOL
Definition: json.h:97
@ JSON_PARSE_ERROR_NUMBER_DIGIT_END
Definition: json.h:98
@ JSON_PARSE_ERROR_NUMBER_EXPONENT_FOLLOW_LEADING_ZERO
Definition: json.h:96
@ JSON_PARSE_ERROR_NO_LIBYAML
Definition: json.h:114
@ JSON_PARSE_ERROR_NO_DATA
Definition: json.h:117
@ JSON_PARSE_ERROR_TRUNCATED
Definition: json.h:118
@ JSON_PARSE_ERROR_STRING_NO_DOUBLEQUOTE_START
Definition: json.h:86
@ JSON_PARSE_ERROR_ARRAY_START
Definition: json.h:100
@ JSON_PARSE_ERROR_OBJECT_END
Definition: json.h:106
@ JSON_PARSE_ERROR_NUMBER_EXPONENT_NEGATIVE
Definition: json.h:89
@ JSON_PARSE_ERROR_STRING_NO_DOUBLEQUOTE_END
Definition: json.h:87
@ JSON_PARSE_ERROR_OBJECT_BAD_SYMBOL
Definition: json.h:104
@ JSON_PARSE_ERROR_NUMBER_EXPONENT_DIGIT
Definition: json.h:95
@ JSON_PARSE_ERROR_NUMBER_MULTIPLE_DOTS
Definition: json.h:93
@ JSON_PARSE_ERROR_OBJECT_OBJECT_LVAL
Definition: json.h:110
@ JSON_PARSE_ERROR_ARRAY_COMMA
Definition: json.h:102
@ JSON_PARSE_ERROR_INVALID_START
Definition: json.h:113
@ JSON_PARSE_ERROR_LIBYAML_FAILURE
Definition: json.h:115
@ JSON_PARSE_ERROR_NO_SUCH_FILE
Definition: json.h:116
@ JSON_PARSE_ERROR_NUMBER_NO_DIGIT
Definition: json.h:92
@ JSON_PARSE_ERROR_ARRAY_END
Definition: json.h:101
@ JSON_PARSE_ERROR_OBJECT_OPEN_LVAL
Definition: json.h:111
@ JSON_PARSE_ERROR_OBJECT_START
Definition: json.h:105
@ JSON_PARSE_ERROR_MAX
Definition: json.h:120
@ JSON_PARSE_OK
Definition: json.h:84
@ JSON_PARSE_ERROR_NUMBER_DUPLICATE_ZERO
Definition: json.h:91
@ JSON_PARSE_ERROR_NUMBER_EXPONENT_POSITIVE
Definition: json.h:90
@ JSON_PARSE_ERROR_OBJECT_COMMA
Definition: json.h:108
@ JSON_PARSE_ERROR_OBJECT_ARRAY_LVAL
Definition: json.h:109
@ JSON_PARSE_ERROR_OBJECT_COLON
Definition: json.h:107
int64_t JsonPrimitiveGetAsInt64DefaultOnError(const JsonElement *primitive, int64_t default_return)
Definition: json.c:769
JsonElement * JsonIteratorNextValueByType(JsonIterator *iter, JsonElementType type, bool skip_null)
Definition: json.c:583
JsonElement * JsonIntegerCreate(int value)
Definition: json.c:1447
void JsonObjectAppendInteger(JsonElement *object, const char *key, int value)
Append an integer field to an object.
Definition: json.c:1062
JsonElement * JsonObjectDetachKey(JsonElement *object, const char *key)
Detach json element ownership from parent object;.
Definition: json.c:1183
double JsonPrimitiveGetAsReal(const JsonElement *primitive)
Definition: json.c:787
JsonParseError JsonParseFile(const char *path, size_t size_max, JsonElement **json_out)
Convenience function to parse JSON from a file.
Definition: json.c:2727
void JsonDestroyMaybe(JsonElement *element, bool allocated)
Destroy a JSON element if needed.
Definition: json.c:422
JsonParseError JsonParse(const char **data, JsonElement **json_out)
Parse a string to create a JsonElement.
Definition: json.c:2648
void JsonObjectAppendObject(JsonElement *object, const char *key, JsonElement *childObject)
Append an object field to an object.
Definition: json.c:1099
JsonElementType JsonGetElementType(const JsonElement *element)
Definition: json.c:667
JsonElement * JsonArrayGetAsObject(JsonElement *array, size_t index)
Get an object value from an array.
Definition: json.c:1378
JsonType
Definition: json.h:72
@ JSON_TYPE_STRING
Definition: json.h:75
@ JSON_TYPE_BOOL
Definition: json.h:78
@ JSON_TYPE_INTEGER
Definition: json.h:76
@ JSON_TYPE_REAL
Definition: json.h:77
@ JSON_TYPE_OBJECT
Definition: json.h:73
@ JSON_TYPE_ARRAY
Definition: json.h:74
@ JSON_TYPE_NULL
Definition: json.h:79
void JsonArrayAppendArray(JsonElement *array, JsonElement *child_array)
Append an array to an array.
Definition: json.c:1317
void JsonSort(const JsonElement *container, JsonComparator *Compare, void *user_data)
Definition: json.c:803
JsonElement * JsonStringCreate(const char *value)
Definition: json.c:1439
const char * JsonIteratorNextKey(JsonIterator *iter)
Definition: json.c:557
JsonPrimitiveType
Definition: json.h:63
@ JSON_PRIMITIVE_TYPE_REAL
Definition: json.h:66
@ JSON_PRIMITIVE_TYPE_INTEGER
Definition: json.h:65
@ JSON_PRIMITIVE_TYPE_NULL
Definition: json.h:68
@ JSON_PRIMITIVE_TYPE_STRING
Definition: json.h:64
@ JSON_PRIMITIVE_TYPE_BOOL
Definition: json.h:67
void JsonArrayAppendBool(JsonElement *array, bool value)
Definition: json.c:1293
int JsonComparator(const JsonElement *, const JsonElement *, void *user_data)
Definition: json.h:218
const char * JsonParseErrorToString(JsonParseError error)
Definition: json.c:1905
void JsonObjectAppendArray(JsonElement *object, const char *key, JsonElement *array)
Append an array field to an object.
Definition: json.c:1089
void JsonWrite(Writer *writer, const JsonElement *element, size_t indent_level)
Pretty-print a JsonElement recursively into a Writer. If it's a JsonObject, its children will be sort...
Definition: json.c:1701
void JsonObjectAppendString(JsonElement *object, const char *key, const char *value)
Append a string field to an object.
Definition: json.c:1055
const char * JsonPrimitiveGetAsString(const JsonElement *primitive)
Definition: json.c:701
JsonType JsonGetType(const JsonElement *element)
Definition: json.c:674
void JsonDestroy(JsonElement *element)
Destroy a JSON element.
Definition: json.c:386
bool JsonPrimitiveGetAsBool(const JsonElement *primitive)
Definition: json.c:741
JsonElement * JsonObjectGetAsObject(JsonElement *object, const char *key)
Get the value of a field in an object, as an object.
Definition: json.c:1224
void JsonObjectAppendReal(JsonElement *object, const char *key, double value)
Append an real number field to an object.
Definition: json.c:1076
bool JsonObjectRemoveKey(JsonElement *object, const char *key)
Remove key from the object.
Definition: json.c:1167
JsonParseError JsonParseWithLookup(void *lookup_data, JsonLookup *lookup_function, const char **data, JsonElement **json_out)
Parse a string to create a JsonElement.
Definition: json.c:2653
void JsonArrayAppendReal(JsonElement *array, double value)
Append an real to an array.
Definition: json.c:1305
JsonElement * JsonObjectCreate(size_t initial_capacity)
Create a new JSON object.
Definition: json.c:880
void JsonArrayAppendString(JsonElement *array, const char *value)
Append a string to an array.
Definition: json.c:1287
JsonElement * JsonObjectGet(const JsonElement *object, const char *key)
Definition: json.c:1266
JsonElementType JsonIteratorCurrentElementType(const JsonIterator *iter)
Definition: json.c:630
bool JsonArrayContainsOnlyPrimitives(JsonElement *array)
Check if an array contains only primitives.
Definition: json.c:1406
char * Json5EscapeData(Slice unescaped_data)
Definition: json.c:1046
JsonElement * JsonCopy(const JsonElement *json)
Definition: json.c:235
long JsonPrimitiveGetAsInteger(const JsonElement *primitive)
Definition: json.c:750
void JsonObjectAppendNull(JsonElement *object, const char *key)
Append null field to an object.
Definition: json.c:1083
JsonIterator JsonIteratorInit(const JsonElement *container)
Definition: json.c:549
JsonElement * JsonArrayGet(const JsonElement *array, size_t index)
Definition: json.c:1397
void JsonArrayRemoveRange(JsonElement *array, size_t start, size_t end)
Remove an inclusive range from a JSON array.
Definition: json.c:1347
void JsonArrayAppendNull(JsonElement *array)
Append null to an array.
Definition: json.c:1311
JsonElementType
JSON data-structure.
Definition: json.h:51
@ JSON_ELEMENT_TYPE_PRIMITIVE
Definition: json.h:53
@ JSON_ELEMENT_TYPE_CONTAINER
Definition: json.h:52
int64_t JsonPrimitiveGetAsInt64ExitOnError(const JsonElement *primitive)
Definition: json.c:778
JsonElement * JsonRealCreate(double value)
Definition: json.c:1455
char * JsonPrimitiveToString(const JsonElement *primitive)
Definition: json.c:709
JsonParseError JsonParseAnyFile(const char *path, size_t size_max, JsonElement **json_out, bool yaml_format)
Convenience function to parse JSON or YAML from a file.
Definition: json.c:2691
const char * JsonElementGetPropertyName(const JsonElement *element)
Definition: json.c:111
JsonElement * JsonIteratorCurrentValue(const JsonIterator *iter)
Definition: json.c:603
bool JsonIteratorHasMore(const JsonIterator *iter)
Definition: json.c:660
JsonContainerType
Definition: json.h:57
@ JSON_CONTAINER_TYPE_ARRAY
Definition: json.h:59
@ JSON_CONTAINER_TYPE_OBJECT
Definition: json.h:58
int JsonPrimitiveGetAsInt64(const JsonElement *primitive, int64_t *value_out)
Definition: json.c:760
void JsonArrayAppendObject(JsonElement *array, JsonElement *object)
Append an object to an array.
Definition: json.c:1327
JsonElement * JsonAt(const JsonElement *container, size_t index)
Definition: json.c:815
void JsonArrayAppendElement(JsonElement *array, JsonElement *element)
Append any JSON element to an array.
Definition: json.c:1336
JsonElement * JsonArrayCreate(size_t initialCapacity)
Create a new JSON array.
Definition: json.c:1281
const char * JsonArrayGetAsString(JsonElement *array, size_t index)
Get a string value from an array.
Definition: json.c:1359
JsonContainerType JsonGetContainerType(const JsonElement *container)
Definition: json.c:685
JsonContainerType JsonIteratorCurrentContainerType(const JsonIterator *iter)
Definition: json.c:638
void JsonWriteCompact(Writer *w, const JsonElement *element)
Definition: json.c:1832
size_t index
Definition: json.h:128
const JsonElement * container
Definition: json.h:127
Definition: writer.c:45
Definition: json.h:140
void * data
Definition: json.h:145
size_t size
Definition: json.h:146