kyomu-json 0.1.2

Allows recursive reflection based serialization and deserialization of json. Supports structs and no derives are needed. Nightly required.
Documentation
  • Coverage
  • 0%
    0 out of 3 items documented0 out of 2 items with examples
  • Size
  • Source code size: 9.26 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.09 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 12s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Repository
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • 9SonSteroids

Allows recursive reflection based serialization and deserialization of json. Supports structs and no derives are needed. Nightly required.

Supported types

  • Primitives
  • &str
  • [T; N]
  • (1, 2.0)
  • json serialization
  • json deserialization
  • structs
  • &dyn Trait
  • enums
  • unions

Example:

use json::{from_json, to_json};

#[derive(PartialEq, Debug)]
pub struct Test {
    pub a: u8,
    pub b: TestB,
}

#[derive(PartialEq, Debug)]
pub struct TestB {
    pub c: (f64, String),
}

#[test]
fn test() {
    assert_eq!(
        from_json::<Test>("{\"a\": 4, \"b\": {\"c\": [1.1, \"2\"]}}"),
        Test {
            a: 4,
            b: TestB {
                c: (1.1, 2.to_string())
            }
        }
    );

    assert_eq!(
        to_json(&Test {
            a: 2,
            b: TestB {
                c: (1.0, 2.to_string())
            }
        }),
        "{\"a\": 2, \"b\": {\"c\": [1.0, \"2\"]}}"
    );

    assert!(!from_json::<bool>("false"));

    assert_eq!(to_json(&String::from("hi")), "\"hi\"");
    assert_eq!(to_json(&false), "false");
    assert_eq!(to_json(&'d'), "\"d\"");
    assert_eq!(to_json(&"lol"), "\"lol\"");

    assert_eq!(to_json(&[1, 2]), "[1, 2]");

    assert_eq!(from_json::<u8>("1"), 1);
    assert_eq!(from_json::<[u8; 2]>("[1,2]"), [1, 2]);
    assert_eq!(from_json::<String>("\"hi\""), "hi");
    assert_eq!(
        from_json::<TestB>("{\"c\": [1.1, \"2\"]}"),
        TestB {
            c: (1.1, 2.to_string())
        }
    );
}