Migrate serde_json into it's own repo
New location is https://github.com/serde-rs/json.
This commit is contained in:
parent
42069ca669
commit
fa3460e0a7
@ -1,14 +0,0 @@
|
||||
[package]
|
||||
name = "serde_json"
|
||||
version = "0.5.1"
|
||||
authors = ["Erick Tryzelaar <erick.tryzelaar@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
description = "A JSON serialization file format"
|
||||
repository = "https://github.com/serde-rs/serde"
|
||||
documentation = "https://serde-rs.github.io/serde/serde_json/serde_json/index.html"
|
||||
readme = "../README.md"
|
||||
keywords = ["json", "serde", "serialization"]
|
||||
|
||||
[dependencies]
|
||||
num = "*"
|
||||
serde = { version = "*", path = "../serde" }
|
@ -1,85 +0,0 @@
|
||||
// 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 std::collections::BTreeMap;
|
||||
|
||||
use serde::ser::{self, Serialize};
|
||||
|
||||
use value::{self, Value};
|
||||
|
||||
pub struct ArrayBuilder {
|
||||
array: Vec<Value>,
|
||||
}
|
||||
|
||||
impl ArrayBuilder {
|
||||
pub fn new() -> ArrayBuilder {
|
||||
ArrayBuilder { array: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Value {
|
||||
Value::Array(self.array)
|
||||
}
|
||||
|
||||
pub fn push<T: ser::Serialize>(mut self, v: T) -> ArrayBuilder {
|
||||
self.array.push(value::to_value(&v));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn push_array<F>(mut self, f: F) -> ArrayBuilder where
|
||||
F: FnOnce(ArrayBuilder) -> ArrayBuilder
|
||||
{
|
||||
let builder = ArrayBuilder::new();
|
||||
self.array.push(f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn push_object<F>(mut self, f: F) -> ArrayBuilder where
|
||||
F: FnOnce(ObjectBuilder) -> ObjectBuilder
|
||||
{
|
||||
let builder = ObjectBuilder::new();
|
||||
self.array.push(f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ObjectBuilder {
|
||||
object: BTreeMap<String, Value>,
|
||||
}
|
||||
|
||||
impl ObjectBuilder {
|
||||
pub fn new() -> ObjectBuilder {
|
||||
ObjectBuilder { object: BTreeMap::new() }
|
||||
}
|
||||
|
||||
pub fn unwrap(self) -> Value {
|
||||
Value::Object(self.object)
|
||||
}
|
||||
|
||||
pub fn insert<V: ser::Serialize>(mut self, k: String, v: V) -> ObjectBuilder {
|
||||
self.object.insert(k, value::to_value(&v));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn insert_array<F>(mut self, key: String, f: F) -> ObjectBuilder where
|
||||
F: FnOnce(ArrayBuilder) -> ArrayBuilder
|
||||
{
|
||||
let builder = ArrayBuilder::new();
|
||||
self.object.insert(key, f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn insert_object<F>(mut self, key: String, f: F) -> ObjectBuilder where
|
||||
F: FnOnce(ObjectBuilder) -> ObjectBuilder
|
||||
{
|
||||
let builder = ObjectBuilder::new();
|
||||
self.object.insert(key, f(builder).unwrap());
|
||||
self
|
||||
}
|
||||
}
|
@ -1,820 +0,0 @@
|
||||
use std::char;
|
||||
use std::i32;
|
||||
use std::io;
|
||||
use std::str;
|
||||
|
||||
use serde::de;
|
||||
use serde::iter::LineColIterator;
|
||||
|
||||
use super::error::{Error, ErrorCode, Result};
|
||||
|
||||
pub struct Deserializer<Iter: Iterator<Item=io::Result<u8>>> {
|
||||
rdr: LineColIterator<Iter>,
|
||||
ch: Option<u8>,
|
||||
str_buf: Vec<u8>,
|
||||
}
|
||||
|
||||
macro_rules! try_or_invalid {
|
||||
($self_:expr, $e:expr) => {
|
||||
match $e {
|
||||
Some(v) => v,
|
||||
None => { return Err($self_.error(ErrorCode::InvalidNumber)); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter> Deserializer<Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
/// Creates the JSON parser from an `std::iter::Iterator`.
|
||||
#[inline]
|
||||
pub fn new(rdr: Iter) -> Deserializer<Iter> {
|
||||
Deserializer {
|
||||
rdr: LineColIterator::new(rdr),
|
||||
ch: None,
|
||||
str_buf: Vec::with_capacity(128),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn end(&mut self) -> Result<()> {
|
||||
try!(self.parse_whitespace());
|
||||
if try!(self.eof()) {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(self.error(ErrorCode::TrailingCharacters))
|
||||
}
|
||||
}
|
||||
|
||||
fn eof(&mut self) -> Result<bool> {
|
||||
Ok(try!(self.peek()).is_none())
|
||||
}
|
||||
|
||||
fn peek(&mut self) -> Result<Option<u8>> {
|
||||
match self.ch {
|
||||
Some(ch) => Ok(Some(ch)),
|
||||
None => {
|
||||
match self.rdr.next() {
|
||||
Some(Err(err)) => Err(Error::IoError(err)),
|
||||
Some(Ok(ch)) => {
|
||||
self.ch = Some(ch);
|
||||
Ok(self.ch)
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn peek_or_null(&mut self) -> Result<u8> {
|
||||
Ok(try!(self.peek()).unwrap_or(b'\x00'))
|
||||
}
|
||||
|
||||
fn eat_char(&mut self) {
|
||||
self.ch = None;
|
||||
}
|
||||
|
||||
fn next_char(&mut self) -> Result<Option<u8>> {
|
||||
match self.ch.take() {
|
||||
Some(ch) => Ok(Some(ch)),
|
||||
None => {
|
||||
match self.rdr.next() {
|
||||
Some(Err(err)) => Err(Error::IoError(err)),
|
||||
Some(Ok(ch)) => Ok(Some(ch)),
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn next_char_or_null(&mut self) -> Result<u8> {
|
||||
Ok(try!(self.next_char()).unwrap_or(b'\x00'))
|
||||
}
|
||||
|
||||
fn error(&mut self, reason: ErrorCode) -> Error {
|
||||
Error::SyntaxError(reason, self.rdr.line(), self.rdr.col())
|
||||
}
|
||||
|
||||
fn parse_whitespace(&mut self) -> Result<()> {
|
||||
loop {
|
||||
match try!(self.peek_or_null()) {
|
||||
b' ' | b'\n' | b'\t' | b'\r' => {
|
||||
self.eat_char();
|
||||
}
|
||||
_ => { return Ok(()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_value<V>(&mut self, mut visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
if try!(self.eof()) {
|
||||
return Err(self.error(ErrorCode::EOFWhileParsingValue));
|
||||
}
|
||||
|
||||
let value = match try!(self.peek_or_null()) {
|
||||
b'n' => {
|
||||
self.eat_char();
|
||||
try!(self.parse_ident(b"ull"));
|
||||
visitor.visit_unit()
|
||||
}
|
||||
b't' => {
|
||||
self.eat_char();
|
||||
try!(self.parse_ident(b"rue"));
|
||||
visitor.visit_bool(true)
|
||||
}
|
||||
b'f' => {
|
||||
self.eat_char();
|
||||
try!(self.parse_ident(b"alse"));
|
||||
visitor.visit_bool(false)
|
||||
}
|
||||
b'-' => {
|
||||
self.eat_char();
|
||||
self.parse_integer(false, visitor)
|
||||
}
|
||||
b'0' ... b'9' => {
|
||||
self.parse_integer(true, visitor)
|
||||
}
|
||||
b'"' => {
|
||||
self.eat_char();
|
||||
try!(self.parse_string());
|
||||
let s = str::from_utf8(&self.str_buf).unwrap();
|
||||
visitor.visit_str(s)
|
||||
}
|
||||
b'[' => {
|
||||
self.eat_char();
|
||||
visitor.visit_seq(SeqVisitor::new(self))
|
||||
}
|
||||
b'{' => {
|
||||
self.eat_char();
|
||||
visitor.visit_map(MapVisitor::new(self))
|
||||
}
|
||||
_ => {
|
||||
Err(self.error(ErrorCode::ExpectedSomeValue))
|
||||
}
|
||||
};
|
||||
|
||||
match value {
|
||||
Ok(value) => Ok(value),
|
||||
Err(Error::SyntaxError(code, _, _)) => Err(self.error(code)),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_ident(&mut self, ident: &[u8]) -> Result<()> {
|
||||
for c in ident {
|
||||
if Some(*c) != try!(self.next_char()) {
|
||||
return Err(self.error(ErrorCode::ExpectedSomeIdent));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_integer<V>(&mut self, pos: bool, visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match try!(self.next_char_or_null()) {
|
||||
b'0' => {
|
||||
// There can be only one leading '0'.
|
||||
match try!(self.peek_or_null()) {
|
||||
b'0' ... b'9' => {
|
||||
Err(self.error(ErrorCode::InvalidNumber))
|
||||
}
|
||||
_ => {
|
||||
self.parse_number(pos, 0, visitor)
|
||||
}
|
||||
}
|
||||
},
|
||||
c @ b'1' ... b'9' => {
|
||||
let mut res: u64 = (c as u64) - ('0' as u64);
|
||||
|
||||
loop {
|
||||
match try!(self.peek_or_null()) {
|
||||
c @ b'0' ... b'9' => {
|
||||
self.eat_char();
|
||||
|
||||
let digit = (c as u64) - ('0' as u64);
|
||||
|
||||
// We need to be careful with overflow. If we can, try to keep the
|
||||
// number as a `u64` until we grow too large. At that point, switch to
|
||||
// parsing the value as a `f64`.
|
||||
match res.checked_mul(10).and_then(|val| val.checked_add(digit)) {
|
||||
Some(res_) => { res = res_; }
|
||||
None => {
|
||||
return self.parse_float(
|
||||
pos,
|
||||
(res as f64) * 10.0 + (digit as f64),
|
||||
visitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
return self.parse_number(pos, res, visitor);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Err(self.error(ErrorCode::InvalidNumber))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_float<V>(&mut self,
|
||||
pos: bool,
|
||||
mut res: f64,
|
||||
mut visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
loop {
|
||||
match try!(self.next_char_or_null()) {
|
||||
c @ b'0' ... b'9' => {
|
||||
let digit = (c as u64) - ('0' as u64);
|
||||
|
||||
res *= 10.0;
|
||||
res += digit as f64;
|
||||
}
|
||||
_ => {
|
||||
match try!(self.peek_or_null()) {
|
||||
b'.' => {
|
||||
return self.parse_decimal(pos, res, visitor);
|
||||
}
|
||||
b'e' | b'E' => {
|
||||
return self.parse_exponent(pos, res, visitor);
|
||||
}
|
||||
_ => {
|
||||
if !pos {
|
||||
res = -res;
|
||||
}
|
||||
|
||||
return visitor.visit_f64(res);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_number<V>(&mut self,
|
||||
pos: bool,
|
||||
res: u64,
|
||||
mut visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match try!(self.peek_or_null()) {
|
||||
b'.' => {
|
||||
self.parse_decimal(pos, res as f64, visitor)
|
||||
}
|
||||
b'e' | b'E' => {
|
||||
self.parse_exponent(pos, res as f64, visitor)
|
||||
}
|
||||
_ => {
|
||||
if pos {
|
||||
visitor.visit_u64(res)
|
||||
} else {
|
||||
// FIXME: `wrapping_neg` will be stable in Rust 1.2
|
||||
//let res_i64 = (res as i64).wrapping_neg();
|
||||
let res_i64 = (!res + 1) as i64;
|
||||
|
||||
// Convert into a float if we underflow.
|
||||
if res_i64 > 0 {
|
||||
visitor.visit_f64(-(res as f64))
|
||||
} else {
|
||||
visitor.visit_i64(res_i64)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_decimal<V>(&mut self,
|
||||
pos: bool,
|
||||
mut res: f64,
|
||||
mut visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
self.eat_char();
|
||||
|
||||
let mut dec = 0.1;
|
||||
|
||||
// Make sure a digit follows the decimal place.
|
||||
match try!(self.next_char_or_null()) {
|
||||
c @ b'0' ... b'9' => {
|
||||
res += (((c as u64) - (b'0' as u64)) as f64) * dec;
|
||||
}
|
||||
_ => { return Err(self.error(ErrorCode::InvalidNumber)); }
|
||||
}
|
||||
|
||||
loop {
|
||||
match try!(self.peek_or_null()) {
|
||||
c @ b'0' ... b'9' => {
|
||||
self.eat_char();
|
||||
|
||||
dec /= 10.0;
|
||||
res += (((c as u64) - (b'0' as u64)) as f64) * dec;
|
||||
}
|
||||
_ => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
match try!(self.peek_or_null()) {
|
||||
b'e' | b'E' => {
|
||||
self.parse_exponent(pos, res, visitor)
|
||||
}
|
||||
_ => {
|
||||
if pos {
|
||||
visitor.visit_f64(res)
|
||||
} else {
|
||||
visitor.visit_f64(-res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn parse_exponent<V>(&mut self,
|
||||
pos: bool,
|
||||
mut res: f64,
|
||||
mut visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
self.eat_char();
|
||||
|
||||
let pos_exp = match try!(self.peek_or_null()) {
|
||||
b'+' => { self.eat_char(); true }
|
||||
b'-' => { self.eat_char(); false }
|
||||
_ => { true }
|
||||
};
|
||||
|
||||
// Make sure a digit follows the exponent place.
|
||||
let mut exp = match try!(self.next_char_or_null()) {
|
||||
c @ b'0' ... b'9' => { (c as u64) - (b'0' as u64) }
|
||||
_ => { return Err(self.error(ErrorCode::InvalidNumber)); }
|
||||
};
|
||||
|
||||
loop {
|
||||
match try!(self.peek_or_null()) {
|
||||
c @ b'0' ... b'9' => {
|
||||
self.eat_char();
|
||||
|
||||
exp = try_or_invalid!(self, exp.checked_mul(10));
|
||||
exp = try_or_invalid!(self, exp.checked_add((c as u64) - (b'0' as u64)));
|
||||
}
|
||||
_ => { break; }
|
||||
}
|
||||
}
|
||||
|
||||
let exp = if exp <= i32::MAX as u64 {
|
||||
10_f64.powi(exp as i32)
|
||||
} else {
|
||||
return Err(self.error(ErrorCode::InvalidNumber));
|
||||
};
|
||||
|
||||
if pos_exp {
|
||||
res *= exp;
|
||||
} else {
|
||||
res /= exp;
|
||||
}
|
||||
|
||||
if pos {
|
||||
visitor.visit_f64(res)
|
||||
} else {
|
||||
visitor.visit_f64(-res)
|
||||
}
|
||||
}
|
||||
|
||||
fn decode_hex_escape(&mut self) -> Result<u16> {
|
||||
let mut i = 0;
|
||||
let mut n = 0u16;
|
||||
while i < 4 && !try!(self.eof()) {
|
||||
n = match try!(self.next_char_or_null()) {
|
||||
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'b' | b'B' => n * 16_u16 + 11_u16,
|
||||
b'c' | b'C' => n * 16_u16 + 12_u16,
|
||||
b'd' | b'D' => n * 16_u16 + 13_u16,
|
||||
b'e' | b'E' => n * 16_u16 + 14_u16,
|
||||
b'f' | b'F' => n * 16_u16 + 15_u16,
|
||||
_ => { return Err(self.error(ErrorCode::InvalidEscape)); }
|
||||
};
|
||||
|
||||
i += 1;
|
||||
}
|
||||
|
||||
// Error out if we didn't parse 4 digits.
|
||||
if i != 4 {
|
||||
return Err(self.error(ErrorCode::InvalidEscape));
|
||||
}
|
||||
|
||||
Ok(n)
|
||||
}
|
||||
|
||||
fn parse_string(&mut self) -> Result<()> {
|
||||
self.str_buf.clear();
|
||||
|
||||
loop {
|
||||
let ch = match try!(self.next_char()) {
|
||||
Some(ch) => ch,
|
||||
None => { return Err(self.error(ErrorCode::EOFWhileParsingString)); }
|
||||
};
|
||||
|
||||
match ch {
|
||||
b'"' => {
|
||||
return Ok(());
|
||||
}
|
||||
b'\\' => {
|
||||
let ch = match try!(self.next_char()) {
|
||||
Some(ch) => ch,
|
||||
None => { return Err(self.error(ErrorCode::EOFWhileParsingString)); }
|
||||
};
|
||||
|
||||
match ch {
|
||||
b'"' => self.str_buf.push(b'"'),
|
||||
b'\\' => self.str_buf.push(b'\\'),
|
||||
b'/' => self.str_buf.push(b'/'),
|
||||
b'b' => self.str_buf.push(b'\x08'),
|
||||
b'f' => self.str_buf.push(b'\x0c'),
|
||||
b'n' => self.str_buf.push(b'\n'),
|
||||
b'r' => self.str_buf.push(b'\r'),
|
||||
b't' => self.str_buf.push(b'\t'),
|
||||
b'u' => {
|
||||
let c = match try!(self.decode_hex_escape()) {
|
||||
0xDC00 ... 0xDFFF => {
|
||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||
}
|
||||
|
||||
// Non-BMP characters are encoded as a sequence of
|
||||
// two hex escapes, representing UTF-16 surrogates.
|
||||
n1 @ 0xD800 ... 0xDBFF => {
|
||||
match (try!(self.next_char()), try!(self.next_char())) {
|
||||
(Some(b'\\'), Some(b'u')) => (),
|
||||
_ => {
|
||||
return Err(self.error(ErrorCode::UnexpectedEndOfHexEscape));
|
||||
}
|
||||
}
|
||||
|
||||
let n2 = try!(self.decode_hex_escape());
|
||||
|
||||
if n2 < 0xDC00 || n2 > 0xDFFF {
|
||||
return Err(self.error(ErrorCode::LoneLeadingSurrogateInHexEscape));
|
||||
}
|
||||
|
||||
let n = (((n1 - 0xD800) as u32) << 10 |
|
||||
(n2 - 0xDC00) as u32) + 0x1_0000;
|
||||
|
||||
match char::from_u32(n as u32) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
n => {
|
||||
match char::from_u32(n as u32) {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
return Err(self.error(ErrorCode::InvalidUnicodeCodePoint));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// FIXME: this allocation is required in order to be compatible with stable
|
||||
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||
let buf = c.to_string();
|
||||
self.str_buf.extend(buf.bytes());
|
||||
}
|
||||
_ => {
|
||||
return Err(self.error(ErrorCode::InvalidEscape));
|
||||
}
|
||||
}
|
||||
}
|
||||
ch => {
|
||||
self.str_buf.push(ch);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_object_colon(&mut self) -> Result<()> {
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
match try!(self.next_char()) {
|
||||
Some(b':') => Ok(()),
|
||||
Some(_) => Err(self.error(ErrorCode::ExpectedColon)),
|
||||
None => Err(self.error(ErrorCode::EOFWhileParsingObject)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter> de::Deserializer for Deserializer<Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<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>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
match try!(self.peek_or_null()) {
|
||||
b'n' => {
|
||||
self.eat_char();
|
||||
try!(self.parse_ident(b"ull"));
|
||||
visitor.visit_none()
|
||||
}
|
||||
_ => {
|
||||
visitor.visit_some(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parses a newtype struct as the underlying value.
|
||||
#[inline]
|
||||
fn visit_newtype_struct<V>(&mut self,
|
||||
_name: &str,
|
||||
mut visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
/// 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,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
mut visitor: V) -> Result<V::Value>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
match try!(self.next_char_or_null()) {
|
||||
b'{' => {
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
let value = {
|
||||
try!(visitor.visit(&mut *self))
|
||||
};
|
||||
|
||||
try!(self.parse_whitespace());
|
||||
|
||||
match try!(self.next_char_or_null()) {
|
||||
b'}' => {
|
||||
Ok(value)
|
||||
}
|
||||
_ => {
|
||||
Err(self.error(ErrorCode::ExpectedSomeValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
Err(self.error(ErrorCode::ExpectedSomeValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn format() -> &'static str {
|
||||
"json"
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqVisitor<'a, Iter: 'a + Iterator<Item=io::Result<u8>>> {
|
||||
de: &'a mut Deserializer<Iter>,
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Item=io::Result<u8>>> SeqVisitor<'a, Iter> {
|
||||
fn new(de: &'a mut Deserializer<Iter>) -> Self {
|
||||
SeqVisitor {
|
||||
de: de,
|
||||
first: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter> de::SeqVisitor for SeqVisitor<'a, Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>>
|
||||
where T: de::Deserialize,
|
||||
{
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
match try!(self.de.peek()) {
|
||||
Some(b']') => {
|
||||
return Ok(None);
|
||||
}
|
||||
Some(b',') if !self.first => {
|
||||
self.de.eat_char();
|
||||
}
|
||||
Some(_) => {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
return Err(self.de.error(ErrorCode::ExpectedListCommaOrEnd));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Err(self.de.error(ErrorCode::EOFWhileParsingList));
|
||||
}
|
||||
}
|
||||
|
||||
let value = try!(de::Deserialize::deserialize(self.de));
|
||||
Ok(Some(value))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<()> {
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
match try!(self.de.next_char()) {
|
||||
Some(b']') => { Ok(()) }
|
||||
Some(_) => {
|
||||
Err(self.de.error(ErrorCode::TrailingCharacters))
|
||||
}
|
||||
None => {
|
||||
Err(self.de.error(ErrorCode::EOFWhileParsingList))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct MapVisitor<'a, Iter: 'a + Iterator<Item=io::Result<u8>>> {
|
||||
de: &'a mut Deserializer<Iter>,
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<'a, Iter: Iterator<Item=io::Result<u8>>> MapVisitor<'a, Iter> {
|
||||
fn new(de: &'a mut Deserializer<Iter>) -> Self {
|
||||
MapVisitor {
|
||||
de: de,
|
||||
first: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Iter> de::MapVisitor for MapVisitor<'a, Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key<K>(&mut self) -> Result<Option<K>>
|
||||
where K: de::Deserialize,
|
||||
{
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
match try!(self.de.peek()) {
|
||||
Some(b'}') => {
|
||||
return Ok(None);
|
||||
}
|
||||
Some(b',') if !self.first => {
|
||||
self.de.eat_char();
|
||||
try!(self.de.parse_whitespace());
|
||||
}
|
||||
Some(_) => {
|
||||
if self.first {
|
||||
self.first = false;
|
||||
} else {
|
||||
return Err(self.de.error(ErrorCode::ExpectedObjectCommaOrEnd));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Err(self.de.error(ErrorCode::EOFWhileParsingObject));
|
||||
}
|
||||
}
|
||||
|
||||
match try!(self.de.peek()) {
|
||||
Some(b'"') => {
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
Some(_) => {
|
||||
Err(self.de.error(ErrorCode::KeyMustBeAString))
|
||||
}
|
||||
None => {
|
||||
Err(self.de.error(ErrorCode::EOFWhileParsingValue))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_value<V>(&mut self) -> Result<V>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
try!(self.de.parse_object_colon());
|
||||
|
||||
Ok(try!(de::Deserialize::deserialize(self.de)))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<()> {
|
||||
try!(self.de.parse_whitespace());
|
||||
|
||||
match try!(self.de.next_char()) {
|
||||
Some(b'}') => { Ok(()) }
|
||||
Some(_) => {
|
||||
Err(self.de.error(ErrorCode::TrailingCharacters))
|
||||
}
|
||||
None => {
|
||||
Err(self.de.error(ErrorCode::EOFWhileParsingObject))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_field<V>(&mut self, _field: &'static str) -> Result<V>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
let mut de = de::value::ValueDeserializer::into_deserializer(());
|
||||
Ok(try!(de::Deserialize::deserialize(&mut de)))
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter> de::VariantVisitor for Deserializer<Iter>
|
||||
where Iter: Iterator<Item=io::Result<u8>>,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V>
|
||||
where V: de::Deserialize
|
||||
{
|
||||
let val = try!(de::Deserialize::deserialize(self));
|
||||
try!(self.parse_object_colon());
|
||||
Ok(val)
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<()> {
|
||||
de::Deserialize::deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_newtype<T>(&mut self) -> Result<T>
|
||||
where T: de::Deserialize,
|
||||
{
|
||||
de::Deserialize::deserialize(self)
|
||||
}
|
||||
|
||||
fn visit_tuple<V>(&mut self,
|
||||
_len: usize,
|
||||
visitor: V) -> Result<V::Value>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
de::Deserializer::visit(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)
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `std::io::Read`.
|
||||
pub fn from_iter<I, T>(iter: I) -> Result<T>
|
||||
where I: Iterator<Item=io::Result<u8>>,
|
||||
T: de::Deserialize,
|
||||
{
|
||||
let mut de = Deserializer::new(iter);
|
||||
let value = try!(de::Deserialize::deserialize(&mut de));
|
||||
|
||||
// Make sure the whole stream has been consumed.
|
||||
try!(de.end());
|
||||
Ok(value)
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `std::io::Read`.
|
||||
pub fn from_reader<R, T>(rdr: R) -> Result<T>
|
||||
where R: io::Read,
|
||||
T: de::Deserialize,
|
||||
{
|
||||
from_iter(rdr.bytes())
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `&str`.
|
||||
pub fn from_slice<T>(v: &[u8]) -> Result<T>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
from_iter(v.iter().map(|byte| Ok(*byte)))
|
||||
}
|
||||
|
||||
/// Decodes a json value from a `&str`.
|
||||
pub fn from_str<T>(s: &str) -> Result<T>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
from_slice(s.as_bytes())
|
||||
}
|
@ -1,175 +0,0 @@
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::result;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use serde::de;
|
||||
|
||||
/// The errors that can arise while parsing a JSON stream.
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum ErrorCode {
|
||||
EOFWhileParsingList,
|
||||
EOFWhileParsingObject,
|
||||
EOFWhileParsingString,
|
||||
EOFWhileParsingValue,
|
||||
ExpectedColon,
|
||||
ExpectedConversion,
|
||||
ExpectedEnumEnd,
|
||||
ExpectedEnumEndToken,
|
||||
ExpectedEnumMapStart,
|
||||
ExpectedEnumToken,
|
||||
ExpectedEnumVariantString,
|
||||
ExpectedListCommaOrEnd,
|
||||
ExpectedName,
|
||||
ExpectedObjectCommaOrEnd,
|
||||
ExpectedSomeIdent,
|
||||
ExpectedSomeValue,
|
||||
InvalidEscape,
|
||||
InvalidNumber,
|
||||
InvalidUnicodeCodePoint,
|
||||
KeyMustBeAString,
|
||||
LoneLeadingSurrogateInHexEscape,
|
||||
UnknownField(String),
|
||||
MissingField(&'static str),
|
||||
NotFourDigit,
|
||||
NotUtf8,
|
||||
TrailingCharacters,
|
||||
UnexpectedEndOfHexEscape,
|
||||
UnknownVariant,
|
||||
UnrecognizedHex,
|
||||
}
|
||||
|
||||
impl fmt::Debug for ErrorCode {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
use std::fmt::Debug;
|
||||
|
||||
match *self {
|
||||
ErrorCode::EOFWhileParsingList => "EOF While parsing list".fmt(f),
|
||||
ErrorCode::EOFWhileParsingObject => "EOF While parsing object".fmt(f),
|
||||
ErrorCode::EOFWhileParsingString => "EOF While parsing string".fmt(f),
|
||||
ErrorCode::EOFWhileParsingValue => "EOF While parsing value".fmt(f),
|
||||
ErrorCode::ExpectedColon => "expected `:`".fmt(f),
|
||||
ErrorCode::ExpectedConversion => "expected conversion".fmt(f),
|
||||
ErrorCode::ExpectedEnumEnd => "expected enum end".fmt(f),
|
||||
ErrorCode::ExpectedEnumEndToken => "expected enum map end".fmt(f),
|
||||
ErrorCode::ExpectedEnumMapStart => "expected enum map start".fmt(f),
|
||||
ErrorCode::ExpectedEnumToken => "expected enum token".fmt(f),
|
||||
ErrorCode::ExpectedEnumVariantString => "expected variant".fmt(f),
|
||||
ErrorCode::ExpectedListCommaOrEnd => "expected `,` or `]`".fmt(f),
|
||||
ErrorCode::ExpectedName => "expected name".fmt(f),
|
||||
ErrorCode::ExpectedObjectCommaOrEnd => "expected `,` or `}`".fmt(f),
|
||||
ErrorCode::ExpectedSomeIdent => "expected ident".fmt(f),
|
||||
ErrorCode::ExpectedSomeValue => "expected value".fmt(f),
|
||||
ErrorCode::InvalidEscape => "invalid escape".fmt(f),
|
||||
ErrorCode::InvalidNumber => "invalid number".fmt(f),
|
||||
ErrorCode::InvalidUnicodeCodePoint => "invalid unicode code point".fmt(f),
|
||||
ErrorCode::KeyMustBeAString => "key must be a string".fmt(f),
|
||||
ErrorCode::LoneLeadingSurrogateInHexEscape => "lone leading surrogate in hex escape".fmt(f),
|
||||
ErrorCode::UnknownField(ref field) => write!(f, "unknown field \"{}\"", field),
|
||||
ErrorCode::MissingField(ref field) => write!(f, "missing field \"{}\"", field),
|
||||
ErrorCode::NotFourDigit => "invalid \\u escape (not four digits)".fmt(f),
|
||||
ErrorCode::NotUtf8 => "contents not utf-8".fmt(f),
|
||||
ErrorCode::TrailingCharacters => "trailing characters".fmt(f),
|
||||
ErrorCode::UnexpectedEndOfHexEscape => "unexpected end of hex escape".fmt(f),
|
||||
ErrorCode::UnknownVariant => "unknown variant".fmt(f),
|
||||
ErrorCode::UnrecognizedHex => "invalid \\u escape (unrecognized hex)".fmt(f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// msg, line, col
|
||||
SyntaxError(ErrorCode, usize, usize),
|
||||
IoError(io::Error),
|
||||
MissingFieldError(&'static str),
|
||||
FromUtf8Error(FromUtf8Error),
|
||||
}
|
||||
|
||||
impl error::Error for Error {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
Error::SyntaxError(..) => "syntax error",
|
||||
Error::IoError(ref error) => error::Error::description(error),
|
||||
Error::MissingFieldError(_) => "missing field",
|
||||
Error::FromUtf8Error(ref error) => error.description(),
|
||||
}
|
||||
}
|
||||
|
||||
fn cause(&self) -> Option<&error::Error> {
|
||||
match *self {
|
||||
Error::IoError(ref error) => Some(error),
|
||||
Error::FromUtf8Error(ref error) => Some(error),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
Error::SyntaxError(ref code, line, col) => {
|
||||
write!(fmt, "{:?} at line {} column {}", code, line, col)
|
||||
}
|
||||
Error::IoError(ref error) => fmt::Display::fmt(error, fmt),
|
||||
Error::FromUtf8Error(ref error) => fmt::Display::fmt(error, fmt),
|
||||
Error::MissingFieldError(ref field) => {
|
||||
write!(fmt, "missing field {}", field)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(error: io::Error) -> Error {
|
||||
Error::IoError(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FromUtf8Error> for Error {
|
||||
fn from(error: FromUtf8Error) -> Error {
|
||||
Error::FromUtf8Error(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<de::value::Error> for Error {
|
||||
fn from(error: de::value::Error) -> Error {
|
||||
match error {
|
||||
de::value::Error::SyntaxError => {
|
||||
Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)
|
||||
}
|
||||
de::value::Error::EndOfStreamError => {
|
||||
de::Error::end_of_stream()
|
||||
}
|
||||
de::value::Error::UnknownFieldError(field) => {
|
||||
Error::SyntaxError(ErrorCode::UnknownField(field), 0, 0)
|
||||
}
|
||||
de::value::Error::MissingFieldError(field) => {
|
||||
de::Error::missing_field(field)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Error for Error {
|
||||
fn syntax(_: &str) -> Error {
|
||||
Error::SyntaxError(ErrorCode::ExpectedSomeValue, 0, 0)
|
||||
}
|
||||
|
||||
fn end_of_stream() -> Error {
|
||||
Error::SyntaxError(ErrorCode::EOFWhileParsingValue, 0, 0)
|
||||
}
|
||||
|
||||
fn unknown_field(field: &str) -> Error {
|
||||
Error::SyntaxError(ErrorCode::UnknownField(field.to_string()), 0, 0)
|
||||
}
|
||||
|
||||
fn missing_field(field: &'static str) -> Error {
|
||||
Error::MissingFieldError(field)
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper alias for `Result` objects that return a JSON `Error`.
|
||||
pub type Result<T> = result::Result<T, Error>;
|
@ -1,122 +0,0 @@
|
||||
//! JSON and serialization
|
||||
//!
|
||||
//! # What is JSON?
|
||||
//!
|
||||
//! JSON (JavaScript Object Notation) is a way to write data in JavaScript. Like XML, it allows to
|
||||
//! encode structured data in a text format that can be easily read by humans. Its simple syntax
|
||||
//! and native compatibility with JavaScript have made it a widely used format.
|
||||
//!
|
||||
//! Data types that can be encoded are JavaScript types (see the `serde_json:Value` enum for more
|
||||
//! details):
|
||||
//!
|
||||
//! * `Boolean`: equivalent to rust's `bool`
|
||||
//! * `I64`: equivalent to rust's `i64`
|
||||
//! * `U64`: equivalent to rust's `u64`
|
||||
//! * `F64`: equivalent to rust's `i64`
|
||||
//! * `String`: equivalent to rust's `String`
|
||||
//! * `Array`: equivalent to rust's `Vec<T>`, but also allowing objects of different types in the
|
||||
//! same array
|
||||
//! * `Object`: equivalent to rust's `BTreeMap<String, serde_json::Value>`
|
||||
//! * `Null`
|
||||
//!
|
||||
//! An object is a series of string keys mapping to values, in `"key": value` format. Arrays are
|
||||
//! enclosed in square brackets ([ ... ]) and objects in curly brackets ({ ... }). A simple JSON
|
||||
//! document encoding a person, his/her age, address and phone numbers could look like
|
||||
//!
|
||||
//! ```ignore
|
||||
//! {
|
||||
//! "FirstName": "John",
|
||||
//! "LastName": "Doe",
|
||||
//! "Age": 43,
|
||||
//! "Address": {
|
||||
//! "Street": "Downing Street 10",
|
||||
//! "City": "London",
|
||||
//! "Country": "Great Britain"
|
||||
//! },
|
||||
//! "PhoneNumbers": [
|
||||
//! "+44 1234567",
|
||||
//! "+44 2345678"
|
||||
//! ]
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! # Type-based Serialization and Deserialization
|
||||
//!
|
||||
//! Serde provides a mechanism for low boilerplate serialization & deserialization of values to and
|
||||
//! from JSON via the serialization API. To be able to serialize a piece of data, it must implement
|
||||
//! the `serde::Serialize` trait. To be able to deserialize a piece of data, it must implement the
|
||||
//! `serde::Deserialize` trait. Serde provides provides an annotation to automatically generate
|
||||
//! the code for these traits: `#[derive(Serialize, Deserialize)]`.
|
||||
//!
|
||||
//! The JSON API also provides an enum `serde_json::Value` and a method `to_value` to serialize
|
||||
//! objects. A `serde_json::Value` value can be serialized as a string or buffer using the
|
||||
//! functions described above. You can also use the `json::Serializer` object, which implements the
|
||||
//! `Serializer` trait.
|
||||
//!
|
||||
//! # Examples of use
|
||||
//!
|
||||
//! ## Parsing a `str` to `Value` and reading the result
|
||||
//!
|
||||
//! ```rust
|
||||
//! //#![feature(custom_derive, plugin)]
|
||||
//! //#![plugin(serde_macros)]
|
||||
//!
|
||||
//! extern crate serde_json;
|
||||
//!
|
||||
//! use serde_json::Value;
|
||||
//!
|
||||
//! fn main() {
|
||||
//! let data: Value = serde_json::from_str("{\"foo\": 13, \"bar\": \"baz\"}").unwrap();
|
||||
//! println!("data: {:?}", data);
|
||||
//! // data: {"bar":"baz","foo":13}
|
||||
//! println!("object? {}", data.is_object());
|
||||
//! // object? true
|
||||
//!
|
||||
//! let obj = data.as_object().unwrap();
|
||||
//! let foo = obj.get("foo").unwrap();
|
||||
//!
|
||||
//! println!("array? {:?}", foo.as_array());
|
||||
//! // array? None
|
||||
//! println!("u64? {:?}", foo.as_u64());
|
||||
//! // u64? Some(13u64)
|
||||
//!
|
||||
//! for (key, value) in obj.iter() {
|
||||
//! println!("{}: {}", key, match *value {
|
||||
//! Value::U64(v) => format!("{} (u64)", v),
|
||||
//! Value::String(ref v) => format!("{} (string)", v),
|
||||
//! _ => format!("other")
|
||||
//! });
|
||||
//! }
|
||||
//! // bar: baz (string)
|
||||
//! // foo: 13 (u64)
|
||||
//! }
|
||||
//! ```
|
||||
|
||||
extern crate num;
|
||||
extern crate serde;
|
||||
|
||||
pub use self::de::{
|
||||
Deserializer,
|
||||
from_iter,
|
||||
from_reader,
|
||||
from_slice,
|
||||
from_str,
|
||||
};
|
||||
pub use self::error::{Error, ErrorCode, Result};
|
||||
pub use self::ser::{
|
||||
Serializer,
|
||||
to_writer,
|
||||
to_writer_pretty,
|
||||
to_vec,
|
||||
to_vec_pretty,
|
||||
to_string,
|
||||
to_string_pretty,
|
||||
escape_str,
|
||||
};
|
||||
pub use self::value::{Value, to_value, from_value};
|
||||
|
||||
pub mod builder;
|
||||
pub mod de;
|
||||
pub mod error;
|
||||
pub mod ser;
|
||||
pub mod value;
|
@ -1,637 +0,0 @@
|
||||
use std::io;
|
||||
use std::num::FpCategory;
|
||||
|
||||
use serde::ser;
|
||||
use super::error::{Error, ErrorCode, Result};
|
||||
|
||||
/// A structure for implementing serialization to JSON.
|
||||
pub struct Serializer<W, F=CompactFormatter> {
|
||||
writer: W,
|
||||
formatter: F,
|
||||
|
||||
/// `first` is used to signify if we should print a comma when we are walking through a
|
||||
/// sequence.
|
||||
first: bool,
|
||||
}
|
||||
|
||||
impl<W> Serializer<W>
|
||||
where W: io::Write,
|
||||
{
|
||||
/// Creates a new JSON serializer.
|
||||
#[inline]
|
||||
pub fn new(writer: W) -> Self {
|
||||
Serializer::with_formatter(writer, CompactFormatter)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W> Serializer<W, PrettyFormatter<'a>>
|
||||
where W: io::Write,
|
||||
{
|
||||
/// Creates a new JSON pretty print serializer.
|
||||
#[inline]
|
||||
pub fn pretty(writer: W) -> Self {
|
||||
Serializer::with_formatter(writer, PrettyFormatter::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<W, F> Serializer<W, F>
|
||||
where W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
/// Creates a new JSON visitor whose output will be written to the writer
|
||||
/// specified.
|
||||
#[inline]
|
||||
pub fn with_formatter(writer: W, formatter: F) -> Self {
|
||||
Serializer {
|
||||
writer: writer,
|
||||
formatter: formatter,
|
||||
first: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Unwrap the `Writer` from the `Serializer`.
|
||||
#[inline]
|
||||
pub fn into_inner(self) -> W {
|
||||
self.writer
|
||||
}
|
||||
}
|
||||
|
||||
impl<W, F> ser::Serializer for Serializer<W, F>
|
||||
where W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit_bool(&mut self, value: bool) -> Result<()> {
|
||||
if value {
|
||||
self.writer.write_all(b"true").map_err(From::from)
|
||||
} else {
|
||||
self.writer.write_all(b"false").map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_isize(&mut self, value: isize) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i8(&mut self, value: i8) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i16(&mut self, value: i16) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i32(&mut self, value: i32) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, value: i64) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_usize(&mut self, value: usize) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u8(&mut self, value: u8) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u16(&mut self, value: u16) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u32(&mut self, value: u32) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, value: u64) -> Result<()> {
|
||||
write!(&mut self.writer, "{}", value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f32(&mut self, value: f32) -> Result<()> {
|
||||
fmt_f32_or_null(&mut self.writer, value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64(&mut self, value: f64) -> Result<()> {
|
||||
fmt_f64_or_null(&mut self.writer, value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_char(&mut self, value: char) -> Result<()> {
|
||||
escape_char(&mut self.writer, value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str(&mut self, value: &str) -> Result<()> {
|
||||
escape_str(&mut self.writer, value).map_err(From::from)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none(&mut self) -> Result<()> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<V>(&mut self, value: V) -> Result<()>
|
||||
where V: ser::Serialize
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit(&mut self) -> Result<()> {
|
||||
self.writer.write_all(b"null").map_err(From::from)
|
||||
}
|
||||
|
||||
/// Override `visit_newtype_struct` to serialize newtypes without an object wrapper.
|
||||
#[inline]
|
||||
fn visit_newtype_struct<T>(&mut self,
|
||||
_name: &'static str,
|
||||
value: T) -> Result<()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit_variant(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str) -> Result<()> {
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
try!(self.formatter.comma(&mut self.writer, true));
|
||||
try!(self.visit_str(variant));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(self.writer.write_all(b"[]"));
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_newtype_variant<T>(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
value: T) -> Result<()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
try!(self.formatter.comma(&mut self.writer, true));
|
||||
try!(self.visit_str(variant));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(value.serialize(self));
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
match visitor.len() {
|
||||
Some(len) if len == 0 => {
|
||||
self.writer.write_all(b"[]").map_err(From::from)
|
||||
}
|
||||
_ => {
|
||||
try!(self.formatter.open(&mut self.writer, b'['));
|
||||
|
||||
self.first = true;
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
self.formatter.close(&mut self.writer, b']').map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_tuple_variant<V>(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
visitor: V) -> Result<()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
try!(self.formatter.comma(&mut self.writer, true));
|
||||
try!(self.visit_str(variant));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(self.visit_seq(visitor));
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
try!(self.formatter.comma(&mut self.writer, self.first));
|
||||
try!(value.serialize(self));
|
||||
|
||||
self.first = false;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, mut visitor: V) -> Result<()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
match visitor.len() {
|
||||
Some(len) if len == 0 => {
|
||||
self.writer.write_all(b"{}").map_err(From::from)
|
||||
}
|
||||
_ => {
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
|
||||
self.first = true;
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_struct_variant<V>(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
visitor: V) -> Result<()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
try!(self.formatter.open(&mut self.writer, b'{'));
|
||||
try!(self.formatter.comma(&mut self.writer, true));
|
||||
try!(self.visit_str(variant));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(self.visit_map(visitor));
|
||||
|
||||
self.formatter.close(&mut self.writer, b'}')
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<()>
|
||||
where K: ser::Serialize,
|
||||
V: ser::Serialize,
|
||||
{
|
||||
try!(self.formatter.comma(&mut self.writer, self.first));
|
||||
|
||||
try!(key.serialize(&mut MapKeySerializer { ser: self }));
|
||||
try!(self.formatter.colon(&mut self.writer));
|
||||
try!(value.serialize(self));
|
||||
|
||||
self.first = false;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn format() -> &'static str {
|
||||
"json"
|
||||
}
|
||||
}
|
||||
|
||||
struct MapKeySerializer<'a, W: 'a, F: 'a> {
|
||||
ser: &'a mut Serializer<W, F>,
|
||||
}
|
||||
|
||||
impl<'a, W, F> ser::Serializer for MapKeySerializer<'a, W, F>
|
||||
where W: io::Write,
|
||||
F: Formatter,
|
||||
{
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit_str(&mut self, value: &str) -> Result<()> {
|
||||
self.ser.visit_str(value)
|
||||
}
|
||||
|
||||
fn visit_bool(&mut self, _value: bool) -> Result<()> {
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_i64(&mut self, _value: i64) -> Result<()> {
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_u64(&mut self, _value: u64) -> Result<()> {
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_f64(&mut self, _value: f64) -> Result<()> {
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<()> {
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_none(&mut self) -> Result<()> {
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_some<V>(&mut self, _value: V) -> Result<()>
|
||||
where V: ser::Serialize
|
||||
{
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_seq<V>(&mut self, _visitor: V) -> Result<()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_seq_elt<T>(&mut self, _value: T) -> Result<()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_map<V>(&mut self, _visitor: V) -> Result<()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
|
||||
fn visit_map_elt<K, V>(&mut self, _key: K, _value: V) -> Result<()>
|
||||
where K: ser::Serialize,
|
||||
V: ser::Serialize,
|
||||
{
|
||||
Err(Error::SyntaxError(ErrorCode::KeyMustBeAString, 0, 0))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Formatter {
|
||||
fn open<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
|
||||
where W: io::Write;
|
||||
|
||||
fn comma<W>(&mut self, writer: &mut W, first: bool) -> Result<()>
|
||||
where W: io::Write;
|
||||
|
||||
fn colon<W>(&mut self, writer: &mut W) -> Result<()>
|
||||
where W: io::Write;
|
||||
|
||||
fn close<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
|
||||
where W: io::Write;
|
||||
}
|
||||
|
||||
pub struct CompactFormatter;
|
||||
|
||||
impl Formatter for CompactFormatter {
|
||||
fn open<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(&[ch]).map_err(From::from)
|
||||
}
|
||||
|
||||
fn comma<W>(&mut self, writer: &mut W, first: bool) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
if first {
|
||||
Ok(())
|
||||
} else {
|
||||
writer.write_all(b",").map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
||||
fn colon<W>(&mut self, writer: &mut W) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(b":").map_err(From::from)
|
||||
}
|
||||
|
||||
fn close<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(&[ch]).map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct PrettyFormatter<'a> {
|
||||
current_indent: usize,
|
||||
indent: &'a [u8],
|
||||
}
|
||||
|
||||
impl<'a> PrettyFormatter<'a> {
|
||||
fn new() -> Self {
|
||||
PrettyFormatter::with_indent(b" ")
|
||||
}
|
||||
|
||||
fn with_indent(indent: &'a [u8]) -> Self {
|
||||
PrettyFormatter {
|
||||
current_indent: 0,
|
||||
indent: indent,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Formatter for PrettyFormatter<'a> {
|
||||
fn open<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
self.current_indent += 1;
|
||||
writer.write_all(&[ch]).map_err(From::from)
|
||||
}
|
||||
|
||||
fn comma<W>(&mut self, writer: &mut W, first: bool) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
if first {
|
||||
try!(writer.write_all(b"\n"));
|
||||
} else {
|
||||
try!(writer.write_all(b",\n"));
|
||||
}
|
||||
|
||||
indent(writer, self.current_indent, self.indent)
|
||||
}
|
||||
|
||||
fn colon<W>(&mut self, writer: &mut W) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
writer.write_all(b": ").map_err(From::from)
|
||||
}
|
||||
|
||||
fn close<W>(&mut self, writer: &mut W, ch: u8) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
self.current_indent -= 1;
|
||||
try!(writer.write(b"\n"));
|
||||
try!(indent(writer, self.current_indent, self.indent));
|
||||
|
||||
writer.write_all(&[ch]).map_err(From::from)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn escape_bytes<W>(wr: &mut W, bytes: &[u8]) -> Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
try!(wr.write_all(b"\""));
|
||||
|
||||
let mut start = 0;
|
||||
|
||||
for (i, byte) in bytes.iter().enumerate() {
|
||||
let escaped = match *byte {
|
||||
b'"' => b"\\\"",
|
||||
b'\\' => b"\\\\",
|
||||
b'\x08' => b"\\b",
|
||||
b'\x0c' => b"\\f",
|
||||
b'\n' => b"\\n",
|
||||
b'\r' => b"\\r",
|
||||
b'\t' => b"\\t",
|
||||
_ => { continue; }
|
||||
};
|
||||
|
||||
if start < i {
|
||||
try!(wr.write_all(&bytes[start..i]));
|
||||
}
|
||||
|
||||
try!(wr.write_all(escaped));
|
||||
|
||||
start = i + 1;
|
||||
}
|
||||
|
||||
if start != bytes.len() {
|
||||
try!(wr.write_all(&bytes[start..]));
|
||||
}
|
||||
|
||||
try!(wr.write_all(b"\""));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn escape_str<W>(wr: &mut W, value: &str) -> Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
escape_bytes(wr, value.as_bytes())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn escape_char<W>(wr: &mut W, value: char) -> Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
// FIXME: this allocation is required in order to be compatible with stable
|
||||
// rust, which doesn't support encoding a `char` into a stack buffer.
|
||||
escape_bytes(wr, value.to_string().as_bytes())
|
||||
}
|
||||
|
||||
fn fmt_f32_or_null<W>(wr: &mut W, value: f32) -> Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
match value.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => {
|
||||
try!(wr.write_all(b"null"))
|
||||
}
|
||||
_ => {
|
||||
try!(write!(wr, "{:?}", value))
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fmt_f64_or_null<W>(wr: &mut W, value: f64) -> Result<()>
|
||||
where W: io::Write
|
||||
{
|
||||
match value.classify() {
|
||||
FpCategory::Nan | FpCategory::Infinite => {
|
||||
try!(wr.write_all(b"null"))
|
||||
}
|
||||
_ => {
|
||||
try!(write!(wr, "{:?}", value))
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` writer.
|
||||
#[inline]
|
||||
pub fn to_writer<W, T>(writer: &mut W, value: &T) -> Result<()>
|
||||
where W: io::Write,
|
||||
T: ser::Serialize,
|
||||
{
|
||||
let mut ser = Serializer::new(writer);
|
||||
try!(value.serialize(&mut ser));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` writer.
|
||||
#[inline]
|
||||
pub fn to_writer_pretty<W, T>(writer: &mut W, value: &T) -> Result<()>
|
||||
where W: io::Write,
|
||||
T: ser::Serialize,
|
||||
{
|
||||
let mut ser = Serializer::pretty(writer);
|
||||
try!(value.serialize(&mut ser));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` buffer.
|
||||
#[inline]
|
||||
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
// We are writing to a Vec, which doesn't fail. So we can ignore
|
||||
// the error.
|
||||
let mut writer = Vec::with_capacity(128);
|
||||
try!(to_writer(&mut writer, value));
|
||||
Ok(writer)
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `[u8]` buffer.
|
||||
#[inline]
|
||||
pub fn to_vec_pretty<T>(value: &T) -> Result<Vec<u8>>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
// We are writing to a Vec, which doesn't fail. So we can ignore
|
||||
// the error.
|
||||
let mut writer = Vec::with_capacity(128);
|
||||
try!(to_writer_pretty(&mut writer, value));
|
||||
Ok(writer)
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `String` buffer.
|
||||
#[inline]
|
||||
pub fn to_string<T>(value: &T) -> Result<String>
|
||||
where T: ser::Serialize
|
||||
{
|
||||
let vec = try!(to_vec(value));
|
||||
let string = try!(String::from_utf8(vec));
|
||||
Ok(string)
|
||||
}
|
||||
|
||||
/// Encode the specified struct into a json `String` buffer.
|
||||
#[inline]
|
||||
pub fn to_string_pretty<T>(value: &T) -> Result<String>
|
||||
where T: ser::Serialize
|
||||
{
|
||||
let vec = try!(to_vec_pretty(value));
|
||||
let string = try!(String::from_utf8(vec));
|
||||
Ok(string)
|
||||
}
|
||||
|
||||
fn indent<W>(wr: &mut W, n: usize, s: &[u8]) -> Result<()>
|
||||
where W: io::Write,
|
||||
{
|
||||
for _ in 0 .. n {
|
||||
try!(wr.write_all(s));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,951 +0,0 @@
|
||||
use std::collections::{BTreeMap, btree_map};
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
use std::str;
|
||||
use std::vec;
|
||||
|
||||
use num::NumCast;
|
||||
|
||||
use serde::de;
|
||||
use serde::ser;
|
||||
|
||||
use error::Error;
|
||||
|
||||
#[derive(Clone, PartialEq)]
|
||||
pub enum Value {
|
||||
Null,
|
||||
Bool(bool),
|
||||
I64(i64),
|
||||
U64(u64),
|
||||
F64(f64),
|
||||
String(String),
|
||||
Array(Vec<Value>),
|
||||
Object(BTreeMap<String, Value>),
|
||||
}
|
||||
|
||||
impl Value {
|
||||
/// If the `Value` is an Object, returns the value associated with the provided key.
|
||||
/// Otherwise, returns None.
|
||||
pub fn find<'a>(&'a self, key: &str) -> Option<&'a Value>{
|
||||
match self {
|
||||
&Value::Object(ref map) => map.get(key),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to get a nested Value Object for each key in `keys`.
|
||||
/// If any key is found not to exist, find_path will return None.
|
||||
/// Otherwise, it will return the `Value` associated with the final key.
|
||||
pub fn find_path<'a>(&'a self, keys: &[&str]) -> Option<&'a Value>{
|
||||
let mut target = self;
|
||||
for key in keys {
|
||||
match target.find(key) {
|
||||
Some(t) => { target = t; },
|
||||
None => return None
|
||||
}
|
||||
}
|
||||
Some(target)
|
||||
}
|
||||
|
||||
/// Looks up a value by path.
|
||||
///
|
||||
/// This is a convenience method that splits the path by `'.'`
|
||||
/// and then feeds the sequence of keys into the `find_path`
|
||||
/// method.
|
||||
///
|
||||
/// ``` ignore
|
||||
/// let obj: Value = json::from_str(r#"{"x": {"a": 1}}"#).unwrap();
|
||||
///
|
||||
/// assert!(obj.lookup("x.a").unwrap() == &Value::U64(1));
|
||||
/// ```
|
||||
pub fn lookup<'a>(&'a self, path: &str) -> Option<&'a Value> {
|
||||
let mut target = self;
|
||||
for key in path.split('.') {
|
||||
match target.find(key) {
|
||||
Some(t) => { target = t; },
|
||||
None => return None
|
||||
}
|
||||
}
|
||||
Some(target)
|
||||
}
|
||||
|
||||
/// If the `Value` is an Object, performs a depth-first search until
|
||||
/// a value associated with the provided key is found. If no value is found
|
||||
/// or the `Value` is not an Object, returns None.
|
||||
pub fn search<'a>(&'a self, key: &str) -> Option<&'a Value> {
|
||||
match self {
|
||||
&Value::Object(ref map) => {
|
||||
match map.get(key) {
|
||||
Some(json_value) => Some(json_value),
|
||||
None => {
|
||||
for (_, v) in map.iter() {
|
||||
match v.search(key) {
|
||||
x if x.is_some() => return x,
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
}
|
||||
},
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is an Object. Returns false otherwise.
|
||||
pub fn is_object<'a>(&'a self) -> bool {
|
||||
self.as_object().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is an Object, returns the associated BTreeMap.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_object<'a>(&'a self) -> Option<&'a BTreeMap<String, Value>> {
|
||||
match self {
|
||||
&Value::Object(ref map) => Some(map),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is an Object, returns the associated mutable BTreeMap.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_object_mut<'a>(&'a mut self) -> Option<&'a mut BTreeMap<String, Value>> {
|
||||
match self {
|
||||
&mut Value::Object(ref mut map) => Some(map),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is an Array. Returns false otherwise.
|
||||
pub fn is_array<'a>(&'a self) -> bool {
|
||||
self.as_array().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is an Array, returns the associated vector.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_array<'a>(&'a self) -> Option<&'a Vec<Value>> {
|
||||
match self {
|
||||
&Value::Array(ref array) => Some(&*array),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is an Array, returns the associated mutable vector.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_array_mut<'a>(&'a mut self) -> Option<&'a mut Vec<Value>> {
|
||||
match self {
|
||||
&mut Value::Array(ref mut list) => Some(list),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a String. Returns false otherwise.
|
||||
pub fn is_string<'a>(&'a self) -> bool {
|
||||
self.as_string().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is a String, returns the associated str.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_string<'a>(&'a self) -> Option<&'a str> {
|
||||
match *self {
|
||||
Value::String(ref s) => Some(&s),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a Number. Returns false otherwise.
|
||||
pub fn is_number(&self) -> bool {
|
||||
match *self {
|
||||
Value::I64(_) | Value::U64(_) | Value::F64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a i64. Returns false otherwise.
|
||||
pub fn is_i64(&self) -> bool {
|
||||
match *self {
|
||||
Value::I64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a u64. Returns false otherwise.
|
||||
pub fn is_u64(&self) -> bool {
|
||||
match *self {
|
||||
Value::U64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a f64. Returns false otherwise.
|
||||
pub fn is_f64(&self) -> bool {
|
||||
match *self {
|
||||
Value::F64(_) => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is a number, return or cast it to a i64.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_i64(&self) -> Option<i64> {
|
||||
match *self {
|
||||
Value::I64(n) => Some(n),
|
||||
Value::U64(n) => NumCast::from(n),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is a number, return or cast it to a u64.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_u64(&self) -> Option<u64> {
|
||||
match *self {
|
||||
Value::I64(n) => NumCast::from(n),
|
||||
Value::U64(n) => Some(n),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// If the `Value` is a number, return or cast it to a f64.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_f64(&self) -> Option<f64> {
|
||||
match *self {
|
||||
Value::I64(n) => NumCast::from(n),
|
||||
Value::U64(n) => NumCast::from(n),
|
||||
Value::F64(n) => Some(n),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a Boolean. Returns false otherwise.
|
||||
pub fn is_boolean(&self) -> bool {
|
||||
self.as_boolean().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is a Boolean, returns the associated bool.
|
||||
/// Returns None otherwise.
|
||||
pub fn as_boolean(&self) -> Option<bool> {
|
||||
match self {
|
||||
&Value::Bool(b) => Some(b),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if the `Value` is a Null. Returns false otherwise.
|
||||
pub fn is_null(&self) -> bool {
|
||||
self.as_null().is_some()
|
||||
}
|
||||
|
||||
/// If the `Value` is a Null, returns ().
|
||||
/// Returns None otherwise.
|
||||
pub fn as_null(&self) -> Option<()> {
|
||||
match self {
|
||||
&Value::Null => Some(()),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::Serialize for Value {
|
||||
#[inline]
|
||||
fn serialize<S>(&self, serializer: &mut S) -> Result<(), S::Error>
|
||||
where S: ser::Serializer,
|
||||
{
|
||||
match *self {
|
||||
Value::Null => serializer.visit_unit(),
|
||||
Value::Bool(v) => serializer.visit_bool(v),
|
||||
Value::I64(v) => serializer.visit_i64(v),
|
||||
Value::U64(v) => serializer.visit_u64(v),
|
||||
Value::F64(v) => serializer.visit_f64(v),
|
||||
Value::String(ref v) => serializer.visit_str(&v),
|
||||
Value::Array(ref v) => v.serialize(serializer),
|
||||
Value::Object(ref v) => v.serialize(serializer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserialize for Value {
|
||||
#[inline]
|
||||
fn deserialize<D>(deserializer: &mut D) -> Result<Value, D::Error>
|
||||
where D: de::Deserializer,
|
||||
{
|
||||
struct ValueVisitor;
|
||||
|
||||
impl de::Visitor for ValueVisitor {
|
||||
type Value = Value;
|
||||
|
||||
#[inline]
|
||||
fn visit_bool<E>(&mut self, value: bool) -> Result<Value, E> {
|
||||
Ok(Value::Bool(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64<E>(&mut self, value: i64) -> Result<Value, E> {
|
||||
if value < 0 {
|
||||
Ok(Value::I64(value))
|
||||
} else {
|
||||
Ok(Value::U64(value as u64))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64<E>(&mut self, value: u64) -> Result<Value, E> {
|
||||
Ok(Value::U64(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64<E>(&mut self, value: f64) -> Result<Value, E> {
|
||||
Ok(Value::F64(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str<E>(&mut self, value: &str) -> Result<Value, E>
|
||||
where E: de::Error,
|
||||
{
|
||||
self.visit_string(value.to_string())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_string<E>(&mut self, value: String) -> Result<Value, E> {
|
||||
Ok(Value::String(value))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none<E>(&mut self) -> Result<Value, E> {
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<D>(&mut self, deserializer: &mut D) -> Result<Value, D::Error>
|
||||
where D: de::Deserializer,
|
||||
{
|
||||
de::Deserialize::deserialize(deserializer)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit<E>(&mut self) -> Result<Value, E> {
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, visitor: V) -> Result<Value, V::Error>
|
||||
where V: de::SeqVisitor,
|
||||
{
|
||||
let values = try!(de::impls::VecVisitor::new().visit_seq(visitor));
|
||||
Ok(Value::Array(values))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, visitor: V) -> Result<Value, V::Error>
|
||||
where V: de::MapVisitor,
|
||||
{
|
||||
let values = try!(de::impls::BTreeMapVisitor::new().visit_map(visitor));
|
||||
Ok(Value::Object(values))
|
||||
}
|
||||
}
|
||||
|
||||
deserializer.visit(ValueVisitor)
|
||||
}
|
||||
}
|
||||
|
||||
struct WriterFormatter<'a, 'b: 'a> {
|
||||
inner: &'a mut fmt::Formatter<'b>,
|
||||
}
|
||||
|
||||
impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
match self.inner.write_str(str::from_utf8(buf).unwrap()) {
|
||||
Ok(_) => Ok(buf.len()),
|
||||
Err(_) => Err(io::Error::last_os_error()),
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Value {
|
||||
/// Serializes a json value into a string
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
let mut wr = WriterFormatter { inner: f };
|
||||
super::ser::to_writer(&mut wr, self).map_err(|_| fmt::Error)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum State {
|
||||
Value(Value),
|
||||
Array(Vec<Value>),
|
||||
Object(BTreeMap<String, Value>),
|
||||
}
|
||||
|
||||
pub struct Serializer {
|
||||
state: Vec<State>,
|
||||
}
|
||||
|
||||
impl Serializer {
|
||||
pub fn new() -> Serializer {
|
||||
Serializer {
|
||||
state: Vec::with_capacity(4),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn unwrap(mut self) -> Value {
|
||||
match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ser::Serializer for Serializer {
|
||||
type Error = ();
|
||||
|
||||
#[inline]
|
||||
fn visit_bool(&mut self, value: bool) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::Bool(value)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_i64(&mut self, value: i64) -> Result<(), ()> {
|
||||
if value < 0 {
|
||||
self.state.push(State::Value(Value::I64(value)));
|
||||
} else {
|
||||
self.state.push(State::Value(Value::U64(value as u64)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_u64(&mut self, value: u64) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::U64(value)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_f64(&mut self, value: f64) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::F64(value as f64)));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_char(&mut self, value: char) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::String(value.to_string())));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_str(&mut self, value: &str) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::String(value.to_string())));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_none(&mut self) -> Result<(), ()> {
|
||||
self.visit_unit()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_some<V>(&mut self, value: V) -> Result<(), ()>
|
||||
where V: ser::Serialize,
|
||||
{
|
||||
value.serialize(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit(&mut self) -> Result<(), ()> {
|
||||
self.state.push(State::Value(Value::Null));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_unit_variant(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str) -> Result<(), ()> {
|
||||
let mut values = BTreeMap::new();
|
||||
values.insert(variant.to_string(), Value::Array(vec![]));
|
||||
|
||||
self.state.push(State::Value(Value::Object(values)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_newtype_variant<T>(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
value: T) -> Result<(), ()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
let mut values = BTreeMap::new();
|
||||
values.insert(variant.to_string(), to_value(&value));
|
||||
|
||||
self.state.push(State::Value(Value::Object(values)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
let len = visitor.len().unwrap_or(0);
|
||||
let values = Vec::with_capacity(len);
|
||||
|
||||
self.state.push(State::Array(values));
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
let values = match self.state.pop().unwrap() {
|
||||
State::Array(values) => values,
|
||||
state => panic!("Expected array, found {:?}", state),
|
||||
};
|
||||
|
||||
self.state.push(State::Value(Value::Array(values)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_tuple_variant<V>(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
visitor: V) -> Result<(), ()>
|
||||
where V: ser::SeqVisitor,
|
||||
{
|
||||
try!(self.visit_seq(visitor));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
let mut object = BTreeMap::new();
|
||||
|
||||
object.insert(variant.to_string(), value);
|
||||
|
||||
self.state.push(State::Value(Value::Object(object)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_seq_elt<T>(&mut self, value: T) -> Result<(), ()>
|
||||
where T: ser::Serialize,
|
||||
{
|
||||
try!(value.serialize(self));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
match *self.state.last_mut().unwrap() {
|
||||
State::Array(ref mut values) => { values.push(value); }
|
||||
ref state => panic!("expected array, found {:?}", state),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map<V>(&mut self, mut visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
let values = BTreeMap::new();
|
||||
|
||||
self.state.push(State::Object(values));
|
||||
|
||||
while let Some(()) = try!(visitor.visit(self)) { }
|
||||
|
||||
let values = match self.state.pop().unwrap() {
|
||||
State::Object(values) => values,
|
||||
state => panic!("expected object, found {:?}", state),
|
||||
};
|
||||
|
||||
self.state.push(State::Value(Value::Object(values)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_struct_variant<V>(&mut self,
|
||||
_name: &str,
|
||||
_variant_index: usize,
|
||||
variant: &str,
|
||||
visitor: V) -> Result<(), ()>
|
||||
where V: ser::MapVisitor,
|
||||
{
|
||||
try!(self.visit_map(visitor));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
let mut object = BTreeMap::new();
|
||||
|
||||
object.insert(variant.to_string(), value);
|
||||
|
||||
self.state.push(State::Value(Value::Object(object)));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_map_elt<K, V>(&mut self, key: K, value: V) -> Result<(), ()>
|
||||
where K: ser::Serialize,
|
||||
V: ser::Serialize,
|
||||
{
|
||||
try!(key.serialize(self));
|
||||
|
||||
let key = match self.state.pop().unwrap() {
|
||||
State::Value(Value::String(value)) => value,
|
||||
state => panic!("expected key, found {:?}", state),
|
||||
};
|
||||
|
||||
try!(value.serialize(self));
|
||||
|
||||
let value = match self.state.pop().unwrap() {
|
||||
State::Value(value) => value,
|
||||
state => panic!("expected value, found {:?}", state),
|
||||
};
|
||||
|
||||
match *self.state.last_mut().unwrap() {
|
||||
State::Object(ref mut values) => { values.insert(key, value); }
|
||||
ref state => panic!("expected object, found {:?}", state),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn format() -> &'static str {
|
||||
"json"
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Deserializer {
|
||||
value: Option<Value>,
|
||||
}
|
||||
|
||||
impl Deserializer {
|
||||
/// Creates a new deserializer instance for deserializing the specified JSON value.
|
||||
pub fn new(value: Value) -> Deserializer {
|
||||
Deserializer {
|
||||
value: Some(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl de::Deserializer for Deserializer {
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
let value = match self.value.take() {
|
||||
Some(value) => value,
|
||||
None => { return Err(de::Error::end_of_stream()); }
|
||||
};
|
||||
|
||||
match value {
|
||||
Value::Null => visitor.visit_unit(),
|
||||
Value::Bool(v) => visitor.visit_bool(v),
|
||||
Value::I64(v) => visitor.visit_i64(v),
|
||||
Value::U64(v) => visitor.visit_u64(v),
|
||||
Value::F64(v) => visitor.visit_f64(v),
|
||||
Value::String(v) => visitor.visit_string(v),
|
||||
Value::Array(v) => {
|
||||
let len = v.len();
|
||||
visitor.visit_seq(SeqDeserializer {
|
||||
de: self,
|
||||
iter: v.into_iter(),
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
Value::Object(v) => {
|
||||
let len = v.len();
|
||||
visitor.visit_map(MapDeserializer {
|
||||
de: self,
|
||||
iter: v.into_iter(),
|
||||
value: None,
|
||||
len: len,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
match self.value {
|
||||
Some(Value::Null) => visitor.visit_none(),
|
||||
Some(_) => visitor.visit_some(self),
|
||||
None => Err(de::Error::end_of_stream()),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_enum<V>(&mut self,
|
||||
_name: &str,
|
||||
_variants: &'static [&'static str],
|
||||
mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::EnumVisitor,
|
||||
{
|
||||
let value = match self.value.take() {
|
||||
Some(Value::Object(value)) => value,
|
||||
Some(_) => { return Err(de::Error::syntax("expected an enum")); }
|
||||
None => { return Err(de::Error::end_of_stream()); }
|
||||
};
|
||||
|
||||
let mut iter = value.into_iter();
|
||||
|
||||
let (variant, value) = match iter.next() {
|
||||
Some(v) => v,
|
||||
None => return Err(de::Error::syntax("expected a variant name")),
|
||||
};
|
||||
|
||||
// enums are encoded in json as maps with a single key:value pair
|
||||
match iter.next() {
|
||||
Some(_) => Err(de::Error::syntax("expected map")),
|
||||
None => visitor.visit(VariantDeserializer {
|
||||
de: self,
|
||||
val: Some(value),
|
||||
variant: Some(Value::String(variant)),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn visit_newtype_struct<V>(&mut self,
|
||||
_name: &'static str,
|
||||
mut visitor: V) -> Result<V::Value, Self::Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_newtype_struct(self)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn format() -> &'static str {
|
||||
"json"
|
||||
}
|
||||
}
|
||||
|
||||
struct VariantDeserializer<'a> {
|
||||
de: &'a mut Deserializer,
|
||||
val: Option<Value>,
|
||||
variant: Option<Value>,
|
||||
}
|
||||
|
||||
impl<'a> de::VariantVisitor for VariantDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_variant<V>(&mut self) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
de::Deserialize::deserialize(&mut Deserializer::new(self.variant.take().unwrap()))
|
||||
}
|
||||
|
||||
fn visit_unit(&mut self) -> Result<(), Error> {
|
||||
de::Deserialize::deserialize(&mut Deserializer::new(self.val.take().unwrap()))
|
||||
}
|
||||
|
||||
fn visit_newtype<T>(&mut self) -> Result<T, Error>
|
||||
where T: de::Deserialize,
|
||||
{
|
||||
de::Deserialize::deserialize(&mut Deserializer::new(self.val.take().unwrap()))
|
||||
}
|
||||
|
||||
fn visit_tuple<V>(&mut self,
|
||||
_len: usize,
|
||||
visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
if let Value::Array(fields) = self.val.take().unwrap() {
|
||||
de::Deserializer::visit(
|
||||
&mut SeqDeserializer {
|
||||
de: self.de,
|
||||
len: fields.len(),
|
||||
iter: fields.into_iter(),
|
||||
},
|
||||
visitor,
|
||||
)
|
||||
} else {
|
||||
Err(de::Error::syntax("expected a tuple"))
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_struct<V>(&mut self,
|
||||
_fields: &'static[&'static str],
|
||||
visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
if let Value::Object(fields) = self.val.take().unwrap() {
|
||||
de::Deserializer::visit(
|
||||
&mut MapDeserializer {
|
||||
de: self.de,
|
||||
len: fields.len(),
|
||||
iter: fields.into_iter(),
|
||||
value: None,
|
||||
},
|
||||
visitor,
|
||||
)
|
||||
} else {
|
||||
Err(de::Error::syntax("expected a struct"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct SeqDeserializer<'a> {
|
||||
de: &'a mut Deserializer,
|
||||
iter: vec::IntoIter<Value>,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<'a> de::Deserializer for SeqDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
if self.len == 0 {
|
||||
visitor.visit_unit()
|
||||
} else {
|
||||
visitor.visit_seq(self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> de::SeqVisitor for SeqDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<T>(&mut self) -> Result<Option<T>, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some(value) => {
|
||||
self.len -= 1;
|
||||
self.de.value = Some(value);
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
if self.len == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(de::Error::length_mismatch(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.len, Some(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
struct MapDeserializer<'a> {
|
||||
de: &'a mut Deserializer,
|
||||
iter: btree_map::IntoIter<String, Value>,
|
||||
value: Option<Value>,
|
||||
len: usize,
|
||||
}
|
||||
|
||||
impl<'a> de::MapVisitor for MapDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
fn visit_key<T>(&mut self) -> Result<Option<T>, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
match self.iter.next() {
|
||||
Some((key, value)) => {
|
||||
self.len -= 1;
|
||||
self.value = Some(value);
|
||||
self.de.value = Some(Value::String(key));
|
||||
Ok(Some(try!(de::Deserialize::deserialize(self.de))))
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_value<T>(&mut self) -> Result<T, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
let value = self.value.take().unwrap();
|
||||
self.de.value = Some(value);
|
||||
Ok(try!(de::Deserialize::deserialize(self.de)))
|
||||
}
|
||||
|
||||
fn end(&mut self) -> Result<(), Error> {
|
||||
if self.len == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(de::Error::length_mismatch(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_field<V>(&mut self, _field: &'static str) -> Result<V, Error>
|
||||
where V: de::Deserialize,
|
||||
{
|
||||
// See if the type can deserialize from a unit.
|
||||
struct UnitDeserializer;
|
||||
|
||||
impl de::Deserializer for UnitDeserializer {
|
||||
type Error = Error;
|
||||
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_unit()
|
||||
}
|
||||
|
||||
fn visit_option<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_none()
|
||||
}
|
||||
}
|
||||
|
||||
Ok(try!(de::Deserialize::deserialize(&mut UnitDeserializer)))
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
(self.len, Some(self.len))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> de::Deserializer for MapDeserializer<'a> {
|
||||
type Error = Error;
|
||||
|
||||
#[inline]
|
||||
fn visit<V>(&mut self, mut visitor: V) -> Result<V::Value, Error>
|
||||
where V: de::Visitor,
|
||||
{
|
||||
visitor.visit_map(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Shortcut function to encode a `T` into a JSON `Value`
|
||||
pub fn to_value<T>(value: &T) -> Value
|
||||
where T: ser::Serialize
|
||||
{
|
||||
let mut ser = Serializer::new();
|
||||
value.serialize(&mut ser).ok().unwrap();
|
||||
ser.unwrap()
|
||||
}
|
||||
|
||||
/// Shortcut function to decode a JSON `Value` into a `T`
|
||||
pub fn from_value<T>(value: Value) -> Result<T, Error>
|
||||
where T: de::Deserialize
|
||||
{
|
||||
let mut de = Deserializer::new(value);
|
||||
de::Deserialize::deserialize(&mut de)
|
||||
}
|
@ -19,4 +19,3 @@ serde_codegen = { version = "*", path = "../serde_codegen", default-features = f
|
||||
num = "*"
|
||||
rustc-serialize = "*"
|
||||
serde = { version = "*", path = "../serde", features = ["nightly"] }
|
||||
serde_json = { version = "*", path = "../serde_json" }
|
||||
|
@ -4,7 +4,6 @@
|
||||
extern crate num;
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate test;
|
||||
|
||||
include!("../../serde_tests/benches/bench.rs.in");
|
||||
|
@ -1,66 +0,0 @@
|
||||
#![feature(custom_derive, plugin)]
|
||||
#![plugin(serde_macros)]
|
||||
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
// Creating serializable types with serde is quite simple with `serde_macros`. It implements a
|
||||
// syntax extension that automatically generates the necessary serde trait implementations.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct Point {
|
||||
x: i32,
|
||||
y: i32,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let point = Point { x: 5, y: 6 };
|
||||
|
||||
// Serializing to JSON is pretty simple by using the `to_string` method:
|
||||
let serialized_point = serde_json::to_string(&point).unwrap();
|
||||
|
||||
println!("{}", serialized_point);
|
||||
// prints:
|
||||
//
|
||||
// {"x":5,"y":6}
|
||||
|
||||
// There is also support for pretty printing using `to_string_pretty`:
|
||||
let serialized_point = serde_json::to_string_pretty(&point).unwrap();
|
||||
|
||||
println!("{}", serialized_point);
|
||||
// prints:
|
||||
//
|
||||
// {
|
||||
// "x":5,
|
||||
// "y":6
|
||||
// }
|
||||
|
||||
// Values can also be deserialized with the same style using `from_str`:
|
||||
let deserialized_point: Point = serde_json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_point);
|
||||
// prints:
|
||||
//
|
||||
// Point { x: 5, y: 6 }
|
||||
|
||||
// `Point`s aren't the only type that can be serialized to. Because `Point` members have the
|
||||
// same type, they can be also serialized into a map. Also,
|
||||
let deserialized_map: BTreeMap<String, i64> =
|
||||
serde_json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_map);
|
||||
// prints:
|
||||
//
|
||||
// {"x": 5, "y": 6}
|
||||
|
||||
// If you need to accept arbitrary data, you can also deserialize into `serde_json::Value`,
|
||||
// which can represent all JSON values.
|
||||
let deserialized_value: serde_json::Value =
|
||||
serde_json::from_str(&serialized_point).unwrap();
|
||||
|
||||
println!("{:?}", deserialized_value);
|
||||
// prints:
|
||||
//
|
||||
// {"x":5,"y":6}
|
||||
}
|
@ -3,7 +3,6 @@
|
||||
extern crate num;
|
||||
extern crate rustc_serialize;
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
extern crate test;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/bench.rs"));
|
||||
|
@ -1,5 +1,4 @@
|
||||
mod bench_enum;
|
||||
mod bench_log;
|
||||
mod bench_map;
|
||||
mod bench_struct;
|
||||
mod bench_vec;
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -417,14 +417,14 @@ fn bench_decoder_100(b: &mut Bencher) {
|
||||
})
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
D: Deserializer<Error=E>,
|
||||
E: Debug,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserialize::deserialize(&mut d).unwrap();
|
||||
fn run_deserializer<D, T>(mut d: D, value: T)
|
||||
where D: Deserializer,
|
||||
D::Error: Debug + PartialEq,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
{
|
||||
let v = T::deserialize(&mut d);
|
||||
|
||||
assert_eq!(value, v);
|
||||
assert_eq!(Ok(value), v);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
@ -504,14 +504,14 @@ fn run_decoder<
|
||||
assert_eq!(Ok(value), v);
|
||||
}
|
||||
|
||||
fn run_deserializer<
|
||||
D: Deserializer<Error=E>,
|
||||
E: Debug,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
>(mut d: D, value: T) {
|
||||
let v: T = Deserialize::deserialize(&mut d).unwrap();
|
||||
fn run_deserializer<D, T>(mut d: D, value: T)
|
||||
where D: Deserializer,
|
||||
D::Error: Debug + PartialEq,
|
||||
T: Clone + PartialEq + Debug + Deserialize
|
||||
{
|
||||
let v = T::deserialize(&mut d);
|
||||
|
||||
assert_eq!(value, v);
|
||||
assert_eq!(Ok(value), v);
|
||||
}
|
||||
|
||||
#[bench]
|
||||
|
@ -1,4 +1,3 @@
|
||||
extern crate serde;
|
||||
extern crate serde_json;
|
||||
|
||||
include!(concat!(env!("OUT_DIR"), "/test.rs"));
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,50 +0,0 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use serde_json::value::Value;
|
||||
use serde_json::builder::{ArrayBuilder, ObjectBuilder};
|
||||
|
||||
#[test]
|
||||
fn test_array_builder() {
|
||||
let value = ArrayBuilder::new().unwrap();
|
||||
assert_eq!(value, Value::Array(Vec::new()));
|
||||
|
||||
let value = ArrayBuilder::new()
|
||||
.push(1)
|
||||
.push(2)
|
||||
.push(3)
|
||||
.unwrap();
|
||||
assert_eq!(value, Value::Array(vec!(Value::U64(1), Value::U64(2), Value::U64(3))));
|
||||
|
||||
let value = ArrayBuilder::new()
|
||||
.push_array(|bld| bld.push(1).push(2).push(3))
|
||||
.unwrap();
|
||||
assert_eq!(value, Value::Array(vec!(Value::Array(vec!(Value::U64(1), Value::U64(2), Value::U64(3))))));
|
||||
|
||||
let value = ArrayBuilder::new()
|
||||
.push_object(|bld|
|
||||
bld
|
||||
.insert("a".to_string(), 1)
|
||||
.insert("b".to_string(), 2))
|
||||
.unwrap();
|
||||
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert("a".to_string(), Value::U64(1));
|
||||
map.insert("b".to_string(), Value::U64(2));
|
||||
assert_eq!(value, Value::Array(vec!(Value::Object(map))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_object_builder() {
|
||||
let value = ObjectBuilder::new().unwrap();
|
||||
assert_eq!(value, Value::Object(BTreeMap::new()));
|
||||
|
||||
let value = ObjectBuilder::new()
|
||||
.insert("a".to_string(), 1)
|
||||
.insert("b".to_string(), 2)
|
||||
.unwrap();
|
||||
|
||||
let mut map = BTreeMap::new();
|
||||
map.insert("a".to_string(), Value::U64(1));
|
||||
map.insert("b".to_string(), Value::U64(2));
|
||||
assert_eq!(value, Value::Object(map));
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user