Skip to main content

cjson_binding/
cjson_ffi.rs

1/***************************************************************************
2 *
3 * cJSON FFI BINDING FOR RUST
4 * Copyright (C) 2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***************************************************************************/
19
20#![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
28// ========================
29// COSTANTI
30// ========================
31
32pub 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// ========================
53// TYPES
54// ========================
55
56// Struct cJSON
57#[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, // "type" it is a reserved keyword in Rust
63    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// cJSON_Hooks struct
72#[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
78// ========================
79// FUNCTIONS C (declare extern "C")
80// ========================
81
82unsafe 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// ========================
197// MACRO (translated from C to Rust)
198// ========================
199
200// cJSON_SetIntValue
201#[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// cJSON_SetNumberValue
215#[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// cJSON_SetBoolValue
227#[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// cJSON_ArrayForEach
244#[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                // body of the loop goes here
255            }
256            $element = unsafe { (*$element).next };
257        }
258    };
259}
260
261// // ========================
262// // FUNZIONI DI UTILITÀ (opzionali)
263// // ========================
264
265// // Wrapper sicuro per cJSON_Delete (evita double free)
266// pub unsafe fn cJSON_Delete_safe(item: *mut cJSON) {
267//     if !item.is_null() {
268//         unsafe { cJSON_Delete(item); }
269//     }
270// }
271
272// // Wrapper per cJSON_Parse con gestione errori — usa alloc::string::String
273// pub unsafe fn cJSON_Parse_safe(json_str: &str) -> Option<*mut cJSON> {
274//     let c_str = CString::new(json_str).ok()?;
275//     let ptr = unsafe { cJSON_Parse(c_str.as_ptr()) };
276//     if ptr.is_null() {
277//         None
278//     } else {
279//         Some(ptr)
280//     }
281// }
282
283// // Wrapper per cJSON_Print con deallocazione automatica — ritorna alloc::string::String
284// pub unsafe fn cJSON_Print_safe(item: *mut cJSON) -> Option<String> {
285//     if item.is_null() {
286//         return None;
287//     }
288//     let c_str = unsafe { cJSON_Print(item) };
289//     if c_str.is_null() {
290//         return None;
291//     }
292//     let rust_str = unsafe { CStr::from_ptr(c_str).to_string_lossy().into_owned() };
293//     unsafe { cJSON_free(c_str as *mut c_void); }
294//     Some(rust_str)
295// }
296
297// // Wrapper per cJSON_GetStringValue — ritorna String
298// pub unsafe fn cJSON_GetStringValue_safe(item: *const cJSON) -> Option<String> {
299//     if item.is_null() {
300//         return None;
301//     }
302//     let c_str = unsafe { cJSON_GetStringValue(item) };
303//     if c_str.is_null() {
304//         return None;
305//     }
306//     Some(unsafe { CStr::from_ptr(c_str).to_string_lossy().into_owned() })
307// }
308
309// // Wrapper per cJSON_GetNumberValue — ritorna f64
310// pub unsafe fn cJSON_GetNumberValue_safe(item: *const cJSON) -> Option<f64> {
311//     if item.is_null() {
312//         return None;
313//     }
314//     Some(unsafe { cJSON_GetNumberValue(item) })
315// }