yan_json 0.2.0

A lightweight library for easily handling JSON
Documentation
mod serialization {
    use crate::prelude::*;

    #[test]
    fn primitives() {
        assert_eq!(json!("hey").to_string(), "\"hey\"");
        assert_eq!(json!(42).to_string(), "42");
        assert_eq!(json!(1.5).to_string(), "1.5");
        assert_eq!(json!(true).to_string(), "true");
        assert_eq!(json!(false).to_string(), "false");
        assert_eq!(json!(null).to_string(), "null");
    }

    #[test]
    fn arrays() {
        assert_eq!(
            json!([null, 1, 2, 3, "abc", true]).to_string(),
            r#"[null,1,2,3,"abc",true]"#
        );
    }

    #[test]
    fn objects() {
        let obj = json!({"name": "yan", "age": 15, "siblings": true});
        let obj_str = r#"{"name": "yan", "age": 15, "siblings": true}"#;
        assert_eq!(JsonNode::parse(&obj.to_string()), JsonNode::parse(obj_str))
    }

    #[test]
    fn array_object_combination() {
        let obj = json!({"name": "yan", "age": 15, "siblings": ["A", "B", "C", "def"]});
        let obj_str = r#"{"name": "yan", "age": 15, "siblings": ["A", "B", "C", "def"]}"#;
        assert_eq!(JsonNode::parse(&obj.to_string()), JsonNode::parse(obj_str));
    }
}

mod deserialization {
    use crate::prelude::*;

    #[test]
    fn primitives() {
        assert_eq!(JsonNode::parse(r#""hey""#).unwrap(), json!("hey"));
        assert_eq!(JsonNode::parse("42").unwrap(), json!(42));
        assert_eq!(JsonNode::parse("1.5").unwrap(), json!(1.5));
        assert_eq!(JsonNode::parse("true").unwrap(), json!(true));
        assert_eq!(JsonNode::parse("false").unwrap(), json!(false));
        assert_eq!(JsonNode::parse("null").unwrap(), json!(null));
    }

    #[test]
    fn arrays() {
        assert_eq!(JsonNode::parse(r#"[1, 2, 3]"#), Some(json!([1, 2, 3])));
        assert_eq!(
            JsonNode::parse(r#"[1.1, true, null, "string"]"#),
            Some(json!([1.1, true, null, "string"]))
        );
        assert_eq!(
            JsonNode::parse(r#"[1, 2, [3, 4, ["five"]], 6]"#),
            Some(json!([1, 2, [3, 4, ["five"]], 6]))
        );
    }

    #[test]
    fn objects() {
        assert_eq!(JsonNode::parse(r#"{"key":"value"}"#), Some(json!({"key": "value"})))
    }

    #[test]
    fn array_object_combination() {
        let obj = json!({"name": "yan", "age": 15, "siblings": ["A", "B", "C", "def"]});
        let obj_str = r#"{"name": "yan", "age": 15, "siblings": ["A", "B", "C", "def"]}"#;
        assert_eq!(JsonNode::parse(&obj.to_string()), JsonNode::parse(obj_str));
    }
}

mod macros {
    use crate::prelude::*;

    #[test]
    fn primitives() {
        // null
        assert_eq!(json!(null), JsonNode::Null);

        // boolean
        assert_eq!(json!(true), JsonNode::Bool(true));
        assert_eq!(json!(false), JsonNode::Bool(false));

        // number
        use std::f64::consts;
        assert_eq!(json!(consts::PI), JsonNode::Number(consts::PI));
        assert_eq!(json!(0.25), JsonNode::Number(0.25));
        assert_eq!(json!(42), JsonNode::Number(42.0));

        // string
        assert_eq!(json!("hi"), JsonNode::String("hi".into()));
        assert_eq!(
            json!("tricky,string]"),
            JsonNode::String("tricky,string]".to_string())
        );
    }

    #[test]
    fn arrays() {
        assert_eq!(
            json!([true, null, "42", 12]),
            JsonNode::Array(vec![
                JsonNode::Bool(true),
                JsonNode::Null,
                JsonNode::String("42".to_string()),
                JsonNode::Number(12.0)
            ])
        );
        assert_eq!(
            json!([true, false, null, "hi", 42]),
            JsonNode::Array(vec![
                JsonNode::Bool(true),
                JsonNode::Bool(false),
                JsonNode::Null,
                "hi".into(),
                JsonNode::Number(42.0)
            ])
        );
    }

    #[test]
    fn objects() {
        assert_eq!(
            json!({
                "hello": "world",
                "a": true,
                "b": null
            }),
            JsonNode::Object(HashMap::from([
                ("hello".to_string(), JsonNode::String("world".to_string())),
                ("a".to_string(), JsonNode::Bool(true)),
                ("b".to_string(), JsonNode::Null)
            ]))
        );
        assert_eq!(
            json! ({
                "key": "value",
                "hello": true,
                "a": null,
                "count": 3
            }),
            JsonNode::Object(HashMap::from([
                ("key".into(), "value".into()),
                ("hello".into(), true.into()),
                ("a".into(), JsonNode::Null),
                ("count".into(), JsonNode::from(3)),
            ]))
        );
    }

    #[test]
    fn object_array_combination() {
        assert_eq!(
            json!({
                "words": [
                    "abc,def",
                    "hi",
                    {
                        "w": true,
                        "l": null
                    }
                ],
                "time in ms": 166
            }),
            JsonNode::Object(HashMap::from([
                (
                    "words".to_string(),
                    JsonNode::Array(vec![
                        "abc,def".into(),
                        "hi".into(),
                        JsonNode::Object(HashMap::from([
                            ("w".into(), JsonNode::Bool(true)),
                            ("l".into(), JsonNode::Null)
                        ]))
                    ])
                ),
                ("time in ms".into(), JsonNode::Number(166.0))
            ]))
        )
    }

    #[test]
    fn wild() {
        assert_eq!(
            json!({
                "users": [
                    {
                        "id": 1,
                        "name": "Alice",
                        "roles": ["admin", "editor"],
                        "meta": {
                            "active": true,
                            "last_login": "2025-10-06T12:34:56Z",
                            "rating": 4.9,
                            "preferences": {
                                "theme": "dark",
                                "notifications": false
                            }
                        }
                    },
                    {
                        "id": 2,
                        "name": "Bob",
                        "roles": [],
                        "meta": null
                    }
                ],
                "stats": {
                    "users_online": 128,
                    "load_avg": [0.52, 0.76, 0.89],
                    "uptime": { "days": 42, "hours": 5 }
                },
                "features": [
                    "json-macro",
                    "serde-free",
                    {
                        "experimental": true,
                        "flags": [
                            "alpha",
                            "beta",
                            { "deep": "nesting", "level": 3 }
                        ]
                    }
                ]
            }),
            JsonNode::Object(HashMap::from([
                (
                    "users".to_string(),
                    JsonNode::Array(vec![
                        JsonNode::Object(HashMap::from([
                            ("id".into(), 1.into()),
                            ("name".into(), "Alice".into()),
                            (
                                "roles".into(),
                                JsonNode::Array(vec!["admin".into(), "editor".into()]),
                            ),
                            (
                                "meta".into(),
                                JsonNode::Object(HashMap::from([
                                    ("active".into(), true.into()),
                                    ("last_login".into(), "2025-10-06T12:34:56Z".into()),
                                    ("rating".into(), 4.9.into()),
                                    (
                                        "preferences".into(),
                                        JsonNode::Object(HashMap::from([
                                            ("theme".into(), "dark".into()),
                                            ("notifications".into(), false.into()),
                                        ])),
                                    ),
                                ])),
                            ),
                        ])),
                        JsonNode::Object(HashMap::from([
                            ("id".into(), 2.into()),
                            ("name".into(), "Bob".into()),
                            ("roles".into(), JsonNode::Array(vec![])),
                            ("meta".into(), JsonNode::Null),
                        ])),
                    ]),
                ),
                (
                    "stats".into(),
                    JsonNode::Object(HashMap::from([
                        ("users_online".into(), 128.into()),
                        (
                            "load_avg".into(),
                            JsonNode::Array(vec![0.52.into(), 0.76.into(), 0.89.into()]),
                        ),
                        (
                            "uptime".into(),
                            JsonNode::Object(HashMap::from([
                                ("days".into(), 42.into()),
                                ("hours".into(), 5.into()),
                            ])),
                        ),
                    ])),
                ),
                (
                    "features".into(),
                    JsonNode::Array(vec![
                        "json-macro".into(),
                        "serde-free".into(),
                        JsonNode::Object(HashMap::from([
                            ("experimental".into(), true.into()),
                            (
                                "flags".into(),
                                JsonNode::Array(vec![
                                    "alpha".into(),
                                    "beta".into(),
                                    JsonNode::Object(HashMap::from([
                                        ("deep".into(), "nesting".into()),
                                        ("level".into(), 3.into()),
                                    ])),
                                ]),
                            ),
                        ])),
                    ]),
                ),
            ]))
        );
    }
}