| Crates.io | cjson-bindings |
| lib.rs | cjson-bindings |
| version | 0.5.0 |
| created_at | 2025-12-06 11:07:55.80149+00 |
| updated_at | 2025-12-06 11:07:55.80149+00 |
| description | Safe Rust bindings for the cJSON library - a lightweight JSON parser in C with support for JSON Pointer (RFC6901), JSON Patch (RFC6902), and JSON Merge Patch (RFC7386) |
| homepage | https://github.com/passy1977/cjson-bindings |
| repository | https://github.com/passy1977/cjson-bindings |
| max_upload_size | |
| id | 1969990 |
| size | 112,282 |
Safe Rust bindings for the cJSON library - a lightweight JSON parser in C.
cjson-bindings provides idiomatic, safe Rust wrappers around the cJSON C library, offering:
Result types for error handlingThe library is designed for embedded systems and supports no_std environments:
malloc/free via FFIdisable_panic feature to provide your ownstd: Enables standard library support (required for tests)disable_panic: Disables both the default allocator and panic handler, allowing you to provide your ownExample with custom allocator and panic handler:
[dependencies]
cjson-bindings = { version = "0.5.0", features = ["disable_panic"] }
Then provide your own in your application:
use core::alloc::{GlobalAlloc, Layout};
struct MyAllocator;
unsafe impl GlobalAlloc for MyAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// Your custom allocation
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
// Your custom deallocation
}
}
#[global_allocator]
static ALLOCATOR: MyAllocator = MyAllocator;
#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
// Your custom panic handling
loop {}
}
/foo/bar/0use cjson_rs::{CJson, CJsonResult};
fn main() -> CJsonResult<()> {
// Parse JSON
let json = CJson::parse(r#"{"name": "John", "age": 30}"#)?;
// Access values
let name = json.get_object_item("name")?;
println!("Name: {}", name.get_string_value()?);
// Create new JSON
let mut obj = CJson::create_object()?;
obj.add_string_to_object("city", "New York")?;
obj.add_number_to_object("population", 8_000_000.0)?;
// Print JSON
println!("{}", obj.print()?);
Ok(())
}
use cjson_rs::{CJson, JsonPointer};
let json = CJson::parse(r#"{
"users": [
{"name": "Alice", "age": 25},
{"name": "Bob", "age": 30}
]
}"#)?;
// Navigate using JSON Pointer
let bob = JsonPointer::get(&json, "/users/1/name")?;
println!("User: {}", bob.get_string_value()?); // "Bob"
use cjson_rs::{CJson, JsonPatch};
let mut original = CJson::parse(r#"{"name": "John", "age": 30}"#)?;
let patches = CJson::parse(r#"[
{"op": "replace", "path": "/age", "value": 31},
{"op": "add", "path": "/city", "value": "NYC"}
]"#)?;
// Apply patches
JsonPatch::apply(&mut original, &patches)?;
println!("{}", original.print()?);
// Output: {"name":"John","age":31,"city":"NYC"}
use cjson_rs::{CJson, JsonMergePatch};
let mut target = CJson::parse(r#"{"name": "John", "age": 30}"#)?;
let patch = CJson::parse(r#"{"age": 31, "city": "NYC"}"#)?;
// Apply merge patch
let result = JsonMergePatch::apply(&mut target, &patch)?;
println!("{}", result.print()?);
CJson: Owned JSON value with automatic memory managementCJsonRef: Borrowed reference to a JSON value (non-owning)CJsonResult<T>: Result type for operations that can failCJsonError: Error enumeration for all possible errorsJsonPointer: JSON Pointer (RFC6901) operationsJsonPatch: JSON Patch (RFC6902) operationsJsonMergePatch: JSON Merge Patch (RFC7386) operationsJsonUtils: Additional utilities (e.g., sorting)All operations that can fail return CJsonResult<T>, which is a type alias for Result<T, CJsonError>:
pub enum CJsonError {
ParseError,
NullPointer,
InvalidUtf8,
NotFound,
TypeError,
AllocationError,
InvalidOperation,
}
cjson-bindings ensures memory safety through:
CJson automatically frees memory when droppedCJsonRef provides safe borrowing without ownership transferinto_raw() for explicit ownership transfer when neededThis crate links against the cJSON C library. You need to have cJSON installed or provide it as part of your build process.
This Rust wrapper is licensed under the GNU General Public License v3.0 (GPL-3.0).
The underlying cJSON library is licensed under the MIT License.
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
See LICENSE for the full GPL-3.0 license text and cJSON's license for details.
The library includes comprehensive unit tests covering all major functionality. To run the tests:
cargo test --features std
Or use the provided alias:
cargo test-std
See TESTS.md for detailed test documentation and coverage information.
libcjson and libcjson_utils)std feature for testing)Contributions are welcome! Please feel free to submit a Pull Request.