sjson.rs
Set JSON values quickly in Rust.
inspired by https://github.com/tidwall/sjson
Getting Started
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use ;
API Reference
Functions
set(json: &str, path: &str, value: &str) -> Result<String, SjsonError>
Sets a string value for the specified path.
set_bool(json: &str, path: &str, value: bool, opts: Option<&Options>) -> Result<String, SjsonError>
Sets a boolean value for the specified path.
set_int<T: std::fmt::Display>(json: &str, path: &str, value: T, opts: Option<&Options>) -> Result<String, SjsonError>
Sets an integer value for the specified path.
set_float<T: std::fmt::Display>(json: &str, path: &str, value: T, opts: Option<&Options>) -> Result<String, SjsonError>
Sets a float value for the specified path.
set_value<T: serde::Serialize>(json: &str, path: &str, value: &T, opts: Option<&Options>) -> Result<String, SjsonError>
Sets any serializable value for the specified path.
set_raw(json: &str, path: &str, value: &str) -> Result<String, SjsonError>
Sets a raw JSON value for the specified path.
delete(json: &str, path: &str) -> Result<String, SjsonError>
Deletes a value from JSON for the specified path.
Options
use Options;
let mut opts = default;
opts.optimistic = true; // Hint that value likely exists
Optimistic Mode
When optimistic is set to true, sjson will attempt to perform a fast string-based replacement instead of full JSON parsing. This can provide significant performance improvements (up to 10x faster) for simple operations where the path exists and the value can be found directly in the JSON string.
use ;
let json = r#"{"name":"Tom","age":37}"#;
let mut opts = default;
opts.optimistic = true;
// This will use fast string replacement
let result = set_options.unwrap;
Path Syntax
A path is a series of keys separated by a dot. For example:
"name.last"→"Anderson""age"→37"children.1"→"Alex""friends.0.first"→"James"
Error Handling
use ;
match set
Performance
sjson.rs is designed for high performance JSON manipulation. It uses the serde_json library for fast JSON parsing and provides efficient string manipulation for setting values.
License
MIT License - see LICENSE file for details.