diff --git a/Cargo.toml b/Cargo.toml index fbf260e8a..cc4375a44 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,14 +4,14 @@ name = "serde-json-core" version = "0.1.0" [dependencies] -heapless = "0.2.4" +heapless = "0.4.0" [dependencies.serde] default-features = false -version = "1.0.33" +version = "1.0.80" [dev-dependencies] -serde_derive = "1.0.33" +serde_derive = "1.0.80" [features] std = ["serde/std"] \ No newline at end of file diff --git a/src/de/mod.rs b/src/de/mod.rs index eb1000036..8ea3c3277 100644 --- a/src/de/mod.rs +++ b/src/de/mod.rs @@ -548,8 +548,32 @@ impl de::Error for Error { } impl fmt::Display for Error { - fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { - unreachable!() + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", match self { + Error::EofWhileParsingList => "EOF while parsing a list.", + Error::EofWhileParsingObject => "EOF while parsing an object.", + Error::EofWhileParsingString => "EOF while parsing a string.", + Error::EofWhileParsingValue => "EOF while parsing a JSON value.", + Error::ExpectedColon => "Expected this character to be a `':'`.", + Error::ExpectedListCommaOrEnd => "Expected this character to be either a `','` or\ + a \ + `']'`.", + Error::ExpectedObjectCommaOrEnd => "Expected this character to be either a `','` \ + or a \ + `'}'`.", + Error::ExpectedSomeIdent => "Expected to parse either a `true`, `false`, or a \ + `null`.", + Error::ExpectedSomeValue => "Expected this character to start a JSON value.", + Error::InvalidNumber => "Invalid number.", + Error::InvalidType => "Invalid type", + Error::InvalidUnicodeCodePoint => "Invalid unicode code point.", + Error::KeyMustBeAString => "Object key is not a string.", + Error::TrailingCharacters => "JSON has non-whitespace trailing characters after \ + the \ + value.", + Error::TrailingComma => "JSON has a comma after the last value in an array or map.", + _ => "Invalid JSON" + }) } } diff --git a/src/ser/mod.rs b/src/ser/mod.rs index d376a5a01..030fe9052 100644 --- a/src/ser/mod.rs +++ b/src/ser/mod.rs @@ -1,11 +1,10 @@ //! Serialize a Rust data structure into JSON data -use core::marker::Unsize; use core::{fmt, mem}; use serde::ser; -use heapless::{BufferFullError, String, Vec}; +use heapless::{String, Vec}; use self::seq::SerializeSeq; use self::struct_::SerializeStruct; @@ -25,6 +24,19 @@ pub enum Error { __Extensible, } +impl From<()> for Error { + fn from(_:()) -> Error { + Error::BufferFull + } +} + + +impl From for Error { + fn from(_:u8) -> Error { + Error::BufferFull + } +} + #[cfg(feature = "std")] impl ::std::error::Error for Error { fn description(&self) -> &str { @@ -32,28 +44,23 @@ impl ::std::error::Error for Error { } } -impl From for Error { - fn from(_: BufferFullError) -> Self { - Error::BufferFull - } -} impl fmt::Display for Error { - fn fmt(&self, _f: &mut fmt::Formatter) -> fmt::Result { - unreachable!() + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "Buffer is full") } } pub(crate) struct Serializer where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { buf: Vec, } impl Serializer where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { fn new() -> Self { Serializer { buf: Vec::new() } @@ -120,7 +127,7 @@ macro_rules! serialize_signed { impl<'a, B> ser::Serializer for &'a mut Serializer where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { type Ok = (); type Error = Error; @@ -317,7 +324,7 @@ where /// Serializes the given data structure as a string of JSON text pub fn to_string(value: &T) -> Result> where - B: Unsize<[u8]>, + B: heapless::ArrayLength, T: ser::Serialize + ?Sized, { let mut ser = Serializer::new(); @@ -328,7 +335,7 @@ where /// Serializes the given data structure as a JSON byte vector pub fn to_vec(value: &T) -> Result> where - B: Unsize<[u8]>, + B: heapless::ArrayLength, T: ser::Serialize + ?Sized, { let mut ser = Serializer::new(); @@ -414,19 +421,20 @@ impl ser::SerializeStructVariant for Unreachable { #[cfg(test)] mod tests { - const N: usize = 128; + use heapless::consts::U128; + type N = U128; #[test] fn array() { assert_eq!( - &*super::to_string::<[u8; N], _>(&[0, 1, 2]).unwrap(), + &*super::to_string::(&[0, 1, 2]).unwrap(), "[0,1,2]" ); } #[test] fn bool() { - assert_eq!(&*super::to_string::<[u8; N], _>(&true).unwrap(), "true"); + assert_eq!(&*super::to_string::(&true).unwrap(), "true"); } #[test] @@ -440,12 +448,12 @@ mod tests { } assert_eq!( - &*super::to_string::<[u8; N], _>(&Type::Boolean).unwrap(), + &*super::to_string::(&Type::Boolean).unwrap(), r#""boolean""# ); assert_eq!( - &*super::to_string::<[u8; N], _>(&Type::Number).unwrap(), + &*super::to_string::(&Type::Number).unwrap(), r#""number""# ); } @@ -453,7 +461,7 @@ mod tests { #[test] fn str() { assert_eq!( - &*super::to_string::<[u8; N], _>("hello").unwrap(), + &*super::to_string::("hello").unwrap(), r#""hello""# ); } @@ -466,7 +474,7 @@ mod tests { } assert_eq!( - &*super::to_string::<[u8; N], _>(&Led { led: true }).unwrap(), + &*super::to_string::(&Led { led: true }).unwrap(), r#"{"led":true}"# ); } @@ -479,22 +487,22 @@ mod tests { } assert_eq!( - &*super::to_string::<[u8; N], _>(&Temperature { temperature: 127 }).unwrap(), + &*super::to_string::(&Temperature { temperature: 127 }).unwrap(), r#"{"temperature":127}"# ); assert_eq!( - &*super::to_string::<[u8; N], _>(&Temperature { temperature: 20 }).unwrap(), + &*super::to_string::(&Temperature { temperature: 20 }).unwrap(), r#"{"temperature":20}"# ); assert_eq!( - &*super::to_string::<[u8; N], _>(&Temperature { temperature: -17 }).unwrap(), + &*super::to_string::(&Temperature { temperature: -17 }).unwrap(), r#"{"temperature":-17}"# ); assert_eq!( - &*super::to_string::<[u8; N], _>(&Temperature { temperature: -128 }).unwrap(), + &*super::to_string::(&Temperature { temperature: -128 }).unwrap(), r#"{"temperature":-128}"# ); } @@ -507,7 +515,7 @@ mod tests { } assert_eq!( - super::to_string::<[u8; N], _>(&Property { + super::to_string::(&Property { description: Some("An ambient temperature sensor"), }).unwrap(), r#"{"description":"An ambient temperature sensor"}"# @@ -515,7 +523,7 @@ mod tests { // XXX Ideally this should produce "{}" assert_eq!( - super::to_string::<[u8; N], _>(&Property { description: None }).unwrap(), + super::to_string::(&Property { description: None }).unwrap(), r#"{"description":null}"# ); } @@ -528,7 +536,7 @@ mod tests { } assert_eq!( - &*super::to_string::<[u8; N], _>(&Temperature { temperature: 20 }).unwrap(), + &*super::to_string::(&Temperature { temperature: 20 }).unwrap(), r#"{"temperature":20}"# ); } @@ -539,7 +547,7 @@ mod tests { struct Empty {} assert_eq!( - &*super::to_string::<[u8; N], _>(&Empty {}).unwrap(), + &*super::to_string::(&Empty {}).unwrap(), r#"{}"# ); @@ -550,7 +558,7 @@ mod tests { } assert_eq!( - &*super::to_string::<[u8; N], _>(&Tuple { a: true, b: false }).unwrap(), + &*super::to_string::(&Tuple { a: true, b: false }).unwrap(), r#"{"a":true,"b":false}"# ); } diff --git a/src/ser/seq.rs b/src/ser/seq.rs index f1fd2ce80..553a02170 100644 --- a/src/ser/seq.rs +++ b/src/ser/seq.rs @@ -1,12 +1,10 @@ -use core::marker::Unsize; - use serde::ser; use ser::{Error, Result, Serializer}; pub struct SerializeSeq<'a, B> where - B: Unsize<[u8]> + 'a, + B: heapless::ArrayLength + 'a, { de: &'a mut Serializer, first: bool, @@ -14,7 +12,7 @@ where impl<'a, B> SerializeSeq<'a, B> where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { pub(crate) fn new(de: &'a mut Serializer) -> Self { SerializeSeq { de, first: true } @@ -23,7 +21,7 @@ where impl<'a, B> ser::SerializeSeq for SerializeSeq<'a, B> where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { type Ok = (); type Error = Error; @@ -49,7 +47,7 @@ where impl<'a, B> ser::SerializeTuple for SerializeSeq<'a, B> where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { type Ok = (); type Error = Error; diff --git a/src/ser/struct_.rs b/src/ser/struct_.rs index 481dea121..a6a51ad71 100644 --- a/src/ser/struct_.rs +++ b/src/ser/struct_.rs @@ -1,12 +1,10 @@ -use core::marker::Unsize; - use serde::ser; use ser::{Error, Result, Serializer}; pub struct SerializeStruct<'a, B> where - B: Unsize<[u8]> + 'a, + B: heapless::ArrayLength + 'a, { de: &'a mut Serializer, first: bool, @@ -14,7 +12,7 @@ where impl<'a, B> SerializeStruct<'a, B> where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { pub(crate) fn new(de: &'a mut Serializer) -> Self { SerializeStruct { de, first: true } @@ -23,7 +21,7 @@ where impl<'a, B> ser::SerializeStruct for SerializeStruct<'a, B> where - B: Unsize<[u8]>, + B: heapless::ArrayLength, { type Ok = (); type Error = Error;