File: json.h

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (670 lines) | stat: -rw-r--r-- 23,433 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
  Copyright 2024 Northern.tech AS

  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

  To the extent this program is licensed as part of the Enterprise
  versions of CFEngine, the applicable Commercial Open Source License
  (COSL) may apply to this file if you as a licensee so wish it. See
  included file COSL.txt.
*/

#ifndef CFENGINE_JSON_H
#define CFENGINE_JSON_H

#include <writer.h>
#include <inttypes.h> // int64_t
#include <assert.h>

/**
  @brief JSON data-structure.

  This is a JSON Document Object Model (DOM). Clients deal only with the opaque
  JsonElement, which may be either a container or a primitive (client should
  probably not deal much with primitive elements). A JSON container may be
  either an object or an array. The JSON DOM currently supports copy semantics
  for primitive values, but not for container types. In practice, this means
  that clients always just free the parent element, but an element should just
  have a single parent, or none.

  JSON primitives as JsonElement are currently not well supported.

  JSON DOM is currently built upon Sequence.
  The JSON specification may be found at @link http://www.json.org @endlink.

  @see Sequence
*/

typedef enum
{
    JSON_ELEMENT_TYPE_CONTAINER = 1,
    JSON_ELEMENT_TYPE_PRIMITIVE = 2,
} JsonElementType;

typedef enum
{
    JSON_CONTAINER_TYPE_OBJECT = 3,
    JSON_CONTAINER_TYPE_ARRAY = 4,
} JsonContainerType;

typedef enum
{
    JSON_PRIMITIVE_TYPE_STRING = 5,
    JSON_PRIMITIVE_TYPE_INTEGER = 6,
    JSON_PRIMITIVE_TYPE_REAL = 7,
    JSON_PRIMITIVE_TYPE_BOOL = 8,
    JSON_PRIMITIVE_TYPE_NULL = 9,
} JsonPrimitiveType;

typedef enum
{
    JSON_TYPE_OBJECT = JSON_CONTAINER_TYPE_OBJECT,
    JSON_TYPE_ARRAY = JSON_CONTAINER_TYPE_ARRAY,
    JSON_TYPE_STRING = JSON_PRIMITIVE_TYPE_STRING,
    JSON_TYPE_INTEGER = JSON_PRIMITIVE_TYPE_INTEGER,
    JSON_TYPE_REAL = JSON_PRIMITIVE_TYPE_REAL,
    JSON_TYPE_BOOL = JSON_PRIMITIVE_TYPE_BOOL,
    JSON_TYPE_NULL = JSON_PRIMITIVE_TYPE_NULL,
} JsonType;

typedef enum
{
    JSON_PARSE_OK = 0,

    JSON_PARSE_ERROR_STRING_NO_DOUBLEQUOTE_START,
    JSON_PARSE_ERROR_STRING_NO_DOUBLEQUOTE_END,

    JSON_PARSE_ERROR_NUMBER_EXPONENT_NEGATIVE,
    JSON_PARSE_ERROR_NUMBER_EXPONENT_POSITIVE,
    JSON_PARSE_ERROR_NUMBER_DUPLICATE_ZERO,
    JSON_PARSE_ERROR_NUMBER_NO_DIGIT,
    JSON_PARSE_ERROR_NUMBER_MULTIPLE_DOTS,
    JSON_PARSE_ERROR_NUMBER_EXPONENT_DUPLICATE,
    JSON_PARSE_ERROR_NUMBER_EXPONENT_DIGIT,
    JSON_PARSE_ERROR_NUMBER_EXPONENT_FOLLOW_LEADING_ZERO,
    JSON_PARSE_ERROR_NUMBER_BAD_SYMBOL,
    JSON_PARSE_ERROR_NUMBER_DIGIT_END,

    JSON_PARSE_ERROR_ARRAY_START,
    JSON_PARSE_ERROR_ARRAY_END,
    JSON_PARSE_ERROR_ARRAY_COMMA,

    JSON_PARSE_ERROR_OBJECT_BAD_SYMBOL,
    JSON_PARSE_ERROR_OBJECT_START,
    JSON_PARSE_ERROR_OBJECT_END,
    JSON_PARSE_ERROR_OBJECT_COLON,
    JSON_PARSE_ERROR_OBJECT_COMMA,
    JSON_PARSE_ERROR_OBJECT_ARRAY_LVAL,
    JSON_PARSE_ERROR_OBJECT_OBJECT_LVAL,
    JSON_PARSE_ERROR_OBJECT_OPEN_LVAL,

    JSON_PARSE_ERROR_INVALID_START,
    JSON_PARSE_ERROR_INVALID_END,
    JSON_PARSE_ERROR_NO_LIBYAML,
    JSON_PARSE_ERROR_LIBYAML_FAILURE,
    JSON_PARSE_ERROR_NO_SUCH_FILE,
    JSON_PARSE_ERROR_NO_DATA,
    JSON_PARSE_ERROR_TRUNCATED,

    JSON_PARSE_ERROR_MAX
} JsonParseError;

typedef struct JsonElement_ JsonElement;

typedef struct
{
    const JsonElement *container;
    size_t index;
} JsonIterator;


//////////////////////////////////////////////////////////////////////////////
// String encoding (escaping)
//////////////////////////////////////////////////////////////////////////////

char *JsonDecodeString(const char *escaped_string);
char *JsonEncodeString(const char *const unescaped_string);

typedef struct _Slice
{
    // Slice is used to represent a section of memory which may or may not
    // contain NUL bytes. This is useful for storing the unescaped versions of
    // JSON(5) strings (which may have NUL bytes).

    void *data;  // Binary data here, not just ascii plain text
    size_t size; // Allocated size in bytes (or shorter if you shrink later)
} Slice;

char *Json5EscapeData(Slice unescaped_data);

// Not implemented yet:
// Slice Json5UnescapeString(const char *escaped_string);

//////////////////////////////////////////////////////////////////////////////
// Generic JSONElement functions
//////////////////////////////////////////////////////////////////////////////

JsonElement *JsonCopy(const JsonElement *json);

/**
 * @brief compare two JsonElement instances
 *
 * @param a JsonElement
 * @param b JsonElement
 * @return int like strcmp(), 0 is equal, non-zero is not equal
 */
int JsonCompare(const JsonElement *a, const JsonElement *b);

JsonElement *JsonMerge(const JsonElement *a, const JsonElement *b);

/**
 * @brief Recursively merge one object into another
 *
 * @param base Object to merge data into
 * @param extra Object to merge data from
 * @return Pointer to base object (convenient for nested function calls)
 *
 * @note The function should not return NULL
 * @warning Side effects: The base object is modified in place. If this is not
 *          the intention, consider using JsonObjectMergeDeep
 */
JsonElement *JsonObjectMergeDeepInplace(JsonElement *base, const JsonElement *extra);

/**
 * @brief Recursively merge two objects into a new object
 *
 * @param base Object to copy and merge data into
 * @param extra Object to merge data from
 * @return Pointer to merged object
 *
 * @note The function should not return NULL
 * @warning Performance penalty: A new object is created. If this is not the
 *          intension, consider using JsonObjectMergeDeepInplace
 */
static inline JsonElement *JsonObjectMergeDeep(const JsonElement *const base, const JsonElement *const extra)
{
    assert(base != NULL);
    /* This could do a little better job by constructing the JsonElement while
     * merging base and extra. Not sure if it's worth the extra complexity,
     * though. */
    JsonElement *const copy = JsonCopy(base);
    return JsonObjectMergeDeepInplace(copy, extra);
}

/**
  @brief Destroy a JSON element
  @param element [in] The JSON element to destroy.
  */
void JsonDestroy(JsonElement *element);

/**
  @brief Destroy a JSON element if needed
  @param element [in] The JSON element to destroy.
  @param allocated [in] Whether the element was allocated and needs to be
                        destroyed.
  */
void JsonDestroyMaybe(JsonElement *element, bool allocated);

/**
  @brief Get the length of a JsonElement. This is the number of elements or
  fields in an array or object respectively.
  @param element [in] The JSON element.
  */
size_t JsonLength(const JsonElement *element);

JsonElementType JsonGetElementType(const JsonElement *element);
JsonType JsonGetType(const JsonElement *element);
const char *JsonElementGetPropertyName(const JsonElement *element);

const char *JsonGetPropertyAsString(const JsonElement *element);

#define NULL_JSON(json) ((json == NULL) || (JsonGetType(json) == JSON_TYPE_NULL))
#define JSON_NOT_NULL(json) ((json != NULL) && (JsonGetType(json) != JSON_TYPE_NULL))

//////////////////////////////////////////////////////////////////////////////
// JSON Primitives
//////////////////////////////////////////////////////////////////////////////

const char *JsonContainerTypeToString(JsonContainerType type);
const char *JsonPrimitiveTypeToString(JsonPrimitiveType type);
JsonPrimitiveType JsonGetPrimitiveType(const JsonElement *primitive);
const char *JsonPrimitiveGetAsString(const JsonElement *primitive);
char *JsonPrimitiveToString(const JsonElement *primitive);
bool JsonPrimitiveGetAsBool(const JsonElement *primitive);
long JsonPrimitiveGetAsInteger(const JsonElement *primitive);
int JsonPrimitiveGetAsInt64(const JsonElement *primitive, int64_t *value_out);
int64_t JsonPrimitiveGetAsInt64DefaultOnError(const JsonElement *primitive, int64_t default_return);
int64_t JsonPrimitiveGetAsInt64ExitOnError(const JsonElement *primitive);
double JsonPrimitiveGetAsReal(const JsonElement *primitive);

JsonElement *JsonStringCreate(const char *value);
JsonElement *JsonIntegerCreate(int value);
JsonElement *JsonIntegerCreate64(int64_t value);
JsonElement *JsonRealCreate(double value);
JsonElement *JsonBoolCreate(bool value);
JsonElement *JsonNullCreate();


//////////////////////////////////////////////////////////////////////////////
// JSON Containers (array or object)
//////////////////////////////////////////////////////////////////////////////

void JsonContainerReverse(JsonElement *array);

typedef int JsonComparator(
    const JsonElement *, const JsonElement *, void *user_data);

void JsonSort(
    const JsonElement *container, JsonComparator *Compare, void *user_data);
JsonElement *JsonAt(const JsonElement *container, size_t index);
JsonElement *JsonSelect(
    JsonElement *element, size_t num_indices, char **indices);

JsonContainerType JsonGetContainerType(const JsonElement *container);


//////////////////////////////////////////////////////////////////////////////
// JSON Object (dictionary)
//////////////////////////////////////////////////////////////////////////////

/**
  @brief Create a new JSON object
  @param initial_capacity [in] The number of fields to preallocate space for.
  @returns A pointer to the created object.
  */
JsonElement *JsonObjectCreate(size_t initial_capacity);

/**
  @brief Append a string field to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param value [in] The value of the field.
  */
void JsonObjectAppendString(
    JsonElement *object, const char *key, const char *value);

/**
  @brief Append an integer field to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param value [in] The value of the field.
  */
void JsonObjectAppendInteger(JsonElement *object, const char *key, int value);

/**
  @brief Append a 64-bit integer field to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param value [in] The value of the field.
  */
void JsonObjectAppendInteger64(JsonElement *object, const char *key, int64_t value);

/**
  @brief Append an real number field to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param value [in] The value of the field.
  */
void JsonObjectAppendReal(JsonElement *object, const char *key, double value);

/**
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param value [in] The value of the field.
  */
void JsonObjectAppendBool(JsonElement *object, const char *key, _Bool value);

/**
  @brief Append null field to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  */
void JsonObjectAppendNull(JsonElement *object, const char *key);

/**
  @brief Append an array field to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param value [in] The value of the field.
  */
void JsonObjectAppendArray(
    JsonElement *object, const char *key, JsonElement *array);

/**
  @brief Append an object field to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param value [in] The value of the field.
  */
void JsonObjectAppendObject(
    JsonElement *object, const char *key, JsonElement *childObject);

/**
  @brief Append any JSON element to an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @param element [in] The element to append
*/
void JsonObjectAppendElement(
    JsonElement *object, const char *key, JsonElement *element);

/**
  @brief Get the value of a field in an object, as a string.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @returns A pointer to the string value, or NULL if non-existent.
  */
const char *JsonObjectGetAsString(const JsonElement *object, const char *key);


/**
  @brief Get the value of a field in an object, as a boolean.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @returns The boolean value for the key or false if key is not available
  */
bool JsonObjectGetAsBool(const JsonElement *const object, const char *key);

/**
  @brief Get the value of a field in an object, as an object.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @returns A pointer to the object value, or NULL if non-existent.
  */
JsonElement *JsonObjectGetAsObject(JsonElement *object, const char *key);

/**
  @brief Get the value of a field in an object, as an array.
  @param object [in] The JSON object parent.
  @param key [in] the key of the field.
  @returns A pointer to the array value, or NULL if non-existent.
  */
JsonElement *JsonObjectGetAsArray(JsonElement *object, const char *key);

JsonElement *JsonObjectGet(const JsonElement *object, const char *key);

/**
  @brief Remove key from the object
  @param object containing the key property
  @param property name to be removed
  @return True if key was removed
  */
bool JsonObjectRemoveKey(JsonElement *object, const char *key);

/**
  @brief Detach json element ownership from parent object;
  @param object containing the key property
  @param property name to be detached
  */
JsonElement *JsonObjectDetachKey(JsonElement *object, const char *key);


//////////////////////////////////////////////////////////////////////////////
// JSON Array (list)
//////////////////////////////////////////////////////////////////////////////

/**
  @brief Create a new JSON array
  @param initial_capacity [in] The number of fields to preallocate space for.
  @returns The pointer to the created array.
  */
JsonElement *JsonArrayCreate(size_t initialCapacity);

/**
  @brief Append a string to an array.
  @param array [in] The JSON array parent.
  @param value [in] The string value to append.
  */
void JsonArrayAppendString(JsonElement *array, const char *value);

void JsonArrayAppendBool(JsonElement *array, bool value);

/**
  @brief Append an integer to an array.
  @param array [in] The JSON array parent.
  @param value [in] The integer value to append.
  */
void JsonArrayAppendInteger(JsonElement *array, int value);

/**
  @brief Append an real to an array.
  @param array [in] The JSON array parent.
  @param value [in] The real value to append.
  */
void JsonArrayAppendReal(JsonElement *array, double value);

/**
  @brief Append null to an array.
  @param array [in] The JSON array parent.
  */
void JsonArrayAppendNull(JsonElement *array);

/**
  @brief Append an array to an array.
  @param array [in] The JSON array parent.
  @param child_array [in] The array value to append.
  */
void JsonArrayAppendArray(JsonElement *array, JsonElement *child_array);

/**
  @brief Append an object to an array.
  @param array [in] The JSON array parent.
  @param object [in] The object value to append.
  */
void JsonArrayAppendObject(JsonElement *array, JsonElement *object);

/**
  @brief Append any JSON element to an array.
  @param array [in] The JSON array parent.
  @param element [in] The object to append.
  */
void JsonArrayAppendElement(JsonElement *array, JsonElement *element);

/**
  * @brief Move elements from JSON array `b` to JSON array `a`.
  * @param a [in] The JSON array to move elements to.
  * @param b [in] The JSON array to move elements from.
  * @note JSON array `a` takes ownership of elements in JSON array `b`.
  *       JSON array `b` is freed from memory.
  */
void JsonArrayExtend(JsonElement *a, JsonElement *b);

/**
  @brief Remove an inclusive range from a JSON array.
  @see SequenceRemoveRange
  @param array [in] The JSON array parent.
  @param start [in] Index of the first element to remove.
  @param end [in] Index of the last element to remove.
  */
void JsonArrayRemoveRange(JsonElement *array, size_t start, size_t end);

/**
  @brief Get a string value from an array
  @param array [in] The JSON array parent
  @param index [in] Position of the value to get
  @returns A pointer to the string value, or NULL if non-existent.
  */
const char *JsonArrayGetAsString(JsonElement *array, size_t index);

/**
  @brief Get an object value from an array
  @param array [in] The JSON array parent
  @param index [in] Position of the value to get
  @returns A pointer to the object value, or NULL if non-existent.
  */
JsonElement *JsonArrayGetAsObject(JsonElement *array, size_t index);

JsonElement *JsonArrayGet(const JsonElement *array, size_t index);

/**
  @brief Check if an array contains only primitives
  @param array [in] The JSON array parent
  @returns true if the array contains only primitives, false otherwise
  */
bool JsonArrayContainsOnlyPrimitives(JsonElement *array);


//////////////////////////////////////////////////////////////////////////////
// JSON Iterator
//////////////////////////////////////////////////////////////////////////////

JsonIterator JsonIteratorInit(const JsonElement *container);
const char *JsonIteratorNextKey(JsonIterator *iter);
JsonElement *JsonIteratorNextValue(JsonIterator *iter);
JsonElement *JsonIteratorNextValueByType(
    JsonIterator *iter, JsonElementType type, bool skip_null);
const char *JsonIteratorCurrentKey(const JsonIterator *iter);
JsonElement *JsonIteratorCurrentValue(const JsonIterator *iter);
JsonElementType JsonIteratorCurrentElementType(const JsonIterator *iter);
JsonContainerType JsonIteratorCurrentContainerType(const JsonIterator *iter);
JsonPrimitiveType JsonIteratorCurrentPrimitiveType(const JsonIterator *iter);
bool JsonIteratorHasMore(const JsonIterator *iter);


/**
 * @param element current element being visited
 * @param data    arbitrary data passed to JsonWalk()
 * @return        whether the recursive walk should continue or not
 */
typedef bool JsonElementVisitor(JsonElement *element, void *data);

/**
 * Recursively walk over the JSON element.
 *
 * @param element           JSON element to start with
 * @param object_visitor    function to call on child objects
 * @param array_visitor     function to call on child arrays
 * @param primitive_visitor function to call on child primitives
 * @param data              arbitrary data passed to visitor functions
 * @return                  whether the recursive walk finished or was stopped (see #JsonElementVisitor)
 *
 * The function starts with the given JSON element #element and recursively
 * visits its child elements (if any), calling respective visitor functions on
 * each of the child elements.
 *
 * @note Each parent element is visited before its child elements.
 * @note Every element in the given JSON is visited unless one of the visitor functions returns
 *       #false.
 * @note Every element is visited at most once.
 * @warning Modifications of the visited elements must be done with extreme caution and good
 *          understanding of the implementation of the #JsonElement and #JsonIterator internals.
 */
bool JsonWalk(JsonElement *element,
              JsonElementVisitor object_visitor,
              JsonElementVisitor array_visitor,
              JsonElementVisitor primitive_visitor,
              void *data);

/**
 * Visitor that just stops the walk on any element.
 *
 * Can be used as one of the visitor functions for JsonWalk() to detect
 * undesired child elements.
 */
bool JsonErrorVisitor(JsonElement *element, void *data);

//////////////////////////////////////////////////////////////////////////////
// JSON Parsing
//////////////////////////////////////////////////////////////////////////////

typedef JsonElement *JsonLookup(void *ctx, const char **data);

/**
  @brief Parse a string to create a JsonElement
  @param data [in] Pointer to the string to parse
  @param json_out Resulting JSON object
  @returns See JsonParseError and JsonParseErrorToString
  */
JsonParseError JsonParse(const char **data, JsonElement **json_out);

/**
  @brief Parse the whole string to create a JsonElement
  @param data [in] Pointer to the string to parse
  @param json_out Resulting JSON object
  @note In contrast to JsonParse(), this function will return
        JSON_PARSE_ERROR_INVALID_END if there are trailing non-whitespace
        characters after termination of the JsonElement in the remainder of
        the string.
  @returns See JsonParseError and JsonParseErrorToString
  */
JsonParseError JsonParseAll(const char **data, JsonElement **json_out);

/**
  @brief Parse a string to create a JsonElement
  @param lookup_data [in] Evaluation context for variable lookups
  @param lookup_function [in] Callback function for variable lookups
  @param data [in] Pointer to the string to parse
  @param json_out Resulting JSON object
  @returns See JsonParseError and JsonParseErrorToString

  The lookup_context type is void so we don't have to include
  eval_context.h from libpromises into libutil
  */
JsonParseError JsonParseWithLookup(
    void *lookup_data,
    JsonLookup *lookup_function,
    const char **data,
    JsonElement **json_out);

/**
 * @brief Convenience function to parse JSON or YAML from a file
 * @param path Path to the file
 * @param size_max Maximum size to read in memory
 * @param json_out Resulting JSON object
 * @param yaml_format Whether or not the file is in yaml format
 * @return See JsonParseError and JsonParseErrorToString
 */
JsonParseError JsonParseAnyFile(
    const char *path,
    size_t size_max,
    JsonElement **json_out,
    bool yaml_format);

/**
 * @brief Convenience function to parse JSON from a file.
 * @param path Path to the file
 * @param size_max Maximum size to read in memory
 * @param json_out Resulting JSON object
 * @return See JsonParseError and JsonParseErrorToString
 */
JsonParseError JsonParseFile(
    const char *path, size_t size_max, JsonElement **json_out);

const char *JsonParseErrorToString(JsonParseError error);


//////////////////////////////////////////////////////////////////////////////
// JSON Serialization (Write)
//////////////////////////////////////////////////////////////////////////////

/**
  @brief Pretty-print a JsonElement recursively into a Writer.  If it's a
  JsonObject, its children will be sorted to produce canonical JSON output, but
  the object's contents are not modified so it's still a const.
  @see Writer
  @param writer [in] The Writer object to use as a buffer.
  @param element [in] The JSON element to print.
  @param indent_level [in] The nesting level with which the printing should be
  done. This is mainly to allow the function to be called recursively. Clients
  will normally want to set this to 0.
  */
void JsonWrite(
    Writer *writer, const JsonElement *element, size_t indent_level);

void JsonWriteCompact(Writer *w, const JsonElement *element);

void JsonEncodeStringWriter(const char *const unescaped_string, Writer *const writer);

#endif