use crate::error::{ Error, Result }; use std::collections::HashMap; use std::str::FromStr; use json_event_parser::{ JsonEvent, SliceJsonParser }; #[allow(unused)] #[derive(Debug)] pub enum Value { Object(Object), Array(Array), Integer(i64), Float(f64), String(String), Boolean(bool), Null, } pub type Object = HashMap; pub type Array = Vec; pub fn parse_json(json: &[u8]) -> Result { let mut parser = SliceJsonParser::new(json); if let Some(event) = parser.next() { match event? { JsonEvent::StartObject => { Ok(Value::Object(parse_json_object(&mut parser)?)) } JsonEvent::StartArray => { Ok(Value::Array(parse_json_array(&mut parser)?)) } JsonEvent::String(_) => { Err(Error::custom("JSON: unexpected string at top level")) } JsonEvent::Number(_) => { Err(Error::custom("JSON: unexpected number at top level")) } JsonEvent::Boolean(_) => { Err(Error::custom("JSON: unexpected Boolean at top level")) } JsonEvent::Null => { Err(Error::custom("JSON: unexpected null at top level")) } JsonEvent::ObjectKey(_) => { Err(Error::custom("JSON: unexpected key at top level")) } JsonEvent::EndObject => { Err(Error::custom("JSON: unexpected object end at top level")) } JsonEvent::EndArray => { Err(Error::custom("JSON: unexpected array end at top level")) } } } else { Err(Error::custom("JSON: unexpected end at top level")) } } fn parse_json_object(parser: &mut SliceJsonParser) -> Result { let mut object = HashMap::new(); loop { if let Some(event) = parser.next() { match event? { JsonEvent::EndObject => { return Ok(object); } JsonEvent::ObjectKey(key) => { if object.contains_key(key.as_ref()) { return Err(Error::custom(&format!("JSON: duplicate key {}", key))); } let value = parse_json_value(parser)?; object.insert(key.into_owned(), value); } JsonEvent::StartObject => { return Err(Error::custom("JSON: unexpected object instead of key")); } JsonEvent::StartArray => { return Err(Error::custom("JSON: unexpected array instead of key")); } JsonEvent::String(_) => { return Err(Error::custom("JSON: unexpected string instead of key")); } JsonEvent::Number(_) => { return Err(Error::custom("JSON: unexpected number instead of key")); } JsonEvent::Boolean(_) => { return Err(Error::custom("JSON: unexpected Boolean instead of key")); } JsonEvent::Null => { return Err(Error::custom("JSON: unexpected null instead of key")); } JsonEvent::EndArray => { return Err(Error::custom("JSON: unexpected array end instead of key")); } } } else { return Err(Error::custom("JSON: unexpected end instead of key")); } } } fn parse_json_array(parser: &mut SliceJsonParser) -> Result { let mut array = Vec::new(); loop { if let Some(event) = parser.next() { match event? { JsonEvent::EndArray => { return Ok(array); } JsonEvent::StartObject => { array.push(Value::Object(parse_json_object(parser)?)); } JsonEvent::StartArray => { array.push(Value::Array(parse_json_array(parser)?)); } other => { array.push(parse_json_atomic_value(other)?); } } } else { return Err(Error::custom("JSON: unexpected end instead of value")); } } } fn parse_json_value(parser: &mut SliceJsonParser) -> Result { if let Some(event) = parser.next() { match event? { JsonEvent::StartObject => { Ok(Value::Object(parse_json_object(parser)?)) } JsonEvent::StartArray => { Ok(Value::Array(parse_json_array(parser)?)) } other => parse_json_atomic_value(other), } } else { Err(Error::custom("JSON: unexpected end instead of value")) } } fn parse_json_atomic_value(event: JsonEvent) -> Result { match event { JsonEvent::String(string) => { Ok(Value::String(string.into_owned())) } JsonEvent::Number(token) => { parse_json_number(&token) } JsonEvent::Boolean(value) => { Ok(Value::Boolean(value)) } JsonEvent::Null => { Ok(Value::Null) } JsonEvent::ObjectKey(_) => { Err(Error::custom("JSON: unexpected key instead of value")) } JsonEvent::EndObject => { Err(Error::custom("JSON: unexpected object end instead of value")) } JsonEvent::EndArray => { Err(Error::custom("JSON: unexpected array end instead of value")) } /* These ones don't actually come up from how we're called, but it's * better to handle the cases exhaustively. */ JsonEvent::StartObject => { Err(Error::custom( "JSON: unexpected object start instead of atomic value")) } JsonEvent::StartArray => { Err(Error::custom( "JSON: unexpected array start instead of atomic value")) } } } fn parse_json_number(token: &str) -> Result { if let Ok(number) = i64::from_str_radix(&token, 10) { Ok(Value::Integer(number)) } else if let Ok(number) = f64::from_str(&token) { Ok(Value::Float(number)) } else { /* JSON is highly permissive about numbers, but we aren't. */ Err(Error::custom("JSON: unsupported number syntax")) } }