Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ before_script:
export PATH=$HOME/.local/bin:$PATH

script:
# TEMPORARY UNTIL SERDE 0.7 IS RELEASED
- |
mkdir .travis-deps
mkdir .cargo
echo 'paths = ["./.travis-deps/serde"]' > .cargo/config
git clone https://github.com/serde-rs/serde .travis-deps/serde
pushd .travis-deps/serde
git reset --hard origin/master
popd
- |
(cd json && travis-cargo build) &&
(cd json && travis-cargo test) &&
Expand Down
5 changes: 4 additions & 1 deletion json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ keywords = ["json", "serde", "serialization"]
nightly-testing = ["clippy"]

[dependencies]
serde = "~0.6.1"
num = { version = "~0.1.27", default-features = false }
clippy = { version = "^0.*", optional = true }

[dependencies.serde]
serde = "0.6"

24 changes: 10 additions & 14 deletions json/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,15 +525,15 @@ impl<Iter> de::Deserializer for Deserializer<Iter>
type Error = Error;

#[inline]
fn visit<V>(&mut self, visitor: V) -> Result<V::Value>
fn deserialize<V>(&mut self, visitor: V) -> Result<V::Value>
where V: de::Visitor,
{
self.parse_value(visitor)
}

/// Parses a `null` as a None, and any other values as a `Some(...)`.
#[inline]
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value>
fn deserialize_option<V>(&mut self, mut visitor: V) -> Result<V::Value>
where V: de::Visitor,
{
try!(self.parse_whitespace());
Expand All @@ -552,7 +552,7 @@ impl<Iter> de::Deserializer for Deserializer<Iter>

/// Parses a newtype struct as the underlying value.
#[inline]
fn visit_newtype_struct<V>(&mut self,
fn deserialize_newtype_struct<V>(&mut self,
_name: &str,
mut visitor: V) -> Result<V::Value>
where V: de::Visitor,
Expand All @@ -563,7 +563,7 @@ impl<Iter> de::Deserializer for Deserializer<Iter>
/// Parses an enum as an object like `{"$KEY":$VALUE}`, where $VALUE is either a straight
/// value, a `[..]`, or a `{..}`.
#[inline]
fn visit_enum<V>(&mut self,
fn deserialize_enum<V>(&mut self,
_name: &str,
_variants: &'static [&'static str],
mut visitor: V) -> Result<V::Value>
Expand Down Expand Up @@ -595,11 +595,6 @@ impl<Iter> de::Deserializer for Deserializer<Iter>
}
}
}

#[inline]
fn format() -> &'static str {
"json"
}
}

struct SeqVisitor<'a, Iter: 'a + Iterator<Item=io::Result<u8>>> {
Expand Down Expand Up @@ -753,14 +748,15 @@ impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter>
impl de::Deserializer for MissingFieldDeserializer {
type Error = de::value::Error;

fn visit<V>(&mut self, _visitor: V) -> std::result::Result<V::Value, Self::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::MissingFieldError(field))
Err(de::value::Error::MissingField(field))
}

fn visit_option<V>(&mut self, mut visitor: V) -> std::result::Result<V::Value, Self::Error>
fn deserialize_option<V>(&mut self,
mut visitor: V) -> std::result::Result<V::Value, Self::Error>
where V: de::Visitor,
{
visitor.visit_none()
Expand Down Expand Up @@ -800,15 +796,15 @@ impl<Iter> de::VariantVisitor for Deserializer<Iter>
visitor: V) -> Result<V::Value>
where V: de::Visitor,
{
de::Deserializer::visit(self, visitor)
de::Deserializer::deserialize(self, visitor)
}

fn visit_struct<V>(&mut self,
_fields: &'static [&'static str],
visitor: V) -> Result<V::Value>
where V: de::Visitor,
{
de::Deserializer::visit(self, visitor)
de::Deserializer::deserialize(self, visitor)
}
}

Expand Down
34 changes: 30 additions & 4 deletions json/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::result;
use std::string::FromUtf8Error;

use serde::de;
use serde::ser;

/// The errors that can arise while parsing a JSON stream.
#[derive(Clone, PartialEq)]
Expand Down Expand Up @@ -67,6 +68,15 @@ pub enum ErrorCode {

/// Unexpected end of hex excape.
UnexpectedEndOfHexEscape,

/// Invalid length
Length(usize),

/// Incorrect type from value
Type(de::Type),

/// Catchall for syntax error messages
Syntax(String),
}

impl fmt::Debug for ErrorCode {
Expand All @@ -92,6 +102,9 @@ impl fmt::Debug for ErrorCode {
ErrorCode::MissingField(ref field) => write!(f, "missing field \"{}\"", field),
ErrorCode::TrailingCharacters => "trailing characters".fmt(f),
ErrorCode::UnexpectedEndOfHexEscape => "unexpected end of hex escape".fmt(f),
ErrorCode::Length(ref len) => write!(f, "incorrect value length {}", len),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes are already contained in PR #11 and are likely to conflict if that PR is merged.

ErrorCode::Type(ref ty) => write!(f, "incorrect value type: {:?}", ty),
ErrorCode::Syntax(ref msg) => write!(f, "syntax error: {:?}", msg),
}
}
}
Expand Down Expand Up @@ -156,18 +169,24 @@ impl From<FromUtf8Error> for Error {
impl From<de::value::Error> for Error {
fn from(error: de::value::Error) -> Error {
match error {
de::value::Error::SyntaxError => {
de::value::Error::Syntax(_) => {
Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)
}
de::value::Error::EndOfStreamError => {
de::value::Error::EndOfStream => {
de::Error::end_of_stream()
}
de::value::Error::UnknownFieldError(field) => {
de::value::Error::UnknownField(field) => {
Error::SyntaxError(ErrorCode::UnknownField(field), 0, 0)
}
de::value::Error::MissingFieldError(field) => {
de::value::Error::MissingField(field) => {
Error::SyntaxError(ErrorCode::MissingField(field), 0, 0)
}
de::value::Error::Length(len) => {
Error::SyntaxError(ErrorCode::Length(len), 0, 0)
}
de::value::Error::Type(ty) => {
Error::SyntaxError(ErrorCode::Type(ty), 0, 0)
}
}
}
}
Expand All @@ -190,5 +209,12 @@ impl de::Error for Error {
}
}

impl ser::Error for Error {
/// Raised when there is general error when deserializing a type.
fn syntax(msg: &str) -> Self {
Error::SyntaxError(ErrorCode::Syntax(String::from(msg)), 0, 0)
}
}

/// Helper alias for `Result` objects that return a JSON `Error`.
pub type Result<T> = result::Result<T, Error>;
Loading