rust_json_derive 0.1.2

ToJson and FromJson derive for rust-json
Documentation
  • Coverage
  • 0%
    0 out of 3 items documented0 out of 2 items with examples
  • Size
  • Source code size: 17.96 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 312.28 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 4s Average build duration of successful builds.
  • all releases: 4s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Puellaquae

rust_json_derive

Latest Version

Derive the ToJson and FromJson for rust_json.

#[derive(ToJson)]

use rust_json::ToJson;
use rust_json_derive::ToJson;

#[derive(ToJson)]
struct Simple {
    n: f64,
    #[rename = "sim.b"]
    b: bool,
}

#[derive(ToJson)]
struct Nest {
    a: Vec<f64>,
    s: Simple,
}

#[derive(ToJson)]
enum Enum {
    #[rename = "u"]
    Unit,
    One(i32),
    Two(i32, i32),
    Cmpx { 
        a: i32, 
        b: i32, 
        #[rename = "z"]
        c: i32,
    },
}

fn main() {
    let s = Simple { n: 12.3, b: true };
    println!("{}", s.to_json());

    let n = Nest {
        a: vec![1.2, 2.3],
        s: s,
    };
    println!("{}", n.to_json());

    let u = E::Unit;
    let o = E::One(1);
    let t = E::Two(1, 2);
    let c = E::Cmpx { a: 1, b: 2, c: 3 };
    println!("{}", u.to_json());
    println!("{}", o.to_json());
    println!("{}", t.to_json());
    println!("{}", c.to_json());
}

#[derive(FromJson)]

use rust_json::json_parse;
use rust_json_derive::FromJson;

#[derive(Debug, FromJson)]
struct Simple {
    n: f64,
    #[rename = "sim.b"]
    b: bool,
}

#[derive(Debug, FromJson)]
struct Nest {
    a: Vec<f64>,
    s: Simple,
}

#[derive(Debug, FromJson)]
enum Enum {
    #[rename = "u"]
    Unit,
    One(i32),
    Two(i32, i32),
    Cmpx { 
        a: i32, 
        b: i32, 
        #[rename = "z"]
        c: i32,
    },
}

fn main() {
    println!("{:?}", json_parse(r#"{"n": 12.3, "sim.b": true}"#).get::<Simple>());

    println!("{:?}", json_parse(r#"
    {
        "a": [1.2, 2.3],
        "s": {"n": 12.3, "b": true}
    }
    "#).get::<Nest>());

    println!("{:?}", json_parse(r#""u""#).get::<Enum>());
    
    println!("{:?}", json_parse(r#"{"One": 1}"#).get::<Enum>());

    println!("{:?}", json_parse(r#"
    {
        "Two": [1, 2]
    }
    "#).get::<Enum>());

    println!("{:?}", json_parse(r#"
    {
        "Cmpx": {"a": 1, "b": 2, "z": 3}
    }
    "#).get::<Enum>());
}