summaryrefslogtreecommitdiff
path: root/src/dged/json.h
blob: 7f64e31e0d040cbdc9dd97bc9643cd4430cdb55c (plain)
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
#ifndef _JSON_H
#define _JSON_H

#include <stdbool.h>
#include <stdint.h>

#include "s8.h"

enum json_type {
  Json_Null = 0,
  Json_Array,
  Json_Object,
  Json_Number,
  Json_String,
  Json_Bool,
};

struct json_value {
  enum json_type type;
  union {
    struct s8 string;
    struct json_object *object;
    struct json_array *array;
    double number;
    bool boolean;
  } value;
  struct json_value *parent;

  const uint8_t *start;
  const uint8_t *end;
};

struct json_result {
  bool ok;
  union {
    const char *error;
    struct json_value document;
  } result;
};

/**
 * Parse a json document from a string.
 *
 * @returns Structure describing the result of the parse
 * operation. The member @ref ok, if true represents a
 * successful parse, with the result in @ref result.document.
 * If @ref ok is false, the parse operation has an error,
 * and @ref result.error contains a descriptive error message.
 */
struct json_result json_parse(const uint8_t *buf, uint64_t size);

/**
 * Destroy a json value, returning all memory
 * allocated for the structure.
 *
 * @param [in] value The json value to destroy.
 */
void json_destroy(struct json_value *value);

/**
 * Check if a JSON object is empty.
 *
 * @param [in] obj The JSON object to check if empty.
 *
 * @returns True if @ref obj is empty, false otherwise.
 */
bool json_empty(struct json_object *obj);

/**
 * Return the number of members in a JSON object.
 *
 * @param [in] obj The JSON object to get number of members for.
 *
 * @returns The number of members in @ref obj.
 */
uint64_t json_len(struct json_object *obj);

/**
 * Test if the JSON object contains the specified key.
 *
 * @param [in] obj The JSON object to look for @ref key in.
 * @param [in] key The key to search for.
 *
 * @returns True if @ref key exists in @ref obj, false otherwise.
 */
bool json_contains(struct json_object *obj, struct s8 key);

/**
 * Iterate all key-value pairs in a JSON object.
 *
 * @param [in] obj The JSON object to iterate.
 * @param [in] cb The callback to call for each kv-pair.
 * @param [in] userdata Pointer that is sent unmodified to @ref cb.
 */
void json_foreach(struct json_object *obj,
                  void (*cb)(struct s8, struct json_value *, void *),
                  void *userdata);

/**
 * Get a value from a JSON object.
 *
 * @param [in] obj The JSON object to get from.
 * @param [in] key The key of the value to get.
 *
 * @returns A pointer to the json value distinguished by @ref key,
 * if it exists, NULL otherwise.
 */
struct json_value *json_get(struct json_object *obj, struct s8 key);

/**
 * Set a value in a JSON object.
 *
 * @param [in] obj The JSON object to set in.
 * @param [in] key The key of the value to set.
 * @param [in] value The JSON value to set.
 */
void json_set(struct json_object *obj, struct s8 key, struct json_value val);

/**
 * Get the length of a JSON array.
 *
 * @param [in] arr The array to get the length of
 *
 * @returns The length of @ref arr.
 */
uint64_t json_array_len(struct json_array *arr);

/**
 * Iterate a JSON array.
 *
 * @param [in] arr The array to iterate.
 * @param [in] userdata Pointer to user-defined data that is passed
                        to the callback.
 * @param [in] cb The callback to invoke for each member in @ref arr.
 */
void json_array_foreach(struct json_array *arr, void *userdata,
                        void (*cb)(uint64_t, struct json_value *, void *));

/**
 * Get a member from a JSON array by index.
 *
 * @param [in] arr The array to get from.
 * @param [in] idx The index to get the value at.
 *
 * @returns A pointer to the value at @ref idx in @ref arr. If @ref idx
 * is outside the array length, this returns NULL.
 */
struct json_value *json_array_get(struct json_array *arr, uint64_t idx);

/**
 * Render a JSON value to a string.
 *
 * @param [in] val The json value to render to a string.
 *
 * @returns The JSON object rendered as a string.
 */
struct s8 json_value_to_string(const struct json_value *val);

struct s8 unescape_json_string(struct s8 input);
struct s8 escape_json_string(struct s8 input);

#endif