yan_json 0.1.0

A lightweight library for easily handling JSON
Documentation
use std::collections::HashMap;

use crate::*;

#[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 array() {
    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 object() {
    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()),
                                ])),
                            ]),
                        ),
                    ])),
                ]),
            ),
        ]))
    );
}