cjson_binding/
cjson_ffi.rs1#![allow(non_upper_case_globals)]
21#![allow(non_camel_case_types)]
22#![allow(non_snake_case)]
23#![allow(dead_code)]
24#![allow(clashing_extern_declarations)]
25
26use core::ffi::{c_char, c_double, c_int, c_void};
27
28pub const cJSON_Invalid: c_int = 0;
33pub const cJSON_False: c_int = 1 << 0;
34pub const cJSON_True: c_int = 1 << 1;
35pub const cJSON_NULL: c_int = 1 << 2;
36pub const cJSON_Number: c_int = 1 << 3;
37pub const cJSON_String: c_int = 1 << 4;
38pub const cJSON_Array: c_int = 1 << 5;
39pub const cJSON_Object: c_int = 1 << 6;
40pub const cJSON_Raw: c_int = 1 << 7;
41
42pub const cJSON_IsReference: c_int = 256;
43pub const cJSON_StringIsConst: c_int = 512;
44
45pub const CJSON_VERSION_MAJOR: c_int = 1;
46pub const CJSON_VERSION_MINOR: c_int = 7;
47pub const CJSON_VERSION_PATCH: c_int = 19;
48
49pub const CJSON_NESTING_LIMIT: c_int = 1000;
50pub const CJSON_CIRCULAR_LIMIT: c_int = 10000;
51
52#[repr(C)]
58pub struct cJSON {
59 pub next: *mut cJSON,
60 pub prev: *mut cJSON,
61 pub child: *mut cJSON,
62 pub type_: c_int, pub valuestring: *mut c_char,
64 pub valueint: c_int,
65 pub valuedouble: c_double,
66 pub string: *mut c_char,
67}
68
69pub type cJSON_bool = c_int;
70
71#[repr(C)]
73pub struct cJSON_Hooks {
74 pub malloc_fn: Option<unsafe extern "C" fn(size: usize) -> *mut c_void>,
75 pub free_fn: Option<unsafe extern "C" fn(ptr: *mut c_void)>,
76}
77
78unsafe extern "C" {
83 pub fn cJSON_Version() -> *const c_char;
84
85 pub fn cJSON_InitHooks(hooks: *mut cJSON_Hooks);
86
87 pub fn cJSON_Parse(value: *const c_char) -> *mut cJSON;
88 pub fn cJSON_ParseWithLength(value: *const c_char, buffer_length: usize) -> *mut cJSON;
89 pub fn cJSON_ParseWithOpts(
90 value: *const c_char,
91 return_parse_end: *mut *const c_char,
92 require_null_terminated: cJSON_bool,
93 ) -> *mut cJSON;
94 pub fn cJSON_ParseWithLengthOpts(
95 value: *const c_char,
96 buffer_length: usize,
97 return_parse_end: *mut *const c_char,
98 require_null_terminated: cJSON_bool,
99 ) -> *mut cJSON;
100
101 pub fn cJSON_Print(item: *const cJSON) -> *mut c_char;
102 pub fn cJSON_PrintUnformatted(item: *const cJSON) -> *mut c_char;
103 pub fn cJSON_PrintBuffered(item: *const cJSON, prebuffer: c_int, fmt: cJSON_bool) -> *mut c_char;
104 pub fn cJSON_PrintPreallocated(
105 item: *mut cJSON,
106 buffer: *mut c_char,
107 length: c_int,
108 format: cJSON_bool,
109 ) -> cJSON_bool;
110
111 pub fn cJSON_Delete(item: *mut cJSON);
112
113 pub fn cJSON_GetArraySize(array: *const cJSON) -> c_int;
114 pub fn cJSON_GetArrayItem(array: *const cJSON, index: c_int) -> *mut cJSON;
115 pub fn cJSON_GetObjectItem(object: *const cJSON, string: *const c_char) -> *mut cJSON;
116 pub fn cJSON_GetObjectItemCaseSensitive(object: *const cJSON, string: *const c_char) -> *mut cJSON;
117 pub fn cJSON_HasObjectItem(object: *const cJSON, string: *const c_char) -> cJSON_bool;
118
119 pub fn cJSON_GetErrorPtr() -> *const c_char;
120
121 pub fn cJSON_GetStringValue(item: *const cJSON) -> *const c_char;
122 pub fn cJSON_GetNumberValue(item: *const cJSON) -> c_double;
123
124 pub fn cJSON_IsInvalid(item: *const cJSON) -> cJSON_bool;
125 pub fn cJSON_IsFalse(item: *const cJSON) -> cJSON_bool;
126 pub fn cJSON_IsTrue(item: *const cJSON) -> cJSON_bool;
127 pub fn cJSON_IsBool(item: *const cJSON) -> cJSON_bool;
128 pub fn cJSON_IsNull(item: *const cJSON) -> cJSON_bool;
129 pub fn cJSON_IsNumber(item: *const cJSON) -> cJSON_bool;
130 pub fn cJSON_IsString(item: *const cJSON) -> cJSON_bool;
131 pub fn cJSON_IsArray(item: *const cJSON) -> cJSON_bool;
132 pub fn cJSON_IsObject(item: *const cJSON) -> cJSON_bool;
133 pub fn cJSON_IsRaw(item: *const cJSON) -> cJSON_bool;
134
135 pub fn cJSON_CreateNull() -> *mut cJSON;
136 pub fn cJSON_CreateTrue() -> *mut cJSON;
137 pub fn cJSON_CreateFalse() -> *mut cJSON;
138 pub fn cJSON_CreateBool(boolean: cJSON_bool) -> *mut cJSON;
139 pub fn cJSON_CreateNumber(num: c_double) -> *mut cJSON;
140 pub fn cJSON_CreateString(string: *const c_char) -> *mut cJSON;
141 pub fn cJSON_CreateRaw(raw: *const c_char) -> *mut cJSON;
142 pub fn cJSON_CreateArray() -> *mut cJSON;
143 pub fn cJSON_CreateObject() -> *mut cJSON;
144
145 pub fn cJSON_CreateStringReference(string: *const c_char) -> *mut cJSON;
146 pub fn cJSON_CreateObjectReference(child: *const cJSON) -> *mut cJSON;
147 pub fn cJSON_CreateArrayReference(child: *const cJSON) -> *mut cJSON;
148
149 pub fn cJSON_CreateIntArray(numbers: *const c_int, count: c_int) -> *mut cJSON;
150 pub fn cJSON_CreateFloatArray(numbers: *const f32, count: c_int) -> *mut cJSON;
151 pub fn cJSON_CreateDoubleArray(numbers: *const c_double, count: c_int) -> *mut cJSON;
152 pub fn cJSON_CreateStringArray(strings: *const *const c_char, count: c_int) -> *mut cJSON;
153
154 pub fn cJSON_AddItemToArray(array: *mut cJSON, item: *mut cJSON) -> cJSON_bool;
155 pub fn cJSON_AddItemToObject(object: *mut cJSON, string: *const c_char, item: *mut cJSON) -> cJSON_bool;
156 pub fn cJSON_AddItemToObjectCS(object: *mut cJSON, string: *const c_char, item: *mut cJSON) -> cJSON_bool;
157 pub fn cJSON_AddItemReferenceToArray(array: *mut cJSON, item: *mut cJSON) -> cJSON_bool;
158 pub fn cJSON_AddItemReferenceToObject(object: *mut cJSON, string: *const c_char, item: *mut cJSON) -> cJSON_bool;
159
160 pub fn cJSON_DetachItemViaPointer(parent: *mut cJSON, item: *mut cJSON) -> *mut cJSON;
161 pub fn cJSON_DetachItemFromArray(array: *mut cJSON, which: c_int) -> *mut cJSON;
162 pub fn cJSON_DeleteItemFromArray(array: *mut cJSON, which: c_int);
163 pub fn cJSON_DetachItemFromObject(object: *mut cJSON, string: *const c_char) -> *mut cJSON;
164 pub fn cJSON_DetachItemFromObjectCaseSensitive(object: *mut cJSON, string: *const c_char) -> *mut cJSON;
165 pub fn cJSON_DeleteItemFromObject(object: *mut cJSON, string: *const c_char);
166 pub fn cJSON_DeleteItemFromObjectCaseSensitive(object: *mut cJSON, string: *const c_char);
167
168 pub fn cJSON_InsertItemInArray(array: *mut cJSON, which: c_int, newitem: *mut cJSON) -> cJSON_bool;
169 pub fn cJSON_ReplaceItemViaPointer(parent: *mut cJSON, item: *mut cJSON, replacement: *mut cJSON) -> cJSON_bool;
170 pub fn cJSON_ReplaceItemInArray(array: *mut cJSON, which: c_int, newitem: *mut cJSON) -> cJSON_bool;
171 pub fn cJSON_ReplaceItemInObject(object: *mut cJSON, string: *const c_char, newitem: *mut cJSON) -> cJSON_bool;
172 pub fn cJSON_ReplaceItemInObjectCaseSensitive(object: *mut cJSON, string: *const c_char, newitem: *mut cJSON) -> cJSON_bool;
173
174 pub fn cJSON_Duplicate(item: *const cJSON, recurse: cJSON_bool) -> *mut cJSON;
175 pub fn cJSON_Compare(a: *const cJSON, b: *const cJSON, case_sensitive: cJSON_bool) -> cJSON_bool;
176
177 pub fn cJSON_Minify(json: *mut c_char);
178
179 pub fn cJSON_AddNullToObject(object: *mut cJSON, name: *const c_char) -> *mut cJSON;
180 pub fn cJSON_AddTrueToObject(object: *mut cJSON, name: *const c_char) -> *mut cJSON;
181 pub fn cJSON_AddFalseToObject(object: *mut cJSON, name: *const c_char) -> *mut cJSON;
182 pub fn cJSON_AddBoolToObject(object: *mut cJSON, name: *const c_char, boolean: cJSON_bool) -> *mut cJSON;
183 pub fn cJSON_AddNumberToObject(object: *mut cJSON, name: *const c_char, number: c_double) -> *mut cJSON;
184 pub fn cJSON_AddStringToObject(object: *mut cJSON, name: *const c_char, string: *const c_char) -> *mut cJSON;
185 pub fn cJSON_AddRawToObject(object: *mut cJSON, name: *const c_char, raw: *const c_char) -> *mut cJSON;
186 pub fn cJSON_AddObjectToObject(object: *mut cJSON, name: *const c_char) -> *mut cJSON;
187 pub fn cJSON_AddArrayToObject(object: *mut cJSON, name: *const c_char) -> *mut cJSON;
188
189 pub fn cJSON_SetNumberHelper(object: *mut cJSON, number: c_double) -> c_double;
190 pub fn cJSON_SetValuestring(object: *mut cJSON, valuestring: *const c_char) -> *mut c_char;
191
192 pub fn cJSON_malloc(size: usize) -> *mut c_void;
193 pub fn cJSON_free(object: *mut c_void);
194}
195
196#[macro_export]
202macro_rules! cJSON_SetIntValue {
203 ($object:expr, $number:expr) => {
204 if let Some(obj) = unsafe { $object.as_mut() } {
205 obj.valueint = $number;
206 obj.valuedouble = $number as f64;
207 $number
208 } else {
209 $number
210 }
211 };
212}
213
214#[macro_export]
216macro_rules! cJSON_SetNumberValue {
217 ($object:expr, $number:expr) => {
218 if let Some(obj) = unsafe { $object.as_mut() } {
219 crate::cJSON_SetNumberHelper(obj, $number as f64)
220 } else {
221 $number as f64
222 }
223 };
224}
225
226#[macro_export]
228macro_rules! cJSON_SetBoolValue {
229 ($object:expr, $boolValue:expr) => {
230 if let Some(obj) = unsafe { $object.as_mut() } {
231 if (obj.type_ & (crate::cJSON_False | crate::cJSON_True)) != 0 {
232 obj.type_ = (obj.type_ & !(crate::cJSON_False | crate::cJSON_True)) | if $boolValue { crate::cJSON_True } else { crate::cJSON_False };
233 obj.type_
234 } else {
235 crate::cJSON_Invalid
236 }
237 } else {
238 crate::cJSON_Invalid
239 }
240 };
241}
242
243#[macro_export]
245macro_rules! cJSON_ArrayForEach {
246 ($element:ident, $array:expr) => {
247 let mut $element = if let Some(arr) = unsafe { $array.as_ref() } {
248 arr.child
249 } else {
250 core::ptr::null_mut()
251 };
252 while !($element).is_null() {
253 {
254 }
256 $element = unsafe { (*$element).next };
257 }
258 };
259}
260
261