nx_server_plugin_sdk 1.0 Server Plugin SDK

json.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2
3 #pragma once
4
5 #include <stddef.h>
6
7 enum JsonType
8 {
9 jsonStringT,
10 jsonNumberT,
11 jsonObjectT,
12 jsonArrayT,
13 jsonTrueT,
14 jsonFalseT,
15 jsonNullT,
16 };
17
18 struct JsonVal
19 {
20 enum JsonType type;
21 union
22 {
23 char *string;
24 long double number;
25 struct {
26 char **keys;
27 struct JsonVal *values;
28 int len;
29 } object;
30 struct {
31 struct JsonVal *values;
32 int len;
33 } array;
34 } u;
35 };
36
37 struct JsonVal jsonParseString(const char *str, char *errorBuf, int errorBufSize);
38
39 /*creates empty (top-level) object*/
40 struct JsonVal jsonCreateObject();
41
42 /*check type functions*/
43 int JsonVal_isString(const struct JsonVal *val);
44 int JsonVal_isNumber(const struct JsonVal *val);
45 int JsonVal_isObject(const struct JsonVal *val);
46 int JsonVal_isArray(const struct JsonVal *val);
47 int JsonVal_isTrue(const struct JsonVal *val);
48 int JsonVal_isFalse(const struct JsonVal *val);
49 int JsonVal_isNull(const struct JsonVal *val);
50
51 /*add subvalues to existing values. return pointers to the newly created values.*/
52 struct JsonVal *JsonVal_objectAddString(struct JsonVal *val, const char *key, const char *value);
53 struct JsonVal *JsonVal_objectAddNumber(struct JsonVal *val, const char *key, long double number);
54 struct JsonVal *JsonVal_objectAddObject(struct JsonVal *val, const char *key);
55 struct JsonVal *JsonVal_objectAddArray(struct JsonVal *val, const char *key);
56 struct JsonVal *JsonVal_objectAddTrue(struct JsonVal *val, const char *key);
57 struct JsonVal *JsonVal_objectAddFalse(struct JsonVal *val, const char *key);
58 struct JsonVal *JsonVal_objectAddNull(struct JsonVal *val, const char *key);
59
60 struct JsonVal *JsonVal_arrayAddString(struct JsonVal *val, const char *value);
61 struct JsonVal *JsonVal_arrayAddNumber(struct JsonVal *val, long double number);
62 struct JsonVal *JsonVal_arrayAddObject(struct JsonVal *val);
63 struct JsonVal *JsonVal_arrayAddArray(struct JsonVal *val);
64 struct JsonVal *JsonVal_arrayAddTrue(struct JsonVal *val);
65 struct JsonVal *JsonVal_arrayAddFalse(struct JsonVal *val);
66 struct JsonVal *JsonVal_arrayAddNull(struct JsonVal *val);
67
68 /*access functions*/
69 int JsonVal_arrayLen(struct JsonVal *val);
70 struct JsonVal *JsonVal_arrayAt(struct JsonVal* val, int index);
71
72 struct JsonVal *JsonVal_getObjectValueByKey(const struct JsonVal *val, const char *key);
73
74 void JsonVal_forEachArrayElement(
75 const struct JsonVal *val,
76 void *ctx,
77 void (*action)(void *, const struct JsonVal *));
78
79 void JsonVal_forEachObjectElement(
80 const struct JsonVal *val,
81 void *ctx,
82 void (*action)(void *, const char *key, const struct JsonVal *));
83
84 /*write functions*/
85 int JsonVal_write(
86 const struct JsonVal *val,
87 /*User context. Will be passed to the writeFunc callback.*/
88 void *ctx,
89 /*user supplied write callback function.*/
90 int (*writeFunc)(void *ctx, void *buf, int len));
91
92 /*returns total bytes required*/
93 int JsonVal_writeString(const struct JsonVal *val, char *buf, int len);
94
95 void JsonVal_destroy(struct JsonVal *val);
JsonVal
Definition: json.h:18