2020-02-07 20:01:10 -05:00
|
|
|
//! # Naive JSON Parser
|
2020-02-07 21:27:46 -05:00
|
|
|
//!
|
|
|
|
//! Based on [JSON Parser with JavaScript](https://lihautan.com/json-parser-with-javascript/)
|
|
|
|
#![forbid(unsafe_code)]
|
2020-02-07 16:23:51 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::iter::FromIterator;
|
2020-02-07 22:55:09 -05:00
|
|
|
use std::{char, u16};
|
2020-02-07 16:23:51 -05:00
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// The type of JSON value
|
2020-02-07 16:23:51 -05:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum JSONValue {
|
2020-02-07 21:27:46 -05:00
|
|
|
/// Object Literal
|
2020-02-07 16:23:51 -05:00
|
|
|
Object(HashMap<String, JSONValue>),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Array Literal
|
2020-02-07 16:23:51 -05:00
|
|
|
Array(Vec<JSONValue>),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// String Literal
|
2020-02-07 16:23:51 -05:00
|
|
|
String(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Number Literal
|
2020-02-07 16:23:51 -05:00
|
|
|
Number(f64),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// True Literal
|
2020-02-07 16:23:51 -05:00
|
|
|
True,
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// False Literal
|
2020-02-07 16:23:51 -05:00
|
|
|
False,
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Null Literal
|
2020-02-07 16:23:51 -05:00
|
|
|
Null,
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// The type of error returned by the parser
|
2020-02-07 20:01:10 -05:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-02-07 16:23:51 -05:00
|
|
|
pub enum ParseError {
|
2020-02-07 21:27:46 -05:00
|
|
|
/// The input looks like JSON, but seems to end
|
2020-02-07 16:23:51 -05:00
|
|
|
UnexpectedEndOfInput(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Looks like JSON, but seems to have characters after it should
|
2020-02-07 16:23:51 -05:00
|
|
|
ExpectedEndOfInput(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Wasn't this supposed to be an object literal?
|
2020-02-07 16:23:51 -05:00
|
|
|
ExpectedObjectKey(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Hey, wasn't there supposed to be...?
|
2020-02-07 16:23:51 -05:00
|
|
|
ExpectedToken(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// What's this character?
|
2020-02-07 16:23:51 -05:00
|
|
|
UnexpectedToken(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Shouldn't this be a numeral?
|
2020-02-07 16:23:51 -05:00
|
|
|
ExpectedDigit(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// There's a backslash...were you going somewhere with that?
|
2020-02-07 16:23:51 -05:00
|
|
|
ExpectedEscapeChar(String),
|
2020-02-07 21:27:46 -05:00
|
|
|
|
|
|
|
/// Should be a unicode escape character...missing a few digits?
|
2020-02-07 16:23:51 -05:00
|
|
|
ExpectedUnicodeEscape(String),
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// This struct holds a little state for parsing
|
2020-02-07 20:01:10 -05:00
|
|
|
#[derive(Debug, PartialEq)]
|
2020-02-07 16:23:51 -05:00
|
|
|
pub struct JSON {
|
2020-02-07 21:27:46 -05:00
|
|
|
/// The input JSON String as a character array
|
2020-02-07 16:23:51 -05:00
|
|
|
chars: Vec<char>,
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// The internal parsing index
|
|
|
|
i: usize,
|
|
|
|
}
|
2020-02-07 16:23:51 -05:00
|
|
|
|
|
|
|
impl JSON {
|
2020-02-07 21:27:46 -05:00
|
|
|
/// Private constructor
|
2020-02-07 16:23:51 -05:00
|
|
|
fn new(json: &str) -> Self {
|
2020-02-07 21:27:46 -05:00
|
|
|
JSON {
|
|
|
|
chars: json.chars().collect(),
|
|
|
|
i: 0,
|
|
|
|
}
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// Parse a `JSONValue` from the current JSON string
|
|
|
|
///
|
|
|
|
/// This is probably an abuse of iterators...but it's still much better than the alternative
|
|
|
|
/// of nested matches.
|
|
|
|
///
|
|
|
|
/// In order to determine the type of JSON value, each parse method is tried, until one
|
|
|
|
/// matches, or a parse error happens.
|
|
|
|
///
|
2020-02-10 12:26:54 -05:00
|
|
|
/// * `Option`s implement IntoIterator, which returns an iterator of 0 or 1 items: the
|
2020-02-07 21:27:46 -05:00
|
|
|
/// transferred (not borrowed) Some() value.
|
|
|
|
/// * The `chain` method of iterators allows you to link iterators together,
|
|
|
|
/// to act as one iterator
|
|
|
|
/// * The first result from the iterator is the first parse method with a non-empty value,
|
|
|
|
/// and should be the value wanted
|
|
|
|
fn parse_value(&mut self) -> Result<JSONValue, ParseError> {
|
2020-02-07 16:23:51 -05:00
|
|
|
self.skip_whitespace();
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
let mut value = self
|
|
|
|
.parse_string()?
|
|
|
|
.into_iter()
|
2020-02-07 16:23:51 -05:00
|
|
|
.chain(self.parse_number()?.into_iter())
|
|
|
|
.chain(self.parse_object()?.into_iter())
|
|
|
|
.chain(self.parse_array()?.into_iter())
|
|
|
|
.chain(self.parse_keyword("true", JSONValue::True)?.into_iter())
|
|
|
|
.chain(self.parse_keyword("false", JSONValue::False)?.into_iter())
|
|
|
|
.chain(self.parse_keyword("null", JSONValue::Null)?.into_iter());
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
match value.next() {
|
2020-02-07 16:23:51 -05:00
|
|
|
Some(val) => Ok(val),
|
2020-02-07 21:27:46 -05:00
|
|
|
None => Err(ParseError::UnexpectedEndOfInput(String::from(
|
|
|
|
"Doesn't seem to be valid JSON",
|
|
|
|
))),
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// See if there's a `JSONValue::Object` next in the JSON
|
|
|
|
fn parse_object(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
2020-02-07 16:23:51 -05:00
|
|
|
if self.chars[self.i] != '{' {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 16:23:51 -05:00
|
|
|
|
|
|
|
self.skip_whitespace();
|
|
|
|
|
|
|
|
let mut result: HashMap<String, JSONValue> = HashMap::new();
|
|
|
|
|
|
|
|
let mut initial = true;
|
|
|
|
|
|
|
|
// if it is not '}',
|
|
|
|
// we take the path of string -> whitespace -> ':' -> value -> ...
|
|
|
|
while self.chars[self.i] != '}' {
|
|
|
|
if initial == false {
|
2020-02-07 20:01:10 -05:00
|
|
|
self.eat(',')?;
|
2020-02-07 16:23:51 -05:00
|
|
|
self.skip_whitespace();
|
|
|
|
}
|
|
|
|
|
|
|
|
let key = match self.parse_string()? {
|
|
|
|
Some(value) => match value {
|
|
|
|
JSONValue::String(s) => s,
|
|
|
|
_ => panic!("parse_string returned non-string value"),
|
|
|
|
},
|
2020-02-07 21:27:46 -05:00
|
|
|
None => String::new(),
|
2020-02-07 16:23:51 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
self.skip_whitespace();
|
2020-02-07 20:01:10 -05:00
|
|
|
self.eat(':')?;
|
2020-02-07 16:23:51 -05:00
|
|
|
let value = self.parse_value()?;
|
|
|
|
|
|
|
|
result.insert(key, value);
|
|
|
|
|
|
|
|
initial = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move to the next character: '}'
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 16:23:51 -05:00
|
|
|
|
|
|
|
Ok(Some(JSONValue::Object(result)))
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// See if there's a `JSONValue::Array` next in the JSON
|
|
|
|
fn parse_array(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
2020-02-07 16:23:51 -05:00
|
|
|
if self.chars[self.i] != '[' {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 16:23:51 -05:00
|
|
|
self.skip_whitespace();
|
|
|
|
|
|
|
|
let mut result: Vec<JSONValue> = vec![];
|
|
|
|
let mut initial = true;
|
|
|
|
|
|
|
|
while self.chars[self.i] != ']' {
|
|
|
|
if initial == false {
|
2020-02-07 20:01:10 -05:00
|
|
|
self.eat(',')?;
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|
|
|
|
let value = self.parse_value()?;
|
|
|
|
result.push(value);
|
|
|
|
initial = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// move to next character: ']'
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 16:23:51 -05:00
|
|
|
|
|
|
|
Ok(Some(JSONValue::Array(result)))
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// See if there's a `JSONValue::String` next in the JSON
|
|
|
|
fn parse_string(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
|
|
|
if self.chars[self.i] != '"' {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 21:27:46 -05:00
|
|
|
let mut result = String::new();
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
while self.chars[self.i] != '"' && self.i < self.chars.len() - 1 {
|
2020-02-07 22:55:09 -05:00
|
|
|
// All the escape sequences...
|
2020-02-07 21:29:41 -05:00
|
|
|
if self.chars[self.i] == '\\' {
|
2020-02-07 22:55:09 -05:00
|
|
|
let ch = self.chars[self.i + 1];
|
|
|
|
if ['"', '\\', '/'].contains(&ch) {
|
|
|
|
let escaped = ch.escape_default().next().unwrap_or(ch);
|
|
|
|
|
|
|
|
result.push(escaped);
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 22:55:09 -05:00
|
|
|
} else if ['b', 'f', 'n', 'r', 't'].contains(&ch) {
|
|
|
|
let ch = match ch {
|
|
|
|
'b' => '\u{8}',
|
|
|
|
'f' => '\x0C',
|
|
|
|
'n' => '\n',
|
|
|
|
'r' => '\r',
|
|
|
|
't' => '\t',
|
|
|
|
_ => panic!("Shouldn't be possible!"),
|
|
|
|
};
|
|
|
|
result.push(ch);
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 22:55:09 -05:00
|
|
|
} else if ch == 'u'
|
|
|
|
&& self.chars[self.i + 2].is_ascii_hexdigit()
|
|
|
|
&& self.chars[self.i + 3].is_ascii_hexdigit()
|
|
|
|
&& self.chars[self.i + 4].is_ascii_hexdigit()
|
|
|
|
&& self.chars[self.i + 5].is_ascii_hexdigit()
|
|
|
|
{
|
|
|
|
// Blech, parse out a JSON unicode (utf16) escape code. Handles surrogate pairs
|
|
|
|
// by giving you the replacement character...because...yeah
|
|
|
|
let char_str = String::from_iter(&self.chars[self.i + 2..=self.i + 5]);
|
|
|
|
let code = u16::from_str_radix(&char_str, 16)
|
|
|
|
.expect("Failed to parse unicode escape number");
|
|
|
|
let string = String::from_utf16_lossy(&[code]);
|
|
|
|
|
|
|
|
result.push_str(&string);
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(5);
|
2020-02-07 22:55:09 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push(self.chars[self.i]);
|
2020-02-07 21:27:46 -05:00
|
|
|
}
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 21:27:46 -05:00
|
|
|
}
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 22:55:09 -05:00
|
|
|
|
|
|
|
Ok(Some(JSONValue::String(result)))
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// See if there's a `JSONValue::Number` next in the JSON
|
|
|
|
fn parse_number(&mut self) -> Result<Option<JSONValue>, ParseError> {
|
2020-02-10 12:26:54 -05:00
|
|
|
// If it doesn't start with 0-9 or a minus sign, it's probably not a number
|
|
|
|
if ! (self.chars[self.i].is_ascii_digit() || self.chars[self.i] == '-') {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
2020-02-07 23:51:49 -05:00
|
|
|
|
|
|
|
// All this looping basically just counts the number of characters in the number
|
|
|
|
let start = self.i;
|
|
|
|
let max = self.chars.len() - 1;
|
|
|
|
let mut n = start;
|
|
|
|
|
|
|
|
if self.chars[n] == '-' && n < max {
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.chars[n] == '0' && n < max {
|
|
|
|
n += 1;
|
|
|
|
} else if self.chars[n] >= '1' && self.chars[n] <= '9' && n < max {
|
|
|
|
n += 1;
|
2020-02-10 12:26:54 -05:00
|
|
|
while self.chars[n].is_ascii_digit() && n < max {
|
2020-02-07 23:51:49 -05:00
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.chars[n] == '.' && n < max {
|
|
|
|
n += 1;
|
|
|
|
while self.chars[n].is_ascii_digit() && n < max {
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.chars[n] == 'e' || self.chars[n] == 'E' && n < max {
|
|
|
|
n += 1;
|
|
|
|
|
|
|
|
if self.chars[n] == '-' || self.chars[n] == '+' && n < max {
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
while self.chars[n].is_ascii_digit() && n < max {
|
|
|
|
n += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if n > start {
|
|
|
|
let end = if n < self.chars.len() {
|
|
|
|
n
|
|
|
|
} else {
|
|
|
|
self.chars.len()
|
|
|
|
};
|
|
|
|
|
|
|
|
let str = String::from_iter(&self.chars[start..=end]);
|
2020-02-10 12:26:54 -05:00
|
|
|
self.i += str.len() - 1;
|
2020-02-07 23:51:49 -05:00
|
|
|
|
|
|
|
match str.parse::<f64>() {
|
|
|
|
Ok(n) => Ok(Some(JSONValue::Number(n))),
|
|
|
|
Err(e) => Err(ParseError::ExpectedDigit(format!("{:#?}", (str, e)))),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// See if there's a `JSONValue::True`, `JSONValue::False`, or a `JSONValue::Null` next in the JSON
|
|
|
|
fn parse_keyword(
|
|
|
|
&mut self,
|
|
|
|
search: &str,
|
|
|
|
value: JSONValue,
|
|
|
|
) -> Result<Option<JSONValue>, ParseError> {
|
2020-02-07 20:01:10 -05:00
|
|
|
let start = self.i;
|
|
|
|
let end = if self.i + search.len() > self.chars.len() {
|
|
|
|
self.chars.len()
|
|
|
|
} else {
|
|
|
|
self.i + search.len()
|
|
|
|
};
|
|
|
|
|
|
|
|
let slice = &String::from_iter(&self.chars[start..end]);
|
2020-02-07 16:23:51 -05:00
|
|
|
if slice == search {
|
2020-02-07 21:27:46 -05:00
|
|
|
self.i += search.len();
|
2020-02-07 16:23:51 -05:00
|
|
|
|
|
|
|
return Ok(Some(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// Increment the internal index until the next character is not a whitespace character
|
2020-02-07 16:23:51 -05:00
|
|
|
fn skip_whitespace(&mut self) {
|
|
|
|
while self.chars[self.i].is_ascii_whitespace() {
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
/// 'Eat' the specified character
|
|
|
|
///
|
|
|
|
/// * If the next `char` matches the one passed, the internal index is incremented
|
|
|
|
/// * If the next `char` does not match the one passed, a `ParseError::ExpectedToken`
|
|
|
|
/// error is returned
|
2020-02-07 20:01:10 -05:00
|
|
|
fn eat(&mut self, ch: char) -> Result<(), ParseError> {
|
2020-02-07 16:23:51 -05:00
|
|
|
if self.chars[self.i] != ch {
|
2020-02-07 23:51:49 -05:00
|
|
|
let msg = format!("Expected '{}'.", ch);
|
2020-02-07 16:23:51 -05:00
|
|
|
return Err(ParseError::ExpectedToken(msg));
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
self.increment(1);
|
2020-02-07 16:23:51 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
fn increment(&mut self, amount: usize) {
|
|
|
|
let current = self.i;
|
|
|
|
if current + amount >= self.chars.len() {
|
|
|
|
self.i = self.chars.len() - 1;
|
|
|
|
} else {
|
|
|
|
self.i += amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 20:01:10 -05:00
|
|
|
/// Convert a `&str` containing JSON into a `Result<JSONValue, ParseError>`
|
2020-02-07 21:27:46 -05:00
|
|
|
pub fn parse(json: &str) -> Result<JSONValue, ParseError> {
|
2020-02-07 16:23:51 -05:00
|
|
|
JSON::new(json).parse_value()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 20:01:10 -05:00
|
|
|
fn parse_keyword() {
|
|
|
|
let mut parser = JSON::new(r#""foobarbaz""#);
|
|
|
|
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
|
|
|
|
assert_eq!(res, Ok(None));
|
|
|
|
|
|
|
|
let mut parser = JSON::new("true");
|
|
|
|
let res = JSON::parse_keyword(&mut parser, "true", JSONValue::True);
|
|
|
|
assert_eq!(res, Ok(Some(JSONValue::True)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn skip_whitespace() {
|
|
|
|
let mut parser = JSON::new(" \t\r\nx");
|
|
|
|
parser.skip_whitespace();
|
|
|
|
assert_eq!('x', parser.chars[parser.i]);
|
|
|
|
}
|
|
|
|
|
2020-02-07 22:55:09 -05:00
|
|
|
#[test]
|
|
|
|
fn parse_string() {
|
|
|
|
let mut parser = JSON::new(r#""\t""#);
|
|
|
|
let res = JSON::parse_string(&mut parser);
|
|
|
|
assert_eq!(res, Ok(Some(JSONValue::String(String::from("\t")))));
|
|
|
|
|
|
|
|
let mut parser = JSON::new(r#""\u203d""#);
|
|
|
|
let res = JSON::parse_string(&mut parser);
|
|
|
|
assert_eq!(res, Ok(Some(JSONValue::String(String::from("‽")))));
|
|
|
|
}
|
|
|
|
|
2020-02-07 20:01:10 -05:00
|
|
|
#[test]
|
|
|
|
fn parse_empty_array() {
|
|
|
|
let mut parser = JSON::new("[]");
|
|
|
|
let res = JSON::parse_value(&mut parser);
|
|
|
|
assert_eq!(res, Ok(JSONValue::Array(vec![])));
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:51:49 -05:00
|
|
|
#[test]
|
|
|
|
fn parse_number() {
|
2020-02-10 12:26:54 -05:00
|
|
|
let mut parser = JSON::new(r#""foo""#);
|
|
|
|
let res = JSON::parse_number(&mut parser);
|
|
|
|
assert_eq!(res, Ok(None));
|
|
|
|
|
2020-02-07 23:51:49 -05:00
|
|
|
let mut parser = JSON::new("3.14159");
|
|
|
|
let res = JSON::parse_number(&mut parser);
|
|
|
|
assert_eq!(res, Ok(Some(JSONValue::Number(3.14159f64))));
|
|
|
|
|
|
|
|
let mut parser = JSON::new("3e4");
|
|
|
|
let res = JSON::parse_number(&mut parser);
|
|
|
|
assert_eq!(res, Ok(Some(JSONValue::Number(3e4f64))));
|
|
|
|
}
|
|
|
|
|
2020-02-07 20:01:10 -05:00
|
|
|
#[test]
|
|
|
|
fn can_parse_array_of_keywords() {
|
|
|
|
let result = JSON::parse("[true,false,null]");
|
|
|
|
|
2020-02-07 21:27:46 -05:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
Ok(JSONValue::Array(vec![
|
|
|
|
JSONValue::True,
|
|
|
|
JSONValue::False,
|
|
|
|
JSONValue::Null
|
|
|
|
]))
|
|
|
|
);
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|
2020-02-07 23:51:49 -05:00
|
|
|
|
2020-02-10 12:26:54 -05:00
|
|
|
#[test]
|
|
|
|
fn parse_json_types() {
|
|
|
|
// Boolean / Null
|
|
|
|
let res = JSON::parse("true");
|
|
|
|
assert_eq!(res, Ok(JSONValue::True));
|
|
|
|
let res = JSON::parse("false");
|
|
|
|
assert_eq!(res, Ok(JSONValue::False));
|
|
|
|
let res = JSON::parse("null");
|
|
|
|
assert_eq!(res, Ok(JSONValue::Null));
|
|
|
|
|
|
|
|
// Number
|
|
|
|
let res = JSON::parse("9.38083151965");
|
|
|
|
assert_eq!(res, Ok(JSONValue::Number(9.38083151965)), "Failed to parse number");
|
|
|
|
|
|
|
|
// String
|
|
|
|
let res = JSON::parse(r#""/^$/""#);
|
|
|
|
assert_eq!(res, Ok(JSONValue::String(String::from("/^$/"))), "Failed to parse string");
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:51:49 -05:00
|
|
|
#[test]
|
|
|
|
fn can_parse_arbitrary_json() {
|
|
|
|
let result = JSON::parse(
|
|
|
|
r#"[{
|
|
|
|
"a": 9.38083151965,
|
|
|
|
"b": 4e3,
|
|
|
|
"c": [1, 2, 3],
|
|
|
|
"d": "foo",
|
|
|
|
"e": {
|
|
|
|
"f": {
|
|
|
|
"g": {
|
|
|
|
"h": null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"i": ["\"", "\\", "/", "\b", "\f", "\n", "\r", "\t", "\u0001", "\uface"]
|
|
|
|
}]"#,
|
|
|
|
);
|
|
|
|
// let msg = format!("{:#?}", result);
|
|
|
|
assert!(result.is_ok(), format!("{:#?}", result));
|
|
|
|
}
|
2020-02-07 16:23:51 -05:00
|
|
|
}
|