Preserve order when serializing/deserialize json by default. (#3126)

This commit is contained in:
Andrés N. Robalino 2021-03-04 00:35:13 -06:00 committed by GitHub
parent 9c375b33a6
commit 0b71e45072
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
118 changed files with 2487 additions and 1322 deletions

21
Cargo.lock generated
View File

@ -2771,7 +2771,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd" checksum = "6d262045c5b87c0861b3f004610afd0e2c851e2908d08b6c870cbb9d5f494ecd"
dependencies = [ dependencies = [
"serde 0.8.23", "serde 0.8.23",
"serde_test", "serde_test 0.8.23",
] ]
[[package]] [[package]]
@ -2779,6 +2779,10 @@ name = "linked-hash-map"
version = "0.5.4" version = "0.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3"
dependencies = [
"serde 1.0.123",
"serde_test 1.0.123",
]
[[package]] [[package]]
name = "lock_api" name = "lock_api"
@ -3496,10 +3500,14 @@ dependencies = [
name = "nu-json" name = "nu-json"
version = "0.27.2" version = "0.27.2"
dependencies = [ dependencies = [
"dunce",
"lazy_static 1.4.0", "lazy_static 1.4.0",
"linked-hash-map 0.5.4",
"nu-test-support",
"num-traits 0.2.14", "num-traits 0.2.14",
"regex 1.4.3", "regex 1.4.3",
"serde 0.8.23", "serde 1.0.123",
"serde_json",
] ]
[[package]] [[package]]
@ -5386,6 +5394,15 @@ dependencies = [
"serde 0.8.23", "serde 0.8.23",
] ]
[[package]]
name = "serde_test"
version = "1.0.123"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38145a8510bdf71d9a8cceeb57664049538446e77f24648328bdbcf22dc7e169"
dependencies = [
"serde 1.0.123",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.6.1" version = "0.6.1"

View File

@ -8,8 +8,18 @@ version = "0.27.2"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
preserve_order = ["linked-hash-map", "linked-hash-map/serde_impl"]
default = ["preserve_order"]
[dependencies] [dependencies]
lazy_static = "1" serde = "1.0"
num-traits = "0.2.14" num-traits = "0.2.14"
regex = "^1.0" regex = "^1.0"
serde = "^0.8.0" lazy_static = "1"
linked-hash-map = { version = "0.5", optional = true }
[dev-dependencies]
nu-test-support = { version = "0.27.2", path = "../nu-test-support" }
serde_json = "1.0.39"
dunce = "1.0.1"

View File

@ -1,13 +1,3 @@
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use serde::ser; use serde::ser;
use crate::value::{self, Map, Value}; use crate::value::{self, Map, Value};
@ -36,7 +26,8 @@ impl ArrayBuilder {
/// Insert a value into the array. /// Insert a value into the array.
pub fn push<T: ser::Serialize>(mut self, v: T) -> ArrayBuilder { pub fn push<T: ser::Serialize>(mut self, v: T) -> ArrayBuilder {
self.array.push(value::to_value(&v)); self.array
.push(value::to_value(&v).expect("failed to serialize"));
self self
} }
@ -91,7 +82,10 @@ impl ObjectBuilder {
S: Into<String>, S: Into<String>,
V: ser::Serialize, V: ser::Serialize,
{ {
self.object.insert(key.into(), value::to_value(&value)); self.object.insert(
key.into(),
value::to_value(&value).expect("failed to serialize"),
);
self self
} }

View File

@ -74,9 +74,9 @@ where
matches!(ch, b'{' | b'}' | b'[' | b']' | b',' | b':') matches!(ch, b'{' | b'}' | b'[' | b']' | b',' | b':')
} }
fn parse_keyname<V>(&mut self, mut visitor: V) -> Result<V::Value> fn parse_keyname<'de, V>(&mut self, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
// quotes for keys are optional in Hjson // quotes for keys are optional in Hjson
// unless they include {}[],: or whitespace. // unless they include {}[],: or whitespace.
@ -117,9 +117,9 @@ where
} }
} }
fn parse_value<V>(&mut self, mut visitor: V) -> Result<V::Value> fn parse_value<'de, V>(&mut self, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
self.rdr.parse_whitespace()?; self.rdr.parse_whitespace()?;
@ -134,18 +134,18 @@ where
} }
State::Root => { State::Root => {
self.state = State::Normal; self.state = State::Normal;
return visitor.visit_map(MapVisitor::new(self, true)); return self.visit_map(true, visitor);
} }
_ => {} _ => {}
} }
let value = match self.rdr.peek_or_null()? { match self.rdr.peek_or_null()? {
/* /*
b'-' => { b'-' => {
self.rdr.eat_char(); self.rdr.eat_char();
self.parse_integer(false, visitor) self.parse_integer(false, visitor)
} }
b'0' ..= b'9' => { b'0' ... b'9' => {
self.parse_integer(true, visitor) self.parse_integer(true, visitor)
} }
*/ */
@ -157,20 +157,45 @@ where
} }
b'[' => { b'[' => {
self.rdr.eat_char(); self.rdr.eat_char();
visitor.visit_seq(SeqVisitor::new(self)) let ret = visitor.visit_seq(SeqVisitor::new(self))?;
self.rdr.parse_whitespace()?;
match self.rdr.next_char()? {
Some(b']') => Ok(ret),
Some(_) => Err(self.rdr.error(ErrorCode::TrailingCharacters)),
None => Err(self.rdr.error(ErrorCode::EOFWhileParsingList)),
}
} }
b'{' => { b'{' => {
self.rdr.eat_char(); self.rdr.eat_char();
visitor.visit_map(MapVisitor::new(self, false)) self.visit_map(false, visitor)
} }
b'\x00' => Err(self.rdr.error(ErrorCode::ExpectedSomeValue)), b'\x00' => Err(self.rdr.error(ErrorCode::ExpectedSomeValue)),
_ => self.parse_tfnns(visitor), _ => self.parse_tfnns(visitor),
}; }
}
match value { fn visit_map<'de, V>(&mut self, root: bool, visitor: V) -> Result<V::Value>
Ok(value) => Ok(value), where
Err(Error::Syntax(code, _, _)) => Err(self.rdr.error(code)), V: de::Visitor<'de>,
Err(err) => Err(err), {
let ret = visitor.visit_map(MapVisitor::new(self, root))?;
self.rdr.parse_whitespace()?;
match self.rdr.next_char()? {
Some(b'}') => {
if !root {
Ok(ret)
} else {
Err(self.rdr.error(ErrorCode::TrailingCharacters))
} // todo
}
Some(_) => Err(self.rdr.error(ErrorCode::TrailingCharacters)),
None => {
if root {
Ok(ret)
} else {
Err(self.rdr.error(ErrorCode::EOFWhileParsingObject))
}
}
} }
} }
@ -184,9 +209,9 @@ where
Ok(()) Ok(())
} }
fn parse_tfnns<V>(&mut self, mut visitor: V) -> Result<V::Value> fn parse_tfnns<'de, V>(&mut self, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
// Hjson strings can be quoteless // Hjson strings can be quoteless
// returns string, true, false, or null. // returns string, true, false, or null.
@ -244,7 +269,7 @@ where
} }
_ => { _ => {
if chf == b'-' || (b'0'..=b'9').contains(&chf) { if chf == b'-' || (b'0'..=b'9').contains(&chf) {
let mut pn = ParseNumber::new(self.str_buf.iter().cloned()); let mut pn = ParseNumber::new(self.str_buf.iter().copied());
match pn.parse(false) { match pn.parse(false) {
Ok(Number::F64(v)) => { Ok(Number::F64(v)) => {
self.rdr.uneat_char(ch); self.rdr.uneat_char(ch);
@ -274,7 +299,7 @@ where
} }
self.str_buf.push(ch); self.str_buf.push(ch);
if self.str_buf == vec![b'\''; 3] { if self.str_buf == b"'''" {
return self.parse_ml_string(visitor); return self.parse_ml_string(visitor);
} }
} }
@ -283,7 +308,7 @@ where
fn decode_hex_escape(&mut self) -> Result<u16> { fn decode_hex_escape(&mut self) -> Result<u16> {
let mut i = 0; let mut i = 0;
let mut n = 0u16; let mut n = 0u16;
while i < 4 && !(self.rdr.eof()?) { while i < 4 && !self.rdr.eof()? {
n = match self.rdr.next_char_or_null()? { n = match self.rdr.next_char_or_null()? {
c @ b'0'..=b'9' => n * 16_u16 + ((c as u16) - (b'0' as u16)), c @ b'0'..=b'9' => n * 16_u16 + ((c as u16) - (b'0' as u16)),
b'a' | b'A' => n * 16_u16 + 10_u16, b'a' | b'A' => n * 16_u16 + 10_u16,
@ -326,9 +351,9 @@ where
Ok(()) Ok(())
} }
fn parse_ml_string<V>(&mut self, mut visitor: V) -> Result<V::Value> fn parse_ml_string<'de, V>(&mut self, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
self.str_buf.clear(); self.str_buf.clear();
@ -464,11 +489,7 @@ where
}, },
}; };
// FIXME: this allocation is required in order to be compatible with stable self.str_buf.extend(c.encode_utf8(&mut [0; 4]).as_bytes());
// rust, which doesn't support encoding a `char` into a stack buffer.
let mut buf = String::new();
buf.push(c);
self.str_buf.extend(buf.bytes());
} }
_ => { _ => {
return Err(self.rdr.error(ErrorCode::InvalidEscape)); return Err(self.rdr.error(ErrorCode::InvalidEscape));
@ -493,25 +514,27 @@ where
} }
} }
impl<Iter> de::Deserializer for Deserializer<Iter> impl<'de, 'a, Iter> de::Deserializer<'de> for &'a mut Deserializer<Iter>
where where
Iter: Iterator<Item = u8>, Iter: Iterator<Item = u8>,
{ {
type Error = Error; type Error = Error;
#[inline] #[inline]
fn deserialize<V>(&mut self, visitor: V) -> Result<V::Value> fn deserialize_any<V>(self, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
if let State::Root = self.state {}
self.parse_value(visitor) self.parse_value(visitor)
} }
/// Parses a `null` as a None, and any other values as a `Some(...)`. /// Parses a `null` as a None, and any other values as a `Some(...)`.
#[inline] #[inline]
fn deserialize_option<V>(&mut self, mut visitor: V) -> Result<V::Value> fn deserialize_option<V>(self, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
self.rdr.parse_whitespace()?; self.rdr.parse_whitespace()?;
@ -527,42 +550,17 @@ where
/// Parses a newtype struct as the underlying value. /// Parses a newtype struct as the underlying value.
#[inline] #[inline]
fn deserialize_newtype_struct<V>(&mut self, _name: &str, mut visitor: V) -> Result<V::Value> fn deserialize_newtype_struct<V>(self, _name: &str, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
visitor.visit_newtype_struct(self) visitor.visit_newtype_struct(self)
} }
forward_to_deserialize! { serde::forward_to_deserialize_any! {
deserialize_bool(); bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 f64 char str string
deserialize_usize(); bytes byte_buf unit unit_struct seq tuple map
deserialize_u8(); tuple_struct struct enum identifier ignored_any
deserialize_u16();
deserialize_u32();
deserialize_u64();
deserialize_isize();
deserialize_i8();
deserialize_i16();
deserialize_i32();
deserialize_i64();
deserialize_f32();
deserialize_f64();
deserialize_char();
deserialize_str();
deserialize_string();
deserialize_unit();
deserialize_seq();
deserialize_seq_fixed_size(len: usize);
deserialize_bytes();
deserialize_map();
deserialize_unit_struct(name: &'static str);
deserialize_tuple_struct(name: &'static str, len: usize);
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
deserialize_struct_field();
deserialize_tuple(len: usize);
deserialize_enum(name: &'static str, variants: &'static [&'static str]);
deserialize_ignored_any();
} }
} }
@ -576,15 +574,15 @@ impl<'a, Iter: Iterator<Item = u8>> SeqVisitor<'a, Iter> {
} }
} }
impl<'a, Iter> de::SeqVisitor for SeqVisitor<'a, Iter> impl<'de, 'a, Iter> de::SeqAccess<'de> for SeqVisitor<'a, Iter>
where where
Iter: Iterator<Item = u8>, Iter: Iterator<Item = u8>,
{ {
type Error = Error; type Error = Error;
fn visit<T>(&mut self) -> Result<Option<T>> fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where where
T: de::Deserialize, T: de::DeserializeSeed<'de>,
{ {
self.de.rdr.parse_whitespace()?; self.de.rdr.parse_whitespace()?;
@ -598,7 +596,7 @@ where
} }
} }
let value = de::Deserialize::deserialize(self.de)?; let value = seed.deserialize(&mut *self.de)?;
// in Hjson the comma is optional and trailing commas are allowed // in Hjson the comma is optional and trailing commas are allowed
self.de.rdr.parse_whitespace()?; self.de.rdr.parse_whitespace()?;
@ -609,16 +607,6 @@ where
Ok(Some(value)) Ok(Some(value))
} }
fn end(&mut self) -> Result<()> {
self.de.rdr.parse_whitespace()?;
match self.de.rdr.next_char()? {
Some(b']') => Ok(()),
Some(_) => Err(self.de.rdr.error(ErrorCode::TrailingCharacters)),
None => Err(self.de.rdr.error(ErrorCode::EOFWhileParsingList)),
}
}
} }
struct MapVisitor<'a, Iter: 'a + Iterator<Item = u8>> { struct MapVisitor<'a, Iter: 'a + Iterator<Item = u8>> {
@ -637,15 +625,15 @@ impl<'a, Iter: Iterator<Item = u8>> MapVisitor<'a, Iter> {
} }
} }
impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter> impl<'de, 'a, Iter> de::MapAccess<'de> for MapVisitor<'a, Iter>
where where
Iter: Iterator<Item = u8>, Iter: Iterator<Item = u8>,
{ {
type Error = Error; type Error = Error;
fn visit_key<K>(&mut self) -> Result<Option<K>> fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
where where
K: de::Deserialize, K: de::DeserializeSeed<'de>,
{ {
self.de.rdr.parse_whitespace()?; self.de.rdr.parse_whitespace()?;
@ -676,146 +664,51 @@ where
} else { } else {
State::Keyname State::Keyname
}; };
Ok(Some(de::Deserialize::deserialize(self.de)?)) Ok(Some(seed.deserialize(&mut *self.de)?))
} }
None => Err(self.de.rdr.error(ErrorCode::EOFWhileParsingValue)), None => Err(self.de.rdr.error(ErrorCode::EOFWhileParsingValue)),
} }
} }
fn visit_value<V>(&mut self) -> Result<V> fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value>
where where
V: de::Deserialize, V: de::DeserializeSeed<'de>,
{ {
self.de.parse_object_colon()?; self.de.parse_object_colon()?;
de::Deserialize::deserialize(self.de) Ok(seed.deserialize(&mut *self.de)?)
}
fn end(&mut self) -> Result<()> {
self.de.rdr.parse_whitespace()?;
match self.de.rdr.next_char()? {
Some(b'}') => {
if !self.root {
Ok(())
} else {
Err(self.de.rdr.error(ErrorCode::TrailingCharacters))
} // todo
}
Some(_) => Err(self.de.rdr.error(ErrorCode::TrailingCharacters)),
None => {
if self.root {
Ok(())
} else {
Err(self.de.rdr.error(ErrorCode::EOFWhileParsingObject))
}
}
}
}
fn missing_field<V>(&mut self, field: &'static str) -> Result<V>
where
V: de::Deserialize,
{
struct MissingFieldDeserializer(&'static str);
impl de::Deserializer for MissingFieldDeserializer {
type Error = de::value::Error;
fn deserialize<V>(&mut self, _visitor: V) -> std::result::Result<V::Value, Self::Error>
where
V: de::Visitor,
{
let &mut MissingFieldDeserializer(field) = self;
Err(de::value::Error::MissingField(field))
}
fn deserialize_option<V>(
&mut self,
mut visitor: V,
) -> std::result::Result<V::Value, Self::Error>
where
V: de::Visitor,
{
visitor.visit_none()
}
forward_to_deserialize! {
deserialize_bool();
deserialize_usize();
deserialize_u8();
deserialize_u16();
deserialize_u32();
deserialize_u64();
deserialize_isize();
deserialize_i8();
deserialize_i16();
deserialize_i32();
deserialize_i64();
deserialize_f32();
deserialize_f64();
deserialize_char();
deserialize_str();
deserialize_string();
deserialize_unit();
deserialize_seq();
deserialize_seq_fixed_size(len: usize);
deserialize_bytes();
deserialize_map();
deserialize_unit_struct(name: &'static str);
deserialize_newtype_struct(name: &'static str);
deserialize_tuple_struct(name: &'static str, len: usize);
deserialize_struct(name: &'static str, fields: &'static [&'static str]);
deserialize_struct_field();
deserialize_tuple(len: usize);
deserialize_enum(name: &'static str, variants: &'static [&'static str]);
deserialize_ignored_any();
}
}
let mut de = MissingFieldDeserializer(field);
Ok(de::Deserialize::deserialize(&mut de)?)
} }
} }
impl<Iter> de::VariantVisitor for Deserializer<Iter> impl<'de, 'a, Iter> de::VariantAccess<'de> for &'a mut Deserializer<Iter>
where where
Iter: Iterator<Item = u8>, Iter: Iterator<Item = u8>,
{ {
type Error = Error; type Error = Error;
fn visit_variant<V>(&mut self) -> Result<V> fn unit_variant(self) -> Result<()> {
where
V: de::Deserialize,
{
let val = de::Deserialize::deserialize(self)?;
self.parse_object_colon()?;
Ok(val)
}
fn visit_unit(&mut self) -> Result<()> {
de::Deserialize::deserialize(self) de::Deserialize::deserialize(self)
} }
fn visit_newtype<T>(&mut self) -> Result<T> fn newtype_variant_seed<T>(self, seed: T) -> Result<T::Value>
where where
T: de::Deserialize, T: de::DeserializeSeed<'de>,
{ {
de::Deserialize::deserialize(self) seed.deserialize(self)
} }
fn visit_tuple<V>(&mut self, _len: usize, visitor: V) -> Result<V::Value> fn tuple_variant<V>(self, _len: usize, visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
de::Deserializer::deserialize(self, visitor) de::Deserializer::deserialize_any(self, visitor)
} }
fn visit_struct<V>(&mut self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value> fn struct_variant<V>(self, _fields: &'static [&'static str], visitor: V) -> Result<V::Value>
where where
V: de::Visitor, V: de::Visitor<'de>,
{ {
de::Deserializer::deserialize(self, visitor) de::Deserializer::deserialize_any(self, visitor)
} }
} }
@ -825,7 +718,7 @@ where
pub struct StreamDeserializer<T, Iter> pub struct StreamDeserializer<T, Iter>
where where
Iter: Iterator<Item = u8>, Iter: Iterator<Item = u8>,
T: de::Deserialize, T: de::DeserializeOwned,
{ {
deser: Deserializer<Iter>, deser: Deserializer<Iter>,
_marker: PhantomData<T>, _marker: PhantomData<T>,
@ -834,7 +727,7 @@ where
impl<T, Iter> StreamDeserializer<T, Iter> impl<T, Iter> StreamDeserializer<T, Iter>
where where
Iter: Iterator<Item = u8>, Iter: Iterator<Item = u8>,
T: de::Deserialize, T: de::DeserializeOwned,
{ {
/// Returns an `Iterator` of decoded Hjson values from an iterator over /// Returns an `Iterator` of decoded Hjson values from an iterator over
/// `Iterator<Item=u8>`. /// `Iterator<Item=u8>`.
@ -849,7 +742,7 @@ where
impl<T, Iter> Iterator for StreamDeserializer<T, Iter> impl<T, Iter> Iterator for StreamDeserializer<T, Iter>
where where
Iter: Iterator<Item = u8>, Iter: Iterator<Item = u8>,
T: de::Deserialize, T: de::DeserializeOwned,
{ {
type Item = Result<T>; type Item = Result<T>;
@ -879,9 +772,10 @@ where
pub fn from_iter<I, T>(iter: I) -> Result<T> pub fn from_iter<I, T>(iter: I) -> Result<T>
where where
I: Iterator<Item = io::Result<u8>>, I: Iterator<Item = io::Result<u8>>,
T: de::Deserialize, T: de::DeserializeOwned,
{ {
let fold: io::Result<Vec<_>> = iter.collect(); let fold: io::Result<Vec<_>> = iter.collect();
if let Err(e) = fold { if let Err(e) = fold {
return Err(Error::Io(e)); return Err(Error::Io(e));
} }
@ -893,43 +787,31 @@ where
// todo: add compile switch // todo: add compile switch
// deserialize and make sure the whole stream has been consumed // deserialize and make sure the whole stream has been consumed
let mut de = Deserializer::new_for_root(bytes.iter().cloned()); let mut de = Deserializer::new_for_root(bytes.iter().copied());
let value = match de::Deserialize::deserialize(&mut de).and_then(|x| { de::Deserialize::deserialize(&mut de)
de.end()?; .and_then(|x| de.end().map(|()| x))
Ok(x) .or_else(|_| {
}) { let mut de2 = Deserializer::new(bytes.iter().copied());
Ok(v) => Ok(v), de::Deserialize::deserialize(&mut de2).and_then(|x| de2.end().map(|()| x))
Err(_) => { })
let mut de2 = Deserializer::new(bytes.iter().cloned());
match de::Deserialize::deserialize(&mut de2).and_then(|x| {
de2.end()?;
Ok(x)
}) {
Ok(v) => Ok(v),
Err(e) => Err(e),
}
}
};
/* without legacy support: /* without legacy support:
// deserialize and make sure the whole stream has been consumed // deserialize and make sure the whole stream has been consumed
let mut de = Deserializer::new(bytes.iter().map(|b| *b)); let mut de = Deserializer::new(bytes.iter().map(|b| *b));
let value = match de::Deserialize::deserialize(&mut de) let value = match de::Deserialize::deserialize(&mut de)
.and_then(|x| { de.end()); Ok(x) }) .and_then(|x| { try!(de.end()); Ok(x) })
{ {
Ok(v) => Ok(v), Ok(v) => Ok(v),
Err(e) => Err(e), Err(e) => Err(e),
}; };
*/ */
value
} }
/// Decodes a Hjson value from a `std::io::Read`. /// Decodes a Hjson value from a `std::io::Read`.
pub fn from_reader<R, T>(rdr: R) -> Result<T> pub fn from_reader<R, T>(rdr: R) -> Result<T>
where where
R: io::Read, R: io::Read,
T: de::Deserialize, T: de::DeserializeOwned,
{ {
from_iter(rdr.bytes()) from_iter(rdr.bytes())
} }
@ -937,7 +819,7 @@ where
/// Decodes a Hjson value from a byte slice `&[u8]`. /// Decodes a Hjson value from a byte slice `&[u8]`.
pub fn from_slice<T>(v: &[u8]) -> Result<T> pub fn from_slice<T>(v: &[u8]) -> Result<T>
where where
T: de::Deserialize, T: de::DeserializeOwned,
{ {
from_iter(v.iter().map(|byte| Ok(*byte))) from_iter(v.iter().map(|byte| Ok(*byte)))
} }
@ -945,7 +827,7 @@ where
/// Decodes a Hjson value from a `&str`. /// Decodes a Hjson value from a `&str`.
pub fn from_str<T>(s: &str) -> Result<T> pub fn from_str<T>(s: &str) -> Result<T>
where where
T: de::Deserialize, T: de::DeserializeOwned,
{ {
from_slice(s.as_bytes()) from_slice(s.as_bytes())
} }

View File

@ -18,24 +18,6 @@ pub enum ErrorCode {
/// Catchall for syntax error messages /// Catchall for syntax error messages
Custom(String), Custom(String),
/// Incorrect type from value
InvalidType(de::Type),
/// Incorrect value
InvalidValue(String),
/// Invalid length
InvalidLength(usize),
/// Unknown variant in an enum.
UnknownVariant(String),
/// Unknown field in struct.
UnknownField(String),
/// Struct is missing a field.
MissingField(&'static str),
/// EOF while parsing a list. /// EOF while parsing a list.
EOFWhileParsingList, EOFWhileParsingList,
@ -94,12 +76,6 @@ impl fmt::Debug for ErrorCode {
match *self { match *self {
ErrorCode::Custom(ref msg) => write!(f, "{}", msg), ErrorCode::Custom(ref msg) => write!(f, "{}", msg),
ErrorCode::InvalidType(ref ty) => write!(f, "invalid type: {:?}", ty),
ErrorCode::InvalidValue(ref msg) => write!(f, "invalid value: {}", msg),
ErrorCode::InvalidLength(ref len) => write!(f, "invalid value length {}", len),
ErrorCode::UnknownVariant(ref variant) => write!(f, "unknown variant \"{}\"", variant),
ErrorCode::UnknownField(ref field) => write!(f, "unknown field \"{}\"", field),
ErrorCode::MissingField(ref field) => write!(f, "missing field \"{}\"", field),
ErrorCode::EOFWhileParsingList => "EOF while parsing a list".fmt(f), ErrorCode::EOFWhileParsingList => "EOF while parsing a list".fmt(f),
ErrorCode::EOFWhileParsingObject => "EOF while parsing an object".fmt(f), ErrorCode::EOFWhileParsingObject => "EOF while parsing an object".fmt(f),
ErrorCode::EOFWhileParsingString => "EOF while parsing a string".fmt(f), ErrorCode::EOFWhileParsingString => "EOF while parsing a string".fmt(f),
@ -173,69 +149,16 @@ impl From<FromUtf8Error> for Error {
} }
} }
impl From<de::value::Error> for Error {
fn from(error: de::value::Error) -> Error {
match error {
de::value::Error::Custom(e) => Error::Syntax(ErrorCode::Custom(e), 0, 0),
de::value::Error::EndOfStream => de::Error::end_of_stream(),
de::value::Error::InvalidType(ty) => Error::Syntax(ErrorCode::InvalidType(ty), 0, 0),
de::value::Error::InvalidValue(msg) => {
Error::Syntax(ErrorCode::InvalidValue(msg), 0, 0)
}
de::value::Error::InvalidLength(len) => {
Error::Syntax(ErrorCode::InvalidLength(len), 0, 0)
}
de::value::Error::UnknownVariant(variant) => {
Error::Syntax(ErrorCode::UnknownVariant(variant), 0, 0)
}
de::value::Error::UnknownField(field) => {
Error::Syntax(ErrorCode::UnknownField(field), 0, 0)
}
de::value::Error::MissingField(field) => {
Error::Syntax(ErrorCode::MissingField(field), 0, 0)
}
}
}
}
impl de::Error for Error { impl de::Error for Error {
fn custom<T: Into<String>>(msg: T) -> Error { fn custom<T: fmt::Display>(msg: T) -> Error {
Error::Syntax(ErrorCode::Custom(msg.into()), 0, 0) Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
}
fn end_of_stream() -> Error {
Error::Syntax(ErrorCode::EOFWhileParsingValue, 0, 0)
}
fn invalid_type(ty: de::Type) -> Error {
Error::Syntax(ErrorCode::InvalidType(ty), 0, 0)
}
fn invalid_value(msg: &str) -> Error {
Error::Syntax(ErrorCode::InvalidValue(msg.to_owned()), 0, 0)
}
fn invalid_length(len: usize) -> Error {
Error::Syntax(ErrorCode::InvalidLength(len), 0, 0)
}
fn unknown_variant(variant: &str) -> Error {
Error::Syntax(ErrorCode::UnknownVariant(String::from(variant)), 0, 0)
}
fn unknown_field(field: &str) -> Error {
Error::Syntax(ErrorCode::UnknownField(String::from(field)), 0, 0)
}
fn missing_field(field: &'static str) -> Error {
Error::Syntax(ErrorCode::MissingField(field), 0, 0)
} }
} }
impl ser::Error for Error { impl ser::Error for Error {
/// Raised when there is general error when deserializing a type. /// Raised when there is general error when deserializing a type.
fn custom<T: Into<String>>(msg: T) -> Error { fn custom<T: fmt::Display>(msg: T) -> Error {
Error::Syntax(ErrorCode::Custom(msg.into()), 0, 0) Error::Syntax(ErrorCode::Custom(msg.to_string()), 0, 0)
} }
} }

View File

@ -1,38 +0,0 @@
#[macro_export]
/// Create a function to forward a specific serialize call to the generic deserialize
macro_rules! forward_to_deserialize {
($(
$name:ident ( $( $arg:ident : $ty:ty ),* );
)*) => {
$(
forward_to_deserialize!{
func: $name ( $( $arg: $ty ),* );
}
)*
};
(func: deserialize_enum ( $( $arg:ident : $ty:ty ),* );) => {
fn deserialize_enum<V>(
&mut self,
$(_: $ty,)*
_visitor: V,
) -> ::std::result::Result<V::Value, Self::Error>
where V: ::serde::de::EnumVisitor
{
Err(::serde::de::Error::invalid_type(::serde::de::Type::Enum))
}
};
(func: $name:ident ( $( $arg:ident : $ty:ty ),* );) => {
#[inline]
fn $name<V>(
&mut self,
$(_: $ty,)*
visitor: V,
) -> ::std::result::Result<V::Value, Self::Error>
where V: ::serde::de::Visitor
{
self.deserialize(visitor)
}
};
}

View File

@ -5,9 +5,6 @@ pub use self::error::{Error, ErrorCode, Result};
pub use self::ser::{to_string, to_vec, to_writer, Serializer}; pub use self::ser::{to_string, to_vec, to_writer, Serializer};
pub use self::value::{from_value, to_value, Map, Value}; pub use self::value::{from_value, to_value, Map, Value};
#[macro_use]
mod forward;
pub mod builder; pub mod builder;
pub mod de; pub mod de;
pub mod error; pub mod error;

View File

@ -59,23 +59,30 @@ pub enum State {
Rest, Rest,
} }
impl<W, F> ser::Serializer for Serializer<W, F> #[doc(hidden)]
pub struct Compound<'a, W, F> {
ser: &'a mut Serializer<W, F>,
state: State,
}
impl<'a, W, F> ser::Serializer for &'a mut Serializer<W, F>
where where
W: io::Write, W: io::Write,
F: Formatter, F: Formatter,
{ {
type Ok = ();
type Error = Error; type Error = Error;
type SeqState = State; type SerializeSeq = Compound<'a, W, F>;
type TupleState = State; type SerializeTuple = Compound<'a, W, F>;
type TupleStructState = State; type SerializeTupleStruct = Compound<'a, W, F>;
type TupleVariantState = State; type SerializeTupleVariant = Compound<'a, W, F>;
type MapState = State; type SerializeMap = Compound<'a, W, F>;
type StructState = State; type SerializeStruct = Compound<'a, W, F>;
type StructVariantState = State; type SerializeStructVariant = Compound<'a, W, F>;
#[inline] #[inline]
fn serialize_bool(&mut self, value: bool) -> Result<()> { fn serialize_bool(self, value: bool) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
if value { if value {
self.writer.write_all(b"true").map_err(From::from) self.writer.write_all(b"true").map_err(From::from)
@ -85,115 +92,103 @@ where
} }
#[inline] #[inline]
fn serialize_isize(&mut self, value: isize) -> Result<()> { fn serialize_i8(self, value: i8) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_i8(&mut self, value: i8) -> Result<()> { fn serialize_i16(self, value: i16) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_i16(&mut self, value: i16) -> Result<()> { fn serialize_i32(self, value: i32) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_i32(&mut self, value: i32) -> Result<()> { fn serialize_i64(self, value: i64) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_i64(&mut self, value: i64) -> Result<()> { fn serialize_u8(self, value: u8) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_usize(&mut self, value: usize) -> Result<()> { fn serialize_u16(self, value: u16) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_u8(&mut self, value: u8) -> Result<()> { fn serialize_u32(self, value: u32) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_u16(&mut self, value: u16) -> Result<()> { fn serialize_u64(self, value: u64) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from) write!(&mut self.writer, "{}", value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_u32(&mut self, value: u32) -> Result<()> { fn serialize_f32(self, value: f32) -> Result<()> {
self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from)
}
#[inline]
fn serialize_u64(&mut self, value: u64) -> Result<()> {
self.formatter.start_value(&mut self.writer)?;
write!(&mut self.writer, "{}", value).map_err(From::from)
}
#[inline]
fn serialize_f32(&mut self, value: f32) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
fmt_f32_or_null(&mut self.writer, if value == -0f32 { 0f32 } else { value }) fmt_f32_or_null(&mut self.writer, if value == -0f32 { 0f32 } else { value })
.map_err(From::from) .map_err(From::from)
} }
#[inline] #[inline]
fn serialize_f64(&mut self, value: f64) -> Result<()> { fn serialize_f64(self, value: f64) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
fmt_f64_or_null(&mut self.writer, if value == -0f64 { 0f64 } else { value }) fmt_f64_or_null(&mut self.writer, if value == -0f64 { 0f64 } else { value })
.map_err(From::from) .map_err(From::from)
} }
#[inline] #[inline]
fn serialize_char(&mut self, value: char) -> Result<()> { fn serialize_char(self, value: char) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
escape_char(&mut self.writer, value).map_err(From::from) escape_char(&mut self.writer, value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_str(&mut self, value: &str) -> Result<()> { fn serialize_str(self, value: &str) -> Result<()> {
quote_str(&mut self.writer, &mut self.formatter, value).map_err(From::from) quote_str(&mut self.writer, &mut self.formatter, value).map_err(From::from)
} }
#[inline] #[inline]
fn serialize_bytes(&mut self, value: &[u8]) -> Result<()> { fn serialize_bytes(self, value: &[u8]) -> Result<()> {
let mut state = self.serialize_seq(Some(value.len()))?; let mut seq = self.serialize_seq(Some(value.len()))?;
for byte in value { for byte in value {
self.serialize_seq_elt(&mut state, byte)?; ser::SerializeSeq::serialize_element(&mut seq, byte)?
} }
self.serialize_seq_end(state) ser::SerializeSeq::end(seq)
} }
#[inline] #[inline]
fn serialize_unit(&mut self) -> Result<()> { fn serialize_unit(self) -> Result<()> {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
self.writer.write_all(b"null").map_err(From::from) self.writer.write_all(b"null").map_err(From::from)
} }
#[inline] #[inline]
fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<()> { fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
self.serialize_unit() self.serialize_unit()
} }
#[inline] #[inline]
fn serialize_unit_variant( fn serialize_unit_variant(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
variant: &'static str, variant: &'static str,
) -> Result<()> { ) -> Result<()> {
self.serialize_str(variant) self.serialize_str(variant)
@ -201,127 +196,80 @@ where
/// Serialize newtypes without an object wrapper. /// Serialize newtypes without an object wrapper.
#[inline] #[inline]
fn serialize_newtype_struct<T>(&mut self, _name: &'static str, value: T) -> Result<()> fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> Result<()>
where where
T: ser::Serialize, T: ?Sized + ser::Serialize,
{ {
value.serialize(self) value.serialize(self)
} }
#[inline] #[inline]
fn serialize_newtype_variant<T>( fn serialize_newtype_variant<T>(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
variant: &'static str, variant: &'static str,
value: T, value: &T,
) -> Result<()> ) -> Result<()>
where where
T: ser::Serialize, T: ?Sized + ser::Serialize,
{ {
self.formatter.open(&mut self.writer, b'{')?; self.formatter.open(&mut self.writer, b'{')?;
self.formatter.comma(&mut self.writer, true)?; self.formatter.comma(&mut self.writer, true)?;
escape_key(&mut self.writer, variant)?; escape_key(&mut self.writer, variant)?;
self.formatter.colon(&mut self.writer)?; self.formatter.colon(&mut self.writer)?;
value.serialize(self)?; value.serialize(&mut *self)?;
self.formatter.close(&mut self.writer, b'}') self.formatter.close(&mut self.writer, b'}')
} }
#[inline] #[inline]
fn serialize_none(&mut self) -> Result<()> { fn serialize_none(self) -> Result<()> {
self.serialize_unit() self.serialize_unit()
} }
#[inline] #[inline]
fn serialize_some<V>(&mut self, value: V) -> Result<()> fn serialize_some<V>(self, value: &V) -> Result<()>
where where
V: ser::Serialize, V: ?Sized + ser::Serialize,
{ {
value.serialize(self) value.serialize(self)
} }
#[inline] #[inline]
fn serialize_seq(&mut self, len: Option<usize>) -> Result<State> { fn serialize_seq(self, len: Option<usize>) -> Result<Self::SerializeSeq> {
if len == Some(0) { let state = if len == Some(0) {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
self.writer.write_all(b"[]")?; self.writer.write_all(b"[]")?;
Ok(State::Empty) State::Empty
} else { } else {
self.formatter.open(&mut self.writer, b'[')?; self.formatter.open(&mut self.writer, b'[')?;
Ok(State::First) State::First
} };
Ok(Compound { ser: self, state })
} }
#[inline] #[inline]
fn serialize_seq_elt<T: ser::Serialize>(&mut self, state: &mut State, value: T) -> Result<()> fn serialize_tuple(self, len: usize) -> Result<Self::SerializeTuple> {
where
T: ser::Serialize,
{
self.formatter
.comma(&mut self.writer, *state == State::First)?;
*state = State::Rest;
value.serialize(self)
}
#[inline]
fn serialize_seq_end(&mut self, state: State) -> Result<()> {
match state {
State::Empty => Ok(()),
_ => self.formatter.close(&mut self.writer, b']'),
}
}
#[inline]
fn serialize_seq_fixed_size(&mut self, size: usize) -> Result<State> {
self.serialize_seq(Some(size))
}
#[inline]
fn serialize_tuple(&mut self, len: usize) -> Result<State> {
self.serialize_seq(Some(len)) self.serialize_seq(Some(len))
} }
#[inline] #[inline]
fn serialize_tuple_elt<T: ser::Serialize>( fn serialize_tuple_struct(
&mut self, self,
state: &mut State, _name: &'static str,
value: T, len: usize,
) -> Result<()> { ) -> Result<Self::SerializeTupleStruct> {
self.serialize_seq_elt(state, value)
}
#[inline]
fn serialize_tuple_end(&mut self, state: State) -> Result<()> {
self.serialize_seq_end(state)
}
#[inline]
fn serialize_tuple_struct(&mut self, _name: &'static str, len: usize) -> Result<State> {
self.serialize_seq(Some(len)) self.serialize_seq(Some(len))
} }
#[inline]
fn serialize_tuple_struct_elt<T: ser::Serialize>(
&mut self,
state: &mut State,
value: T,
) -> Result<()> {
self.serialize_seq_elt(state, value)
}
#[inline]
fn serialize_tuple_struct_end(&mut self, state: State) -> Result<()> {
self.serialize_seq_end(state)
}
#[inline] #[inline]
fn serialize_tuple_variant( fn serialize_tuple_variant(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
variant: &'static str, variant: &'static str,
len: usize, len: usize,
) -> Result<State> { ) -> Result<Self::SerializeTupleVariant> {
self.formatter.open(&mut self.writer, b'{')?; self.formatter.open(&mut self.writer, b'{')?;
self.formatter.comma(&mut self.writer, true)?; self.formatter.comma(&mut self.writer, true)?;
escape_key(&mut self.writer, variant)?; escape_key(&mut self.writer, variant)?;
@ -330,106 +278,208 @@ where
} }
#[inline] #[inline]
fn serialize_tuple_variant_elt<T: ser::Serialize>( fn serialize_map(self, len: Option<usize>) -> Result<Self::SerializeMap> {
&mut self, let state = if len == Some(0) {
state: &mut State,
value: T,
) -> Result<()> {
self.serialize_seq_elt(state, value)
}
#[inline]
fn serialize_tuple_variant_end(&mut self, state: State) -> Result<()> {
self.serialize_seq_end(state)?;
self.formatter.close(&mut self.writer, b'}')
}
#[inline]
fn serialize_map(&mut self, len: Option<usize>) -> Result<State> {
if len == Some(0) {
self.formatter.start_value(&mut self.writer)?; self.formatter.start_value(&mut self.writer)?;
self.writer.write_all(b"{}")?; self.writer.write_all(b"{}")?;
Ok(State::Empty) State::Empty
} else { } else {
self.formatter.open(&mut self.writer, b'{')?; self.formatter.open(&mut self.writer, b'{')?;
Ok(State::First) State::First
} };
Ok(Compound { ser: self, state })
} }
#[inline] #[inline]
fn serialize_map_key<T: ser::Serialize>(&mut self, state: &mut State, key: T) -> Result<()> { fn serialize_struct(self, _name: &'static str, len: usize) -> Result<Self::SerializeStruct> {
self.formatter
.comma(&mut self.writer, *state == State::First)?;
*state = State::Rest;
key.serialize(&mut MapKeySerializer { ser: self })?;
self.formatter.colon(&mut self.writer)
}
#[inline]
fn serialize_map_value<T: ser::Serialize>(&mut self, _: &mut State, value: T) -> Result<()> {
value.serialize(self)
}
#[inline]
fn serialize_map_end(&mut self, state: State) -> Result<()> {
match state {
State::Empty => Ok(()),
_ => self.formatter.close(&mut self.writer, b'}'),
}
}
#[inline]
fn serialize_struct(&mut self, _name: &'static str, len: usize) -> Result<State> {
self.serialize_map(Some(len)) self.serialize_map(Some(len))
} }
#[inline]
fn serialize_struct_elt<V: ser::Serialize>(
&mut self,
state: &mut State,
key: &'static str,
value: V,
) -> Result<()> {
self.serialize_map_key(state, key)?;
self.serialize_map_value(state, value)
}
#[inline]
fn serialize_struct_end(&mut self, state: State) -> Result<()> {
self.serialize_map_end(state)
}
#[inline] #[inline]
fn serialize_struct_variant( fn serialize_struct_variant(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
variant: &'static str, variant: &'static str,
len: usize, len: usize,
) -> Result<State> { ) -> Result<Self::SerializeStructVariant> {
self.formatter.open(&mut self.writer, b'{')?; self.formatter.open(&mut self.writer, b'{')?;
self.formatter.comma(&mut self.writer, true)?; self.formatter.comma(&mut self.writer, true)?;
escape_key(&mut self.writer, variant)?; escape_key(&mut self.writer, variant)?;
self.formatter.colon(&mut self.writer)?; self.formatter.colon(&mut self.writer)?;
self.serialize_map(Some(len)) self.serialize_map(Some(len))
} }
}
#[inline] impl<'a, W, F> ser::SerializeSeq for Compound<'a, W, F>
fn serialize_struct_variant_elt<V: ser::Serialize>( where
&mut self, W: io::Write,
state: &mut State, F: Formatter,
key: &'static str, {
value: V, type Ok = ();
) -> Result<()> { type Error = Error;
self.serialize_struct_elt(state, key, value)
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
where
T: serde::Serialize,
{
self.ser
.formatter
.comma(&mut self.ser.writer, self.state == State::First)?;
self.state = State::Rest;
value.serialize(&mut *self.ser)
} }
#[inline] fn end(self) -> Result<Self::Ok> {
fn serialize_struct_variant_end(&mut self, state: State) -> Result<()> { match self.state {
self.serialize_struct_end(state)?; State::Empty => Ok(()),
self.formatter.close(&mut self.writer, b'}') _ => self.ser.formatter.close(&mut self.ser.writer, b']'),
}
}
}
impl<'a, W, F> ser::SerializeTuple for Compound<'a, W, F>
where
W: io::Write,
F: Formatter,
{
type Ok = ();
type Error = Error;
fn serialize_element<T: ?Sized>(&mut self, value: &T) -> Result<()>
where
T: serde::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
fn end(self) -> Result<Self::Ok> {
ser::SerializeSeq::end(self)
}
}
impl<'a, W, F> ser::SerializeTupleStruct for Compound<'a, W, F>
where
W: io::Write,
F: Formatter,
{
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
where
T: serde::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
fn end(self) -> Result<Self::Ok> {
ser::SerializeSeq::end(self)
}
}
impl<'a, W, F> ser::SerializeTupleVariant for Compound<'a, W, F>
where
W: io::Write,
F: Formatter,
{
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, value: &T) -> Result<()>
where
T: serde::Serialize,
{
ser::SerializeSeq::serialize_element(self, value)
}
fn end(self) -> Result<Self::Ok> {
match self.state {
State::Empty => {}
_ => self.ser.formatter.close(&mut self.ser.writer, b']')?,
}
self.ser.formatter.close(&mut self.ser.writer, b'}')
}
}
impl<'a, W, F> ser::SerializeMap for Compound<'a, W, F>
where
W: io::Write,
F: Formatter,
{
type Ok = ();
type Error = Error;
fn serialize_key<T: ?Sized>(&mut self, key: &T) -> Result<()>
where
T: serde::Serialize,
{
self.ser
.formatter
.comma(&mut self.ser.writer, self.state == State::First)?;
self.state = State::Rest;
key.serialize(MapKeySerializer { ser: self.ser })?;
self.ser.formatter.colon(&mut self.ser.writer)
}
fn serialize_value<T: ?Sized>(&mut self, value: &T) -> Result<()>
where
T: serde::Serialize,
{
value.serialize(&mut *self.ser)
}
fn end(self) -> Result<Self::Ok> {
match self.state {
State::Empty => Ok(()),
_ => self.ser.formatter.close(&mut self.ser.writer, b'}'),
}
}
}
impl<'a, W, F> ser::SerializeStruct for Compound<'a, W, F>
where
W: io::Write,
F: Formatter,
{
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: serde::Serialize,
{
ser::SerializeMap::serialize_entry(self, key, value)
}
fn end(self) -> Result<Self::Ok> {
ser::SerializeMap::end(self)
}
}
impl<'a, W, F> ser::SerializeStructVariant for Compound<'a, W, F>
where
W: io::Write,
F: Formatter,
{
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, key: &'static str, value: &T) -> Result<()>
where
T: serde::Serialize,
{
ser::SerializeStruct::serialize_field(self, key, value)
}
fn end(self) -> Result<Self::Ok> {
match self.state {
State::Empty => {}
_ => self.ser.formatter.close(&mut self.ser.writer, b'}')?,
}
self.ser.formatter.close(&mut self.ser.writer, b'}')
} }
} }
@ -442,251 +492,163 @@ where
W: io::Write, W: io::Write,
F: Formatter, F: Formatter,
{ {
type Ok = ();
type Error = Error; type Error = Error;
#[inline] #[inline]
fn serialize_str(&mut self, value: &str) -> Result<()> { fn serialize_str(self, value: &str) -> Result<()> {
escape_key(&mut self.ser.writer, value).map_err(From::from) escape_key(&mut self.ser.writer, value).map_err(From::from)
} }
type SeqState = (); type SerializeSeq = ser::Impossible<(), Error>;
type TupleState = (); type SerializeTuple = ser::Impossible<(), Error>;
type TupleStructState = (); type SerializeTupleStruct = ser::Impossible<(), Error>;
type TupleVariantState = (); type SerializeTupleVariant = ser::Impossible<(), Error>;
type MapState = (); type SerializeMap = ser::Impossible<(), Error>;
type StructState = (); type SerializeStruct = ser::Impossible<(), Error>;
type StructVariantState = (); type SerializeStructVariant = ser::Impossible<(), Error>;
fn serialize_bool(&mut self, _value: bool) -> Result<()> { fn serialize_bool(self, _value: bool) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_isize(&mut self, _value: isize) -> Result<()> { fn serialize_i8(self, _value: i8) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_i8(&mut self, _value: i8) -> Result<()> { fn serialize_i16(self, _value: i16) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_i16(&mut self, _value: i16) -> Result<()> { fn serialize_i32(self, _value: i32) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_i32(&mut self, _value: i32) -> Result<()> { fn serialize_i64(self, _value: i64) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_i64(&mut self, _value: i64) -> Result<()> { fn serialize_u8(self, _value: u8) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_usize(&mut self, _value: usize) -> Result<()> { fn serialize_u16(self, _value: u16) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_u8(&mut self, _value: u8) -> Result<()> { fn serialize_u32(self, _value: u32) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_u16(&mut self, _value: u16) -> Result<()> { fn serialize_u64(self, _value: u64) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_u32(&mut self, _value: u32) -> Result<()> { fn serialize_f32(self, _value: f32) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_u64(&mut self, _value: u64) -> Result<()> { fn serialize_f64(self, _value: f64) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_f32(&mut self, _value: f32) -> Result<()> { fn serialize_char(self, _value: char) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_f64(&mut self, _value: f64) -> Result<()> { fn serialize_bytes(self, _value: &[u8]) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_char(&mut self, _value: char) -> Result<()> { fn serialize_unit(self) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_bytes(&mut self, _value: &[u8]) -> Result<()> { fn serialize_unit_struct(self, _name: &'static str) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_unit(&mut self) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_unit_struct(&mut self, _name: &'static str) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_unit_variant( fn serialize_unit_variant(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
) -> Result<()> { ) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_newtype_struct<T>(&mut self, _name: &'static str, _value: T) -> Result<()> fn serialize_newtype_struct<T>(self, _name: &'static str, _value: &T) -> Result<()>
where where
T: ser::Serialize, T: ?Sized + ser::Serialize,
{ {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_newtype_variant<T>( fn serialize_newtype_variant<T>(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_value: T, _value: &T,
) -> Result<()> ) -> Result<()>
where where
T: ser::Serialize, T: ?Sized + ser::Serialize,
{ {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_none(&mut self) -> Result<()> { fn serialize_none(self) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_some<T>(&mut self, _value: T) -> Result<()> fn serialize_some<T>(self, _value: &T) -> Result<()>
where where
T: ser::Serialize, T: ?Sized + ser::Serialize,
{ {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_seq(&mut self, _len: Option<usize>) -> Result<()> { fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeStruct> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_seq_elt<T: ser::Serialize>(&mut self, _state: &mut (), _value: T) -> Result<()> fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
where
T: ser::Serialize,
{
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_seq_end(&mut self, _state: ()) -> Result<()> { fn serialize_tuple_struct(
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) self,
} _name: &'static str,
_len: usize,
fn serialize_seq_fixed_size(&mut self, _size: usize) -> Result<()> { ) -> Result<Self::SerializeTupleStruct> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_tuple(&mut self, _len: usize) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_tuple_elt<T: ser::Serialize>(&mut self, _state: &mut (), _value: T) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_tuple_end(&mut self, _state: ()) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_tuple_struct(&mut self, _name: &'static str, _len: usize) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_tuple_struct_elt<T: ser::Serialize>(
&mut self,
_state: &mut (),
_value: T,
) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_tuple_struct_end(&mut self, _state: ()) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_tuple_variant( fn serialize_tuple_variant(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize, _len: usize,
) -> Result<()> { ) -> Result<Self::SerializeTupleVariant> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_tuple_variant_elt<T: ser::Serialize>( fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
&mut self,
_state: &mut (),
_value: T,
) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_tuple_variant_end(&mut self, _state: ()) -> Result<()> { fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_map(&mut self, _len: Option<usize>) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_map_key<T: ser::Serialize>(&mut self, _state: &mut (), _key: T) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_map_value<T: ser::Serialize>(&mut self, _state: &mut (), _value: T) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_map_end(&mut self, _state: ()) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_struct(&mut self, _name: &'static str, _len: usize) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_struct_elt<V: ser::Serialize>(
&mut self,
_state: &mut (),
_key: &'static str,
_value: V,
) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_struct_end(&mut self, _state: ()) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
fn serialize_struct_variant( fn serialize_struct_variant(
&mut self, self,
_name: &'static str, _name: &'static str,
_variant_index: usize, _variant_index: u32,
_variant: &'static str, _variant: &'static str,
_len: usize, _len: usize,
) -> Result<()> { ) -> Result<Self::SerializeStructVariant> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_struct_variant_elt<V: ser::Serialize>(
&mut self,
_state: &mut (),
_key: &'static str,
_value: V,
) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
}
fn serialize_struct_variant_end(&mut self, _state: ()) -> Result<()> {
Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0)) Err(Error::Syntax(ErrorCode::KeyMustBeAString, 0, 0))
} }
} }

View File

@ -58,6 +58,10 @@ where
Ok(Some(self.ch[idx])) Ok(Some(self.ch[idx]))
} }
// pub fn peek_next_or_null(&mut self, idx: usize) -> Result<u8> {
// Ok(try!(self.peek_next(idx)).unwrap_or(b'\x00'))
// }
pub fn peek(&mut self) -> Result<Option<u8>> { pub fn peek(&mut self) -> Result<Option<u8>> {
self.peek_next(0) self.peek_next(0)
} }
@ -234,6 +238,7 @@ impl<Iter: Iterator<Item = u8>> ParseNumber<Iter> {
if self.rdr.peek_or_null()? == b'0' { if self.rdr.peek_or_null()? == b'0' {
self.result.push(self.rdr.eat_char()); self.result.push(self.rdr.eat_char());
has_value = true; has_value = true;
// There can be only one leading '0'. // There can be only one leading '0'.
if let b'0'..=b'9' = self.rdr.peek_or_null()? { if let b'0'..=b'9' = self.rdr.peek_or_null()? {
return Err(Error::Syntax(ErrorCode::InvalidNumber, 0, 0)); return Err(Error::Syntax(ErrorCode::InvalidNumber, 0, 0));

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,214 @@
extern crate nu_json;
extern crate nu_test_support;
extern crate serde;
extern crate serde_json;
use nu_json::Value;
use regex::Regex;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
fn txt(text: &str) -> String {
let out = String::from_utf8_lossy(text.as_bytes());
#[cfg(windows)]
{
out.replace("\r\n", "").replace("\n", "")
}
#[cfg(not(windows))]
{
out.to_string()
}
}
fn hjson_expectations() -> PathBuf {
let assets = nu_test_support::fs::assets().join("nu_json");
dunce::canonicalize(assets.clone()).unwrap_or_else(|e| {
panic!(
"Couldn't canonicalize hjson assets path {}: {:?}",
assets.display(),
e
)
})
}
fn get_test_content(name: &str) -> io::Result<String> {
let expectations = hjson_expectations();
let mut p = format!("{}/{}_test.hjson", expectations.display(), name);
if !Path::new(&p).exists() {
p = format!("{}/{}_test.json", expectations.display(), name);
}
fs::read_to_string(&p)
}
fn get_result_content(name: &str) -> io::Result<(String, String)> {
let expectations = hjson_expectations();
let p1 = format!("{}/{}_result.json", expectations.display(), name);
let p2 = format!("{}/{}_result.hjson", expectations.display(), name);
Ok((fs::read_to_string(&p1)?, fs::read_to_string(&p2)?))
}
macro_rules! run_test {
// {{ is a workaround for rust stable
($v: ident, $list: expr, $fix: expr) => {{
let name = stringify!($v);
$list.push(format!("{}_test", name));
println!("- running {}", name);
let should_fail = name.starts_with("fail");
let test_content = get_test_content(name).unwrap();
let data: nu_json::Result<Value> = nu_json::from_str(&test_content);
assert!(should_fail == data.is_err());
if !should_fail {
let udata = data.unwrap();
let (rjson, rhjson) = get_result_content(name).unwrap();
let rjson = txt(&rjson);
let rhjson = txt(&rhjson);
let actual_hjson = nu_json::to_string(&udata).unwrap();
let actual_hjson = txt(&actual_hjson);
let actual_json = $fix(serde_json::to_string_pretty(&udata).unwrap());
let actual_json = txt(&actual_json);
if rhjson != actual_hjson {
println!(
"{:?}\n---hjson expected\n{}\n---hjson actual\n{}\n---\n",
name, rhjson, actual_hjson
);
}
if rjson != actual_json {
println!(
"{:?}\n---json expected\n{}\n---json actual\n{}\n---\n",
name, rjson, actual_json
);
}
assert!(rhjson == actual_hjson && rjson == actual_json);
}
}};
}
// add fixes where rust's json differs from javascript
fn std_fix(json: String) -> String {
// serde_json serializes integers with a superfluous .0 suffix
let re = Regex::new(r"(?m)(?P<d>\d)\.0(?P<s>,?)$").unwrap();
re.replace_all(&json, "$d$s").to_string()
}
fn fix_kan(json: String) -> String {
std_fix(json).replace(" -0,", " 0,")
}
fn fix_pass1(json: String) -> String {
std_fix(json)
.replace("1.23456789e34", "1.23456789e+34")
.replace("2.3456789012e76", "2.3456789012e+76")
}
#[test]
fn test_hjson() {
let mut done: Vec<String> = Vec::new();
println!("");
run_test!(charset, done, std_fix);
run_test!(comments, done, std_fix);
run_test!(empty, done, std_fix);
run_test!(failCharset1, done, std_fix);
run_test!(failJSON02, done, std_fix);
run_test!(failJSON05, done, std_fix);
run_test!(failJSON06, done, std_fix);
run_test!(failJSON07, done, std_fix);
run_test!(failJSON08, done, std_fix);
run_test!(failJSON10, done, std_fix);
run_test!(failJSON11, done, std_fix);
run_test!(failJSON12, done, std_fix);
run_test!(failJSON13, done, std_fix);
run_test!(failJSON14, done, std_fix);
run_test!(failJSON15, done, std_fix);
run_test!(failJSON16, done, std_fix);
run_test!(failJSON17, done, std_fix);
run_test!(failJSON19, done, std_fix);
run_test!(failJSON20, done, std_fix);
run_test!(failJSON21, done, std_fix);
run_test!(failJSON22, done, std_fix);
run_test!(failJSON23, done, std_fix);
run_test!(failJSON24, done, std_fix);
run_test!(failJSON26, done, std_fix);
run_test!(failJSON28, done, std_fix);
run_test!(failJSON29, done, std_fix);
run_test!(failJSON30, done, std_fix);
run_test!(failJSON31, done, std_fix);
run_test!(failJSON32, done, std_fix);
run_test!(failJSON33, done, std_fix);
run_test!(failJSON34, done, std_fix);
run_test!(failKey1, done, std_fix);
run_test!(failKey2, done, std_fix);
run_test!(failKey3, done, std_fix);
run_test!(failKey4, done, std_fix);
run_test!(failMLStr1, done, std_fix);
run_test!(failObj1, done, std_fix);
run_test!(failObj2, done, std_fix);
run_test!(failObj3, done, std_fix);
run_test!(failStr1a, done, std_fix);
run_test!(failStr1b, done, std_fix);
run_test!(failStr1c, done, std_fix);
run_test!(failStr1d, done, std_fix);
run_test!(failStr2a, done, std_fix);
run_test!(failStr2b, done, std_fix);
run_test!(failStr2c, done, std_fix);
run_test!(failStr2d, done, std_fix);
run_test!(failStr3a, done, std_fix);
run_test!(failStr3b, done, std_fix);
run_test!(failStr3c, done, std_fix);
run_test!(failStr3d, done, std_fix);
run_test!(failStr4a, done, std_fix);
run_test!(failStr4b, done, std_fix);
run_test!(failStr4c, done, std_fix);
run_test!(failStr4d, done, std_fix);
run_test!(failStr5a, done, std_fix);
run_test!(failStr5b, done, std_fix);
run_test!(failStr5c, done, std_fix);
run_test!(failStr5d, done, std_fix);
run_test!(failStr6a, done, std_fix);
run_test!(failStr6b, done, std_fix);
run_test!(failStr6c, done, std_fix);
run_test!(failStr6d, done, std_fix);
run_test!(kan, done, fix_kan);
run_test!(keys, done, std_fix);
run_test!(oa, done, std_fix);
run_test!(pass1, done, fix_pass1);
run_test!(pass2, done, std_fix);
run_test!(pass3, done, std_fix);
run_test!(pass4, done, std_fix);
run_test!(passSingle, done, std_fix);
run_test!(root, done, std_fix);
run_test!(stringify1, done, std_fix);
run_test!(strings, done, std_fix);
run_test!(trail, done, std_fix);
// check if we include all assets
let paths = fs::read_dir(hjson_expectations()).unwrap();
let all = paths
.map(|item| String::from(item.unwrap().path().file_stem().unwrap().to_str().unwrap()))
.filter(|x| x.contains("_test"))
.collect::<Vec<String>>();
let missing = all
.into_iter()
.filter(|x| done.iter().find(|y| &x == y) == None)
.collect::<Vec<String>>();
if missing.len() > 0 {
for item in missing {
println!("missing: {}", item);
}
assert!(false);
}
}

View File

@ -256,6 +256,10 @@ pub fn fixtures() -> PathBuf {
root().join("tests/fixtures") root().join("tests/fixtures")
} }
pub fn assets() -> PathBuf {
root().join("tests/assets")
}
pub fn in_directory(str: impl AsRef<Path>) -> String { pub fn in_directory(str: impl AsRef<Path>) -> String {
let path = str.as_ref(); let path = str.as_ref();
let path = if path.is_relative() { let path = if path.is_relative() {

View File

@ -0,0 +1,5 @@
{
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
js-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
ml-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
}

View File

@ -0,0 +1,5 @@
{
"ql-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
"js-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
"ml-ascii": "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
}

View File

@ -0,0 +1,6 @@
ql-ascii: ! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
js-ascii: "! \"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
ml-ascii:
'''
! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
'''

View File

@ -0,0 +1,26 @@
{
foo1: This is a string value. # part of the string
foo2: This is a string value.
bar1: This is a string value. // part of the string
bar2: This is a string value.
foobar1: This is a string value./* part of the string */
foobar2: This is a string value.
rem1: "# test"
rem2: "// test"
rem3: "/* test */"
num1: 0
num2: 0
num3: 2
true1: true
true2: true
true3: true
false1: false
false2: false
false3: false
null1: null
null2: null
null3: null
str1: 00 # part of the string
str2: 00.0 // part of the string
str3: 02 /* part of the string */
}

View File

@ -0,0 +1,26 @@
{
"foo1": "This is a string value. # part of the string",
"foo2": "This is a string value.",
"bar1": "This is a string value. // part of the string",
"bar2": "This is a string value.",
"foobar1": "This is a string value./* part of the string */",
"foobar2": "This is a string value.",
"rem1": "# test",
"rem2": "// test",
"rem3": "/* test */",
"num1": 0,
"num2": 0,
"num3": 2,
"true1": true,
"true2": true,
"true3": true,
"false1": false,
"false2": false,
"false3": false,
"null1": null,
"null2": null,
"null3": null,
"str1": "00 # part of the string",
"str2": "00.0 // part of the string",
"str3": "02 /* part of the string */"
}

View File

@ -0,0 +1,48 @@
// test
# all
// comment
/*
styles
*/
# with lf
# !
{
# hjson style comment
foo1: This is a string value. # part of the string
foo2: "This is a string value." # a comment
// js style comment
bar1: This is a string value. // part of the string
bar2: "This is a string value." // a comment
/* js block style comments */foobar1:/* more */This is a string value./* part of the string */
/* js block style comments */foobar2:/* more */"This is a string value."/* a comment */
rem1: "# test"
rem2: "// test"
rem3: "/* test */"
num1: 0 # comment
num2: 0.0 // comment
num3: 2 /* comment */
true1: true # comment
true2: true // comment
true3: true /* comment */
false1: false # comment
false2: false // comment
false3: false /* comment */
null1: null # comment
null2: null // comment
null3: null /* comment */
str1: 00 # part of the string
str2: 00.0 // part of the string
str3: 02 /* part of the string */
}

View File

@ -0,0 +1,3 @@
{
"": empty
}

View File

@ -0,0 +1,3 @@
{
"": "empty"
}

View File

@ -0,0 +1,3 @@
{
"": empty
}

View File

@ -0,0 +1,4 @@
{
# invalid \u char
char: "\uxxxx"
}

View File

@ -0,0 +1 @@
["Unclosed array"

View File

@ -0,0 +1 @@
["double extra comma",,]

View File

@ -0,0 +1 @@
[ , "<-- missing value"]

View File

@ -0,0 +1 @@
["Comma after the close"],

View File

@ -0,0 +1 @@
["Extra close"]]

View File

@ -0,0 +1 @@
{"Extra value after close": true} "misplaced quoted value"

View File

@ -0,0 +1 @@
{"Illegal expression": 1 + 2}

View File

@ -0,0 +1 @@
{"Illegal invocation": alert()}

View File

@ -0,0 +1 @@
{"Numbers cannot have leading zeroes": 013}

View File

@ -0,0 +1 @@
{"Numbers cannot be hex": 0x14}

View File

@ -0,0 +1 @@
["Illegal backslash escape: \x15"]

View File

@ -0,0 +1 @@
[\naked]

View File

@ -0,0 +1 @@
["Illegal backslash escape: \017"]

View File

@ -0,0 +1 @@
{"Missing colon" null}

View File

@ -0,0 +1 @@
{"Double colon":: null}

View File

@ -0,0 +1 @@
{"Comma instead of colon", null}

View File

@ -0,0 +1 @@
["Colon instead of comma": false]

View File

@ -0,0 +1 @@
["Bad value", truth]

View File

@ -0,0 +1 @@
['single quote']

View File

@ -0,0 +1 @@
["tab\ character\ in\ string\ "]

View File

@ -0,0 +1,2 @@
["line\
break"]

View File

@ -0,0 +1 @@
[0e]

View File

@ -0,0 +1 @@
[0e+]

View File

@ -0,0 +1 @@
[0e+-1]

View File

@ -0,0 +1 @@
{"Comma instead if closing brace": true,

View File

@ -0,0 +1 @@
["mismatch"}

View File

@ -0,0 +1,2 @@
A quoteless string is OK,
but two must be contained in an array.

View File

@ -0,0 +1,4 @@
{
# invalid name
wrong name: 0
}

View File

@ -0,0 +1,4 @@
{
# invalid name
{name: 0
}

View File

@ -0,0 +1,4 @@
{
# invalid name
key,name: 0
}

View File

@ -0,0 +1,4 @@
{
# invalid name
: 0
}

View File

@ -0,0 +1,3 @@
{
# invalid multiline string
ml: '''

View File

@ -0,0 +1,6 @@
{
# invalid obj
noDelimiter
{
}
}

View File

@ -0,0 +1,6 @@
{
# invalid obj
noEnd
{
}

View File

@ -0,0 +1,7 @@
{
# missing key
[
test
]
}

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ]
}

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ]x
}

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
]
]

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
]x
]

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: }
}

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: }x
}

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
}
]

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
}x
]

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: {
}

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: {x
}

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
{
]

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
{x
]

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: [
}

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: [x
}

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
[
]

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
[x
]

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: :
}

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: :x
}

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
:
]

View File

@ -0,0 +1,5 @@
[
foo
# invalid quoteless string
:x
]

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ,
}

View File

@ -0,0 +1,4 @@
{
# invalid quoteless string
ql: ,x
}

View File

@ -0,0 +1,6 @@
[
# invalid quoteless string
# note that if there were a preceding value the comma would
# be allowed/ignored as a separator/trailing comma
,
]

View File

@ -0,0 +1,6 @@
[
# invalid quoteless string
# note that if there were a preceding value the comma would
# be allowed/ignored as a separator/trailing comma
,x
]

View File

@ -0,0 +1,48 @@
{
numbers:
[
0
0
0
42
42.1
-5
-5.1
1701
-1701
12.345
-12.345
]
native:
[
true
true
false
false
null
null
]
strings:
[
x 0
.0
00
01
0 0 0
42 x
42.1 asdf
1.2.3
-5 0 -
-5.1 --
17.01e2 +
-17.01e2 :
12345e-3 @
-12345e-3 $
true true
x true
false false
x false
null null
x null
]
}

View File

@ -0,0 +1,45 @@
{
"numbers": [
0,
0,
0,
42,
42.1,
-5,
-5.1,
1701,
-1701,
12.345,
-12.345
],
"native": [
true,
true,
false,
false,
null,
null
],
"strings": [
"x 0",
".0",
"00",
"01",
"0 0 0",
"42 x",
"42.1 asdf",
"1.2.3",
"-5 0 -",
"-5.1 --",
"17.01e2 +",
"-17.01e2 :",
"12345e-3 @",
"-12345e-3 $",
"true true",
"x true",
"false false",
"x false",
"null null",
"x null"
]
}

View File

@ -0,0 +1,49 @@
{
# the comma forces a whitespace check
numbers:
[
0
0 ,
-0
42 ,
42.1 ,
-5
-5.1
17.01e2
-17.01e2
12345e-3 ,
-12345e-3 ,
]
native:
[
true ,
true
false ,
false
null ,
null
]
strings:
[
x 0
.0
00
01
0 0 0
42 x
42.1 asdf
1.2.3
-5 0 -
-5.1 --
17.01e2 +
-17.01e2 :
12345e-3 @
-12345e-3 $
true true
x true
false false
x false
null null
x null
]
}

View File

@ -0,0 +1,34 @@
{
unquoted_key: test
_unquoted: test
test-key: test
-test: test
.key: test
trailing: test
trailing2: test
"#c1": test
"foo#bar": test
"//bar": test
"foo//bar": test
"/*foo*/": test
"foo/*foo*/bar": test
"/*": test
"foo/*bar": test
"\"": test
"foo\"bar": test
"'''": test
"foo'''bar": test
":": test
"foo:bar": test
"{": test
"foo{bar": test
"}": test
"foo}bar": test
"[": test
"foo[bar": test
"]": test
"foo]bar": test
nl1: test
nl2: test
nl3: test
}

View File

@ -0,0 +1,34 @@
{
"unquoted_key": "test",
"_unquoted": "test",
"test-key": "test",
"-test": "test",
".key": "test",
"trailing": "test",
"trailing2": "test",
"#c1": "test",
"foo#bar": "test",
"//bar": "test",
"foo//bar": "test",
"/*foo*/": "test",
"foo/*foo*/bar": "test",
"/*": "test",
"foo/*bar": "test",
"\"": "test",
"foo\"bar": "test",
"'''": "test",
"foo'''bar": "test",
":": "test",
"foo:bar": "test",
"{": "test",
"foo{bar": "test",
"}": "test",
"foo}bar": "test",
"[": "test",
"foo[bar": "test",
"]": "test",
"foo]bar": "test",
"nl1": "test",
"nl2": "test",
"nl3": "test"
}

View File

@ -0,0 +1,48 @@
{
# unquoted keys
unquoted_key: test
_unquoted: test
test-key: test
-test: test
.key: test
# trailing spaces in key names are ignored
trailing : test
trailing2 : test
# comment char in key name
"#c1": test
"foo#bar": test
"//bar": test
"foo//bar": test
"/*foo*/": test
"foo/*foo*/bar": test
"/*": test
"foo/*bar": test
# quotes in key name
"\"": test
"foo\"bar": test
"'''": test
"foo'''bar": test
# control char in key name
":": test
"foo:bar": test
"{": test
"foo{bar": test
"}": test
"foo}bar": test
"[": test
"foo[bar": test
"]": test
"foo]bar": test
# newline
nl1:
test
nl2
:
test
nl3
:
test
}

View File

@ -0,0 +1,13 @@
[
a
{}
{}
[]
[]
{
b: 1
c: []
d: {}
}
[]
]

View File

@ -0,0 +1,13 @@
[
"a",
{},
{},
[],
[],
{
"b": 1,
"c": [],
"d": {}
},
[]
]

View File

@ -0,0 +1,13 @@
[
a
{}
{}
[]
[]
{
b: 1
c: []
d: {}
}
[]
]

View File

@ -0,0 +1,78 @@
[
JSON Test Pattern pass1
{
"object with 1 member":
[
array with 1 element
]
}
{}
[]
-42
true
false
null
{
integer: 1234567890
real: -9876.54321
e: 1.23456789e-13
E: 1.23456789e+34
-: 2.3456789012e+76
zero: 0
one: 1
space: " "
quote: '''"'''
backslash: \
controls: "\b\f\n\r\t"
slash: / & /
alpha: abcdefghijklmnopqrstuvwyz
ALPHA: ABCDEFGHIJKLMNOPQRSTUVWYZ
digit: 0123456789
0123456789: digit
special: `1~!@#$%^&*()_+-={':[,]}|;.</>?
hex: ģ䕧覫췯ꯍ
true: true
false: false
null: null
array: []
object: {}
address: 50 St. James Street
url: http://www.JSON.org/
comment: "// /* <!-- --"
"# -- --> */": " "
" s p a c e d ":
[
1
2
3
4
5
6
7
]
compact:
[
1
2
3
4
5
6
7
]
jsontext: '''{"object with 1 member":["array with 1 element"]}'''
quotes: &#34; " %22 0x22 034 &#x22;
"/\\\"쫾몾ꮘﳞ볚\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?": A key can be any string
}
0.5
98.6
99.44
1066
10
1
0.1
1
2
2
rosebud
]

View File

@ -0,0 +1,75 @@
[
"JSON Test Pattern pass1",
{
"object with 1 member": [
"array with 1 element"
]
},
{},
[],
-42,
true,
false,
null,
{
"integer": 1234567890,
"real": -9876.54321,
"e": 1.23456789e-13,
"E": 1.23456789e+34,
"-": 2.3456789012e+76,
"zero": 0,
"one": 1,
"space": " ",
"quote": "\"",
"backslash": "\\",
"controls": "\b\f\n\r\t",
"slash": "/ & /",
"alpha": "abcdefghijklmnopqrstuvwyz",
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
"digit": "0123456789",
"0123456789": "digit",
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
"hex": "ģ䕧覫췯ꯍ",
"true": true,
"false": false,
"null": null,
"array": [],
"object": {},
"address": "50 St. James Street",
"url": "http://www.JSON.org/",
"comment": "// /* <!-- --",
"# -- --> */": " ",
" s p a c e d ": [
1,
2,
3,
4,
5,
6,
7
],
"compact": [
1,
2,
3,
4,
5,
6,
7
],
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
"quotes": "&#34; \" %22 0x22 034 &#x22;",
"/\\\"쫾몾ꮘﳞ볚\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?": "A key can be any string"
},
0.5,
98.6,
99.44,
1066,
10,
1,
0.1,
1,
2,
2,
"rosebud"
]

View File

@ -0,0 +1,58 @@
[
"JSON Test Pattern pass1",
{"object with 1 member":["array with 1 element"]},
{},
[],
-42,
true,
false,
null,
{
"integer": 1234567890,
"real": -9876.543210,
"e": 0.123456789e-12,
"E": 1.234567890E+34,
"-": 23456789012E66,
"zero": 0,
"one": 1,
"space": " ",
"quote": "\"",
"backslash": "\\",
"controls": "\b\f\n\r\t",
"slash": "/ & \/",
"alpha": "abcdefghijklmnopqrstuvwyz",
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
"digit": "0123456789",
"0123456789": "digit",
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
"true": true,
"false": false,
"null": null,
"array":[ ],
"object":{ },
"address": "50 St. James Street",
"url": "http://www.JSON.org/",
"comment": "// /* <!-- --",
"# -- --> */": " ",
" s p a c e d " :[1,2 , 3
,
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
"quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
: "A key can be any string"
},
0.5 ,98.6
,
99.44
,
1066,
1e1,
0.1e1,
1e-1,
1e00,2e+00,2e-00
,"rosebud"]

View File

@ -0,0 +1,39 @@
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
Not too deep
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]

View File

@ -0,0 +1,39 @@
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
[
"Not too deep"
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]
]

View File

@ -0,0 +1 @@
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]

View File

@ -0,0 +1,7 @@
{
"JSON Test Pattern pass3":
{
"The outermost value": must be an object or array.
"In this test": It is an object.
}
}

View File

@ -0,0 +1,6 @@
{
"JSON Test Pattern pass3": {
"The outermost value": "must be an object or array.",
"In this test": "It is an object."
}
}

View File

@ -0,0 +1,6 @@
{
"JSON Test Pattern pass3": {
"The outermost value": "must be an object or array.",
"In this test": "It is an object."
}
}

View File

@ -0,0 +1 @@
10

Some files were not shown because too many files have changed in this diff Show More