| 1 | // Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module cjson |
| 5 | |
| 6 | // The `json.cjson` module provides more manual and low level access to the cJSON library. |
| 7 | // For example, using it, it is possible to build a tree in memory, that represents a JSON object, |
| 8 | // that has NULL leaves for example, which is currently not convenient/easy to do with just the |
| 9 | // high level `json.encode(value)` API that V has. |
| 10 | |
| 11 | #flag -I @VEXEROOT/thirdparty/cJSON |
| 12 | #flag @VEXEROOT/thirdparty/cJSON/cJSON.o |
| 13 | #include "cJSON.h" |
| 14 | |
| 15 | // As cJSON use `libm`, we need to link it. |
| 16 | $if windows { |
| 17 | $if tinyc { |
| 18 | #flag @VEXEROOT/thirdparty/tcc/lib/openlibm.o |
| 19 | } |
| 20 | } $else { |
| 21 | #flag -lm |
| 22 | } |
| 23 | |
| 24 | @[flag] |
| 25 | pub enum CJsonType { |
| 26 | t_false |
| 27 | t_true |
| 28 | t_null |
| 29 | t_number |
| 30 | t_string |
| 31 | t_array |
| 32 | t_object |
| 33 | t_raw |
| 34 | } |
| 35 | |
| 36 | @[typedef] |
| 37 | pub struct C.cJSON { |
| 38 | pub: |
| 39 | next &C.cJSON // next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem |
| 40 | prev &C.cJSON |
| 41 | child &C.cJSON // An array or object item will have a child pointer pointing to a chain of the items in the array/object |
| 42 | |
| 43 | type CJsonType // The type of the item, as above |
| 44 | |
| 45 | valueint int // writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead |
| 46 | valuedouble f64 // The item's number, if type==cJSON_Number |
| 47 | valuestring &char // The item's string, if type==cJSON_String and type == cJSON_Raw |
| 48 | // @string &char // The item's name string, if this item is the child of, or is in the list of subitems of an object |
| 49 | // TODO: `@string &char` from above does not work. It should be fixed, at least inside `struct C.`. |
| 50 | } |
| 51 | |
| 52 | pub type Node = C.cJSON |
| 53 | |
| 54 | fn C.cJSON_Version() &char |
| 55 | |
| 56 | fn C.cJSON_Parse(const_value &char) &C.cJSON |
| 57 | |
| 58 | fn C.cJSON_CreateObject() &C.cJSON |
| 59 | |
| 60 | fn C.cJSON_CreateArray() &C.cJSON |
| 61 | |
| 62 | fn C.cJSON_CreateBool(bool) &C.cJSON |
| 63 | |
| 64 | fn C.cJSON_CreateTrue() &C.cJSON |
| 65 | |
| 66 | fn C.cJSON_CreateFalse() &C.cJSON |
| 67 | |
| 68 | fn C.cJSON_CreateNull() &C.cJSON |
| 69 | |
| 70 | fn C.cJSON_CreateNumber(f64) &C.cJSON |
| 71 | |
| 72 | fn C.cJSON_CreateString(const_s &char) &C.cJSON |
| 73 | |
| 74 | fn C.cJSON_CreateRaw(const_s &char) &C.cJSON |
| 75 | |
| 76 | fn C.cJSON_IsInvalid(object &C.cJSON) bool |
| 77 | |
| 78 | fn C.cJSON_IsFalse(object &C.cJSON) bool |
| 79 | |
| 80 | fn C.cJSON_IsTrue(object &C.cJSON) bool |
| 81 | |
| 82 | fn C.cJSON_IsBool(object &C.cJSON) bool |
| 83 | |
| 84 | fn C.cJSON_IsNull(object &C.cJSON) bool |
| 85 | |
| 86 | fn C.cJSON_IsNumber(object &C.cJSON) bool |
| 87 | |
| 88 | fn C.cJSON_IsString(object &C.cJSON) bool |
| 89 | |
| 90 | fn C.cJSON_IsArray(object &C.cJSON) bool |
| 91 | |
| 92 | fn C.cJSON_IsObject(object &C.cJSON) bool |
| 93 | |
| 94 | fn C.cJSON_IsRaw(object &C.cJSON) bool |
| 95 | |
| 96 | fn C.cJSON_AddItemToObject(object &C.cJSON, const_key &char, item &C.cJSON) |
| 97 | |
| 98 | fn C.cJSON_AddItemToArray(object &C.cJSON, item &C.cJSON) |
| 99 | |
| 100 | fn C.cJSON_Delete(object &C.cJSON) |
| 101 | |
| 102 | fn C.cJSON_Print(object &C.cJSON) &char |
| 103 | |
| 104 | fn C.cJSON_PrintUnformatted(object &C.cJSON) &char |
| 105 | |
| 106 | fn C.cJSON_free(voidptr) |
| 107 | |
| 108 | // |
| 109 | |
| 110 | // version returns the version of cJSON as a string. |
| 111 | pub fn version() string { |
| 112 | return unsafe { tos3(&char(C.cJSON_Version())) } |
| 113 | } |
| 114 | |
| 115 | // |
| 116 | |
| 117 | // create_object creates a new JSON object/map item. Use .add_item_to_object(key, value) calls, to add other items to it later. |
| 118 | pub fn create_object() &Node { |
| 119 | return C.cJSON_CreateObject() |
| 120 | } |
| 121 | |
| 122 | // create_array creates a new JSON array item. Use .add_item_to_array(value) calls, to add items to it later. |
| 123 | pub fn create_array() &Node { |
| 124 | return C.cJSON_CreateArray() |
| 125 | } |
| 126 | |
| 127 | // create_string creates a new JSON string item. |
| 128 | pub fn create_string(val string) &Node { |
| 129 | return C.cJSON_CreateString(&char(val.str)) |
| 130 | } |
| 131 | |
| 132 | // create_raw creates a new JSON RAW string item. |
| 133 | pub fn create_raw(const_val string) &Node { |
| 134 | return C.cJSON_CreateRaw(&char(const_val.str)) |
| 135 | } |
| 136 | |
| 137 | // create_number creates a new JSON number item. |
| 138 | pub fn create_number(val f64) &Node { |
| 139 | return C.cJSON_CreateNumber(val) |
| 140 | } |
| 141 | |
| 142 | // create_bool creates a new JSON boolean item. |
| 143 | pub fn create_bool(val bool) &Node { |
| 144 | return C.cJSON_CreateBool(val) |
| 145 | } |
| 146 | |
| 147 | // create_true creates a new JSON boolean item, with value `true`. |
| 148 | pub fn create_true() &Node { |
| 149 | return C.cJSON_CreateTrue() |
| 150 | } |
| 151 | |
| 152 | // create_false creates a new JSON boolean item, with value `false`. |
| 153 | pub fn create_false() &Node { |
| 154 | return C.cJSON_CreateFalse() |
| 155 | } |
| 156 | |
| 157 | // create_null creates a new JSON NULL item, with the value `null`. It symbolises a missing value for a given key in an object. |
| 158 | pub fn create_null() &Node { |
| 159 | return C.cJSON_CreateNull() |
| 160 | } |
| 161 | |
| 162 | // |
| 163 | |
| 164 | // delete removes the given node from memory. |
| 165 | // NB: DO NOT USE that node, after you have called `unsafe { delete(node) }` ! |
| 166 | @[unsafe] |
| 167 | pub fn delete(node &Node) { |
| 168 | C.cJSON_Delete(node) |
| 169 | } |
| 170 | |
| 171 | // |
| 172 | |
| 173 | // add_item_to_array adds the given item to the object, under the given `key`. |
| 174 | pub fn (mut obj Node) add_item_to_object(key string, item &Node) { |
| 175 | C.cJSON_AddItemToObject(obj, &char(key.str), item) |
| 176 | } |
| 177 | |
| 178 | // add_item_to_array append the given item to the object. |
| 179 | pub fn (mut obj Node) add_item_to_array(item &Node) { |
| 180 | C.cJSON_AddItemToArray(obj, item) |
| 181 | } |
| 182 | |
| 183 | // |
| 184 | |
| 185 | // print serialises the node to a string, formatting its structure, so the resulting string is more prettier/human readable. |
| 186 | pub fn (mut obj Node) print() string { |
| 187 | mut s := C.cJSON_Print(obj) |
| 188 | return unsafe { tos3(s) } |
| 189 | } |
| 190 | |
| 191 | // print serialises the node to a string, without formatting its structure, so the resulting string is shorter/cheaper to transmit. |
| 192 | pub fn (mut obj Node) print_unformatted() string { |
| 193 | mut s := C.cJSON_PrintUnformatted(obj) |
| 194 | ret := unsafe { tos_clone(&u8(s)) } |
| 195 | C.cJSON_free(s) |
| 196 | return ret |
| 197 | } |
| 198 | |
| 199 | // str returns the unformatted serialisation to string of the given Node. |
| 200 | pub fn (mut obj Node) str() string { |
| 201 | return obj.print_unformatted() |
| 202 | } |
| 203 | |