Skip to main content

parse

Function parse 

Source
pub fn parse<T>(src: &str) -> Result<T, ParseError>
where T: for<'s, 'b> Deserialize<'s, 'b>,
Expand description

Deserialize a fully-owned type from a &str. The scratch buffer is auto-allocated; no size choice required.

§Example

let n: i64 = nanojson::parse("42").unwrap();
assert_eq!(n, 42);
Examples found in repository?
examples/simple.rs (line 14)
11fn main() {
12    // Parse a JSON string into a struct
13    let json = r#"{}"#;
14    let my_struct = nanojson::parse::<MyStruct>(json);
15
16    if let Err(ref e) = my_struct { e.print(json); }
17    if let Ok(mut my_struct) = my_struct {
18        println!("Parsed: {:?}", my_struct);
19
20        // Change the fields and turn back into a JSON string again
21        my_struct.num = 420;
22        my_struct.name = "world".to_string();
23        if let Ok(json) = nanojson::stringify(&my_struct) {
24            println!("JSON: {}", json);
25        }
26    }
27}
More examples
Hide additional examples
examples/derive.rs (line 66)
51fn main() {
52    let entity = Entity {
53        id: 42,
54        active: true,
55        position: Vec2 { x: 100, y: -50 },
56        health: 80,
57    };
58
59    // ----------------------------------------------------------------
60    // std tier — String in, String out, no buffer choices
61    // ----------------------------------------------------------------
62
63    let json = nanojson::stringify(&entity).unwrap();
64    std::println!("Entity JSON (std): {json}");
65
66    let entity2: Entity = nanojson::parse(&json).unwrap();
67    std::println!("Decoded (std):     {:?}", entity2);
68    assert_eq!(entity, entity2);
69
70    // ----------------------------------------------------------------
71    // no_std tier — stack-allocated buffers, explicit sizes
72    //
73    // stringify_sized::<N, _>  — N bytes for the output JSON.
74    // parse_sized::<S, _> — S bytes for the string-decode scratch buffer;
75    //                     only needs to fit the longest single field value.
76    // ----------------------------------------------------------------
77
78    let (buf, len) = nanojson::stringify_sized::<256, _>(&entity).unwrap();
79    std::println!("\nEntity JSON (no_std, {len} bytes): {}", core::str::from_utf8(&buf[..len]).unwrap());
80
81    let entity3: Entity = nanojson::parse_sized::<64, _>(&buf[..len]).unwrap();
82    std::println!("Decoded (no_std):  {:?}", entity3);
83    assert_eq!(entity, entity3);
84
85    // ----------------------------------------------------------------
86    // Unit enum — std and no_std
87    // ----------------------------------------------------------------
88
89    let team = Team::Spectator;
90
91    let json = nanojson::stringify(&team).unwrap();
92    std::println!("\nTeam JSON (std):    {json}");
93    let team2: Team = nanojson::parse(&json).unwrap();
94    assert_eq!(team, team2);
95
96    let (buf, len) = nanojson::stringify_sized::<16, _>(&team).unwrap();
97    std::println!("Team JSON (no_std): {}", core::str::from_utf8(&buf[..len]).unwrap());
98    let team3: Team = nanojson::parse_sized::<16, _>(&buf[..len]).unwrap();
99    assert_eq!(team, team3);
100
101    // ----------------------------------------------------------------
102    // Data enum
103    // ----------------------------------------------------------------
104
105    let events: [Event; 2] = [
106        Event::Spawn { entity_id: 1, x: 0, y: 0 },
107        Event::Death { entity_id: 1 },
108    ];
109
110    for ev in &events {
111        // std
112        let json = nanojson::stringify(ev).unwrap();
113        std::println!("\nEvent JSON (std):    {json}");
114        let ev2: Event = nanojson::parse(&json).unwrap();
115        assert_eq!(*ev, ev2);
116
117        // no_std
118        let (buf, len) = nanojson::stringify_sized::<128, _>(ev).unwrap();
119        std::println!("Event JSON (no_std): {}", core::str::from_utf8(&buf[..len]).unwrap());
120        let ev3: Event = nanojson::parse_sized::<32, _>(&buf[..len]).unwrap();
121        assert_eq!(*ev, ev3);
122    }
123
124    // ----------------------------------------------------------------
125    // Error handling — unknown field, missing field
126    // ----------------------------------------------------------------
127
128    let bad = r#"{"id":1,"is_active":true,"position":{"x":0,"y":0},"health":100,"unknown":999}"#;
129    match nanojson::parse::<Entity>(bad) {
130        Err(e) => std::println!("\nExpected error (unknown field): {:?} at offset {}", e.kind, e.offset),
131        Ok(_)  => panic!("should have failed"),
132    }
133
134    let incomplete = r#"{"id":2,"is_active":false,"position":{"x":1,"y":2}}"#;
135    match nanojson::parse::<Entity>(incomplete) {
136        Err(e) => std::println!("Expected error (missing field): {:?} at offset {}", e.kind, e.offset),
137        Ok(_)  => panic!("should have failed"),
138    }
139
140    // ----------------------------------------------------------------
141    // Size estimation — measure before picking N
142    // ----------------------------------------------------------------
143
144    let n = nanojson::measure(|s| entity.serialize(s));
145    std::println!("\nEntity serializes to {n} bytes — use stringify_sized::<{n}, _> or larger.");
146}