cjson-bindings
Safe Rust bindings for the cJSON library - a lightweight JSON parser in C.
Overview
cjson-bindings provides idiomatic, safe Rust wrappers around the cJSON C library, offering:
- Safe API: Memory-safe wrappers with automatic resource management (RAII)
- Type-safe operations: Strong typing with
Resulttypes for error handling - JSON Pointer support (RFC6901): Navigate JSON documents using JSON Pointer syntax
- JSON Patch support (RFC6902): Generate and apply JSON patches
- JSON Merge Patch support (RFC7386): Generate and apply merge patches
- no_std compatible: Suitable for embedded systems with built-in allocator and panic handler
Embedded & no_std Support
The library is designed for embedded systems and supports no_std environments:
- Global Allocator: Uses C's
malloc/freevia FFI - Default Panic Handler: Provides a simple infinite loop panic handler
- Custom Panic Handler: Use the
disable_panicfeature to provide your own
Cargo Features
std: Enables standard library support (required for tests)disable_panic: Disables both the default allocator and panic handler, allowing you to provide your own
Example with custom allocator and panic handler:
[]
= { = "0.5.0", = ["disable_panic"] }
Then provide your own in your application:
use ;
;
unsafe
static ALLOCATOR: MyAllocator = MyAllocator;
!
Features
Core JSON Operations
- Parse and print JSON with automatic memory management
- Create and manipulate JSON objects, arrays, strings, numbers, booleans, and null values
- Type checking and value retrieval with compile-time safety
- Deep cloning and comparison
Advanced Features
- JSON Pointer (RFC6901): Navigate JSON structures using paths like
/foo/bar/0 - JSON Patch (RFC6902): Generate and apply patch operations (add, remove, replace, move, copy, test)
- JSON Merge Patch (RFC7386): Simpler patch format for common use cases
- Sorting object keys alphabetically
Usage
Basic Example
use ;
JSON Pointer Example
use ;
let json = parse?;
// Navigate using JSON Pointer
let bob = get?;
println!; // "Bob"
JSON Patch Example
use ;
let mut original = parse?;
let patches = parse?;
// Apply patches
apply?;
println!;
// Output: {"name":"John","age":31,"city":"NYC"}
JSON Merge Patch Example
use ;
let mut target = parse?;
let patch = parse?;
// Apply merge patch
let result = apply?;
println!;
API Types
Main Types
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 errors
Utility Types
JsonPointer: JSON Pointer (RFC6901) operationsJsonPatch: JSON Patch (RFC6902) operationsJsonMergePatch: JSON Merge Patch (RFC7386) operationsJsonUtils: Additional utilities (e.g., sorting)
Error Handling
All operations that can fail return CJsonResult<T>, which is a type alias for Result<T, CJsonError>:
Memory Safety
cjson-bindings ensures memory safety through:
- RAII:
CJsonautomatically frees memory when dropped - No manual memory management: All allocations/deallocations are handled automatically
- Reference types:
CJsonRefprovides safe borrowing without ownership transfer - Clear ownership:
into_raw()for explicit ownership transfer when needed
Dependencies
This crate links against the cJSON C library. You need to have cJSON installed or provide it as part of your build process.
License
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.
cJSON 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.
Testing
The library includes comprehensive unit tests covering all major functionality. To run the tests:
Or use the provided alias:
See TESTS.md for detailed test documentation and coverage information.
Test Requirements
- cJSON library installed on your system (
libcjsonandlibcjson_utils) - Standard library support (enabled via the
stdfeature for testing)
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.