mod deserialization {
use crate::prelude::*;
#[test]
fn primitives() {
assert_eq!(JsonNode::parse(r#""hey""#), Some(json!("hey")));
assert_eq!(JsonNode::parse("42"), Some(json!(42)));
assert_eq!(JsonNode::parse("1.5"), Some(json!(1.5)));
assert_eq!(JsonNode::parse("true"), Some(json!(true)));
assert_eq!(JsonNode::parse("false"), Some(json!(false)));
assert_eq!(JsonNode::parse("null"), Some(json!(null)));
assert_eq!(JsonNode::parse(r#" " ,h]ey{" "#), Some(json!(" ,h]ey{")));
assert_eq!(JsonNode::parse(" null "), Some(json!(null)));
}
#[test]
fn arrays() {
assert_eq!(JsonNode::parse("[]"), Some(json!([])));
assert_eq!(JsonNode::parse(r#"[2]"#), Some(json!([2])));
assert_eq!(
JsonNode::parse(r#"[true, false, false]"#),
Some(json!([true, false, false]))
);
assert_eq!(
JsonNode::parse(r#"[null, 3.41, false, "string", 5]"#),
Some(json!([null, 3.41, false, "string", 5]))
);
assert_eq!(JsonNode::parse(r#"[null]"#), Some(json!([null])));
assert_eq!(
JsonNode::parse(r#"[1.1, true, null, ",{,},][]"]"#),
Some(json!([1.1, true, null, ",{,},][]"]))
);
assert_eq!(
JsonNode::parse(r#"[1, 2, [3, 4, ["five"]], 6]"#),
Some(json!([1, 2, [3, 4, ["five"]], 6]))
);
assert_eq!(
JsonNode::parse(r#"[" ,hello,", 42, [[[0.4]]], ","]"#),
Some(json!([" ,hello,", 42, [[[0.4]]], ","]))
);
}
#[test]
fn objects() {
assert_eq!(
JsonNode::parse(r#"{"key":"value"}"#),
Some(json!({"key": "value"}))
);
assert_eq!(JsonNode::parse(r#"{}"#), Some(json!({})));
assert_eq!(
JsonNode::parse(r#"{"name":"Alice","age":30,"active":true}"#),
Some(json!({"name": "Alice", "age": 30, "active": true}))
);
assert_eq!(
JsonNode::parse(r#"{"person":{"name":"Bob","details":{"age":25,"city":"NY"}}}"#),
Some(json!({"person": {"name": "Bob", "details": {"age": 25, "city": "NY"}}}))
);
assert_eq!(
JsonNode::parse(r#"{"numbers":[1,2,3,4],"letters":["a","b","c"]}"#),
Some(json!({"numbers": [1,2,3,4], "letters": ["a","b","c"]}))
);
assert_eq!(
JsonNode::parse(r#"{"str":"hello","num":1.5,"bool":false,"nil":null}"#),
Some(json!({"str": "hello", "num": 1.5, "bool": false, "nil": null}))
);
assert_eq!(
JsonNode::parse(r#"{ "x" : 42 , "y" : [ true , false ] }"#),
Some(json!({"x": 42, "y": [true, false]}))
);
}
#[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));
let arr = json!([{"id": 1}, {"id": 2, "info": {"nested": true}}, {"id": 3}]);
let arr_str = r#"[{"id":1},{"id":2,"info":{"nested":true}},{"id":3}]"#;
assert_eq!(JsonNode::parse(&arr.to_string()), JsonNode::parse(arr_str));
let complex = json!({
"users": [
{"name": "Alice", "roles": ["admin", "editor"]},
{"name": "Bob", "roles": []}
],
"count": 2,
"meta": {"active": true, "tags": ["new","vip"]}
});
let complex_str = r#"{"users":[{"name":"Alice","roles":["admin","editor"]},{"name":"Bob","roles":[]}],"count":2,"meta":{"active":true,"tags":["new","vip"]}}"#;
assert_eq!(
JsonNode::parse(&complex.to_string()),
JsonNode::parse(complex_str)
);
let empty_combo = json!({"empty_array": [], "empty_object": {}});
let empty_combo_str = r#"{"empty_array":[],"empty_object":{}}"#;
assert_eq!(
JsonNode::parse(&empty_combo.to_string()),
JsonNode::parse(empty_combo_str)
);
let spaced = r#"{ "a" : [ 1 , 2 , { "b" : "c" } ] , "d" : null }"#;
let expected = json!({"a": [1, 2, {"b": "c"}], "d": null});
assert_eq!(JsonNode::parse(spaced), Some(expected));
}
#[test]
fn malformed() {
macro_rules! assert_invalid_json {
($($s:literal),* $(,)?) => {
$(assert_eq!(JsonNode::parse($s), None);)*
};
}
assert_invalid_json!("[", "]", "{", "}", "[}", "{]", "{}}[", "]]{]}{}");
assert_invalid_json!(
r#"{"name": "Alice", "age": 30,, "city": "New York"}"#,
r#"{name: "Bob", "age": 25}"#,
r#"{'name': 'Charlie', 'age': 40}"#,
r#"{"name" "Eve", "age": 35}"#,
r#"{"name": "Grace", "age": 30]"#,
r#"{"a": 10, "b": 20"#,
r#"{"a": [1, 2]], "b": 3}"#,
);
}
#[test]
fn wild() {
let wild_json_str = r#"
{
"string": "hello, {world} []",
"number": 12345.678,
"boolean_true": true,
"boolean_false": false,
"null_value": null,
"array": [1, "two", null, true, false, [3.41, ["nested", {"deep": "yes"}]]],
"object": {
"nested_array": [ {"x": 1}, {"y": [true, false, null]} ],
"nested_object": {
"a": "alpha",
"b": 2,
"c": [ {"deep": [1,2,3]}, {} ]
}
},
"empty_array": [],
"empty_object": {},
"weird_spacing": [ " spaced " , 42 , { " spaced_key " : " spaced_value " } ]
}
"#;
let expected = json!({
"string": "hello, {world} []",
"number": 12345.678,
"boolean_true": true,
"boolean_false": false,
"null_value": null,
"array": [1, "two", null, true, false, [3.41, ["nested", {"deep": "yes"}]]],
"object": {
"nested_array": [ {"x": 1}, {"y": [true, false, null]} ],
"nested_object": {
"a": "alpha",
"b": 2,
"c": [ {"deep": [1,2,3]}, {} ]
}
},
"empty_array": [],
"empty_object": {},
"weird_spacing": [" spaced ", 42, {" spaced_key ": " spaced_value "}]
});
assert_eq!(JsonNode::parse(wild_json_str), Some(expected));
}
}
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));
}
fn wild() {
let wild_obj = json!({
"user": {
"name": "Alice",
"age": 30,
"contact": {
"email": "alice@example.com",
"phones": [null, "+1234567890", "+0987654321"]
}
},
"active": true,
"scores": [3.41, 42, null, {"nested": [false, "weird\"quote", {"deep": "value"}]}],
"meta": {
"created_at": "2025-10-07T12:00:00Z",
"tags": ["dev", "test", "💻", null]
}
});
let wild_str = r#"{
"user": {
"name": "Alice",
"age": 30,
"contact": {
"email": "alice@example.com",
"phones": [null, "+1234567890", "+0987654321"]
}
},
"active": true,
"scores": [3.41, 42, null, {"nested": [false, "weird\"quote", {"deep": "value"}]}],
"meta": {
"created_at": "2025-10-07T12:00:00Z",
"tags": ["dev", "test", "💻", null]
}
}"#;
assert_eq!(
JsonNode::parse(&wild_obj.to_string()),
JsonNode::parse(wild_str)
);
}
}
mod macros {
use crate::prelude::*;
#[test]
fn primitives() {
assert_eq!(json!(null), JsonNode::Null);
assert_eq!(json!(true), JsonNode::Bool(true));
assert_eq!(json!(false), JsonNode::Bool(false));
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));
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()),
])),
]),
),
])),
]),
),
]))
);
}
}