parse

Function parse 

Source
pub fn parse(s: &str) -> Result
Expand description

Parses a json value, disregarding whitespace.
See the parse module for more info

Examples found in repository?
examples/parsing-custom-struct.rs (line 46)
43fn parse_person(s: &str) -> Result<Person, ParsePersonError> {
44  use ParsePersonError::*;
45
46  let json = jso::parse(s).map_err(JsonParse)?;
47  let mut o = json.obj().ok_or(JsonNotAnObject)?;
48
49  let name = o
50    .remove("name")
51    .ok_or(Custom("missing \"name\""))?
52    .str()
53    .ok_or(Custom("\"name\" is not a string"))?
54    .trim()
55    .to_string();
56
57  if name.is_empty() {
58    return Err(Custom("\"name\" empty"));
59  }
60
61  let age = o
62    .remove("age")
63    .ok_or(Custom("missing \"age\""))?
64    .num()
65    .ok_or(Custom("\"age\" is not a number"))?;
66
67  if age.fract() != 0. {
68    return Err(Custom("\"age\" must be an integer"));
69  }
70
71  if !(0.0..=255.0).contains(&age) {
72    return Err(Custom("\"age\" must be between 0-255"));
73  }
74
75  let age = age as u8;
76
77  let alive = o
78    .remove("alive")
79    .ok_or(Custom("missing \"alive\""))?
80    .bool()
81    .ok_or(Custom("\"alive\" not a boolean"))?;
82
83  Ok(Person { name, age, alive })
84}