serde_kson Macro
serde_kson is a Rust macro that simplifies the process of building and managing JSON-like data structures. It allows you to dynamically create and manipulate nested JSON objects and arrays using a simple and intuitive syntax.
Features
- Easily create nested JSON structures.
- Supports both object and array syntax.
- Convenient access and update operations.
- Built on top of
serde_json for seamless integration.
Dependencies
To use serde_kson, make sure your Cargo.toml includes the following dependencies:
[dependencies]
serde_json = "1.0"
serde_kson = "0.2.1"
Example Usage
Here is how you can use the kson! macro to build and interact with a JSON-like structure in Rust:
use serde_kson::kson;
fn main() {
kson!(a);
kson!(a["name"] = "kinggunil");
kson!(a["age"] = 40);
kson!(a["phone"]["office"] = "010-28**-3440");
kson!(a["phone"]["home"] = "031-7**-2440");
kson!(a["country"][0] = "Korea");
kson!(a["country"][1] = "Canada");
kson!(a["like"]["number"] = 777);
kson!(a["like"]["numbers"][0]["a"] = 777777);
kson!(a["like"]["numbers"][1]["b"] = 121212);
println!("{:#?}",a);
println!("Name: {:?}", kson!(a["name"] : String)); println!("Age next year: {:?}", kson!(a["age"] : i64) + 1); println!("Office phone: {:?}", kson!(a["phone"]["office"] : String)); println!("Home phone: {:?}", kson!(a["phone"]["home"] : String)); println!("First country: {:?}", kson!(a["country"][0] : String)); println!("Second country: {:?}", kson!(a["country"][1] : String)); println!("number: {:?}", kson!(a["like"]["number"] : i64)); println!("number: {:?}", kson!(a["like"]["numbers"][0]["a"] : i64)); println!("number: {:?}", kson!(a["like"]["numbers"][1]["b"] : i64));
kson!(b);
kson!(b["any"] = 36); println!("any: {:?}", kson!(b["any"] : String)); println!("any: {:?}", kson!(b["any"] : &str));
kson!(b["bee"] = "210316"); println!("bee: {:?}", kson!(b["bee"] : i32)); println!("bee: {:?}", kson!(b["bee"] : i64)); println!("bee: {:?}", kson!(b["bee"] : f64));
kson!(c);
kson!(c[0] = "1"); let cc_0=kson!(c[0] : i64); kson!(c[1] = 3); let cc_1=kson!(c[1] : i64);
let dd=cc_0+cc_1; println!("dd: {:?}", dd); }
License
This project is licensed under the MIT License.