*/
-pub use self::JsonEvent::*;
-pub use self::StackElement::*;
-pub use self::Json::*;
-pub use self::ErrorCode::*;
-pub use self::ParserError::*;
-pub use self::DecoderError::*;
+use self::JsonEvent::*;
+use self::StackElement::*;
+use self::ErrorCode::*;
+use self::ParserError::*;
+use self::DecoderError::*;
use self::ParserState::*;
use self::InternalStackElement::*;
F64(f64),
String(string::String),
Boolean(bool),
- Array(JsonArray),
- Object(JsonObject),
+ Array(self::Array),
+ Object(self::Object),
Null,
}
-pub type JsonArray = Vec<Json>;
-pub type JsonObject = TreeMap<string::String, Json>;
+pub type Array = Vec<Json>;
+pub type Object = TreeMap<string::String, Json>;
/// The errors that can arise while parsing a JSON stream.
#[deriving(Clone, PartialEq)]
/// Returns a readable error string for a given error code.
pub fn error_str(error: ErrorCode) -> &'static str {
- return match error {
+ match error {
InvalidSyntax => "invalid syntax",
InvalidNumber => "invalid number",
EOFWhileParsingObject => "EOF While parsing object",
impl<E: ::Encoder<S>, S> Encodable<E, S> for Json {
fn encode(&self, e: &mut E) -> Result<(), S> {
match *self {
- I64(v) => v.encode(e),
- U64(v) => v.encode(e),
- F64(v) => v.encode(e),
- String(ref v) => v.encode(e),
- Boolean(v) => v.encode(e),
- Array(ref v) => v.encode(e),
- Object(ref v) => v.encode(e),
- Null => e.emit_nil(),
+ Json::I64(v) => v.encode(e),
+ Json::U64(v) => v.encode(e),
+ Json::F64(v) => v.encode(e),
+ Json::String(ref v) => v.encode(e),
+ Json::Boolean(v) => v.encode(e),
+ Json::Array(ref v) => v.encode(e),
+ Json::Object(ref v) => v.encode(e),
+ Json::Null => e.emit_nil(),
}
}
}
/// Otherwise, returns None.
pub fn find<'a>(&'a self, key: &str) -> Option<&'a Json>{
match self {
- &Object(ref map) => map.get(key),
+ &Json::Object(ref map) => map.get(key),
_ => None
}
}
/// or the Json value is not an Object, returns None.
pub fn search<'a>(&'a self, key: &str) -> Option<&'a Json> {
match self {
- &Object(ref map) => {
+ &Json::Object(ref map) => {
match map.get(key) {
Some(json_value) => Some(json_value),
None => {
/// If the Json value is an Object, returns the associated TreeMap.
/// Returns None otherwise.
- pub fn as_object<'a>(&'a self) -> Option<&'a JsonObject> {
+ pub fn as_object<'a>(&'a self) -> Option<&'a Object> {
match self {
- &Object(ref map) => Some(map),
+ &Json::Object(ref map) => Some(map),
_ => None
}
}
/// If the Json value is an Array, returns the associated vector.
/// Returns None otherwise.
- pub fn as_array<'a>(&'a self) -> Option<&'a JsonArray> {
+ pub fn as_array<'a>(&'a self) -> Option<&'a Array> {
match self {
- &Array(ref array) => Some(&*array),
+ &Json::Array(ref array) => Some(&*array),
_ => None
}
}
/// Returns None otherwise.
pub fn as_string<'a>(&'a self) -> Option<&'a str> {
match *self {
- String(ref s) => Some(s.as_slice()),
+ Json::String(ref s) => Some(s.as_slice()),
_ => None
}
}
/// Returns true if the Json value is a Number. Returns false otherwise.
pub fn is_number(&self) -> bool {
match *self {
- I64(_) | U64(_) | F64(_) => true,
+ Json::I64(_) | Json::U64(_) | Json::F64(_) => true,
_ => false,
}
}
/// Returns true if the Json value is a i64. Returns false otherwise.
pub fn is_i64(&self) -> bool {
match *self {
- I64(_) => true,
+ Json::I64(_) => true,
_ => false,
}
}
/// Returns true if the Json value is a u64. Returns false otherwise.
pub fn is_u64(&self) -> bool {
match *self {
- U64(_) => true,
+ Json::U64(_) => true,
_ => false,
}
}
/// Returns true if the Json value is a f64. Returns false otherwise.
pub fn is_f64(&self) -> bool {
match *self {
- F64(_) => true,
+ Json::F64(_) => true,
_ => false,
}
}
/// Returns None otherwise.
pub fn as_i64(&self) -> Option<i64> {
match *self {
- I64(n) => Some(n),
- U64(n) => num::cast(n),
+ Json::I64(n) => Some(n),
+ Json::U64(n) => num::cast(n),
_ => None
}
}
/// Returns None otherwise.
pub fn as_u64(&self) -> Option<u64> {
match *self {
- I64(n) => num::cast(n),
- U64(n) => Some(n),
+ Json::I64(n) => num::cast(n),
+ Json::U64(n) => Some(n),
_ => None
}
}
/// Returns None otherwise.
pub fn as_f64(&self) -> Option<f64> {
match *self {
- I64(n) => num::cast(n),
- U64(n) => num::cast(n),
- F64(n) => Some(n),
+ Json::I64(n) => num::cast(n),
+ Json::U64(n) => num::cast(n),
+ Json::F64(n) => Some(n),
_ => None
}
}
/// Returns None otherwise.
pub fn as_boolean(&self) -> Option<bool> {
match self {
- &Boolean(b) => Some(b),
+ &Json::Boolean(b) => Some(b),
_ => None
}
}
/// Returns None otherwise.
pub fn as_null(&self) -> Option<()> {
match self {
- &Null => Some(()),
+ &Json::Null => Some(()),
_ => None
}
}
impl ops::Index<uint, Json> for Json {
fn index<'a>(&'a self, idx: &uint) -> &'a Json {
match self {
- &Array(ref v) => v.index(idx),
+ &Json::Array(ref v) => v.index(idx),
_ => panic!("can only index Json with uint if it is an array")
}
}
}
fn build_value(&mut self) -> Result<Json, BuilderError> {
- match self.token {
- Some(NullValue) => Ok(Null),
- Some(I64Value(n)) => Ok(I64(n)),
- Some(U64Value(n)) => Ok(U64(n)),
- Some(F64Value(n)) => Ok(F64(n)),
- Some(BooleanValue(b)) => Ok(Boolean(b)),
+ return match self.token {
+ Some(NullValue) => Ok(Json::Null),
+ Some(I64Value(n)) => Ok(Json::I64(n)),
+ Some(U64Value(n)) => Ok(Json::U64(n)),
+ Some(F64Value(n)) => Ok(Json::F64(n)),
+ Some(BooleanValue(b)) => Ok(Json::Boolean(b)),
Some(StringValue(ref mut s)) => {
let mut temp = string::String::new();
swap(s, &mut temp);
- Ok(String(temp))
+ Ok(Json::String(temp))
}
Some(Error(e)) => Err(e),
Some(ArrayStart) => self.build_array(),
loop {
if self.token == Some(ArrayEnd) {
- return Ok(Array(values.into_iter().collect()));
+ return Ok(Json::Array(values.into_iter().collect()));
}
match self.build_value() {
Ok(v) => values.push(v),
loop {
match self.token {
- Some(ObjectEnd) => { return Ok(Object(values)); }
+ Some(ObjectEnd) => { return Ok(Json::Object(values)); }
Some(Error(e)) => { return Err(e); }
None => { break; }
_ => {}
macro_rules! expect(
($e:expr, Null) => ({
match $e {
- Null => Ok(()),
+ Json::Null => Ok(()),
other => Err(ExpectedError("Null".to_string(),
format!("{}", other)))
}
});
($e:expr, $t:ident) => ({
match $e {
- $t(v) => Ok(v),
+ Json::$t(v) => Ok(v),
other => {
Err(ExpectedError(stringify!($t).to_string(),
format!("{}", other)))
($name:ident, $ty:ty) => {
fn $name(&mut self) -> DecodeResult<$ty> {
match self.pop() {
- I64(f) => {
+ Json::I64(f) => {
match num::cast(f) {
Some(f) => Ok(f),
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
}
}
- U64(f) => {
+ Json::U64(f) => {
match num::cast(f) {
Some(f) => Ok(f),
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
}
}
- F64(f) => {
+ Json::F64(f) => {
match num::cast(f) {
Some(f) => Ok(f),
None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
}
}
- String(s) => {
+ Json::String(s) => {
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
// is going to have a string here, as per JSON spec.
match std::str::from_str(s.as_slice()) {
fn read_f64(&mut self) -> DecodeResult<f64> {
debug!("read_f64");
match self.pop() {
- I64(f) => Ok(f as f64),
- U64(f) => Ok(f as f64),
- F64(f) => Ok(f),
- String(s) => {
+ Json::I64(f) => Ok(f as f64),
+ Json::U64(f) => Ok(f as f64),
+ Json::F64(f) => Ok(f),
+ Json::String(s) => {
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
// is going to have a string here, as per JSON spec.
match std::str::from_str(s.as_slice()) {
None => Err(ExpectedError("Number".to_string(), s)),
}
},
- Null => Ok(f64::NAN),
+ Json::Null => Ok(f64::NAN),
value => Err(ExpectedError("Number".to_string(), format!("{}", value)))
}
}
-> DecodeResult<T> {
debug!("read_enum_variant(names={})", names);
let name = match self.pop() {
- String(s) => s,
- Object(mut o) => {
+ Json::String(s) => s,
+ Json::Object(mut o) => {
let n = match o.remove(&"variant".to_string()) {
- Some(String(s)) => s,
+ Some(Json::String(s)) => s,
Some(val) => {
return Err(ExpectedError("String".to_string(), format!("{}", val)))
}
}
};
match o.remove(&"fields".to_string()) {
- Some(Array(l)) => {
+ Some(Json::Array(l)) => {
for field in l.into_iter().rev() {
self.stack.push(field);
}
None => {
// Add a Null and try to parse it as an Option<_>
// to get None as a default value.
- self.stack.push(Null);
+ self.stack.push(Json::Null);
match f(self) {
Ok(x) => x,
Err(_) => return Err(MissingFieldError(name.to_string())),
try!(f(self))
}
};
- self.stack.push(Object(obj));
+ self.stack.push(Json::Object(obj));
Ok(value)
}
fn read_option<T>(&mut self, f: |&mut Decoder, bool| -> DecodeResult<T>) -> DecodeResult<T> {
debug!("read_option()");
match self.pop() {
- Null => f(self, false),
+ Json::Null => f(self, false),
value => { self.stack.push(value); f(self, true) }
}
}
let len = obj.len();
for (key, value) in obj.into_iter() {
self.stack.push(value);
- self.stack.push(String(key));
+ self.stack.push(Json::String(key));
}
f(self, len)
}
macro_rules! to_json_impl_i64(
($($t:ty), +) => (
$(impl ToJson for $t {
- fn to_json(&self) -> Json { I64(*self as i64) }
+ fn to_json(&self) -> Json { Json::I64(*self as i64) }
})+
)
)
macro_rules! to_json_impl_u64(
($($t:ty), +) => (
$(impl ToJson for $t {
- fn to_json(&self) -> Json { U64(*self as u64) }
+ fn to_json(&self) -> Json { Json::U64(*self as u64) }
})+
)
)
impl ToJson for f64 {
fn to_json(&self) -> Json {
match self.classify() {
- FPNaN | FPInfinite => Null,
- _ => F64(*self)
+ FPNaN | FPInfinite => Json::Null,
+ _ => Json::F64(*self)
}
}
}
impl ToJson for () {
- fn to_json(&self) -> Json { Null }
+ fn to_json(&self) -> Json { Json::Null }
}
impl ToJson for bool {
- fn to_json(&self) -> Json { Boolean(*self) }
+ fn to_json(&self) -> Json { Json::Boolean(*self) }
}
impl ToJson for str {
- fn to_json(&self) -> Json { String(self.into_string()) }
+ fn to_json(&self) -> Json { Json::String(self.into_string()) }
}
impl ToJson for string::String {
- fn to_json(&self) -> Json { String((*self).clone()) }
+ fn to_json(&self) -> Json { Json::String((*self).clone()) }
}
macro_rules! tuple_impl {
#[allow(non_snake_case)]
fn to_json(&self) -> Json {
match *self {
- ($(ref $tyvar),*,) => Array(vec![$($tyvar.to_json()),*])
+ ($(ref $tyvar),*,) => Json::Array(vec![$($tyvar.to_json()),*])
}
}
}
tuple_impl!{A, B, C, D, E, F, G, H, I, J, K, L}
impl<A: ToJson> ToJson for [A] {
- fn to_json(&self) -> Json { Array(self.iter().map(|elt| elt.to_json()).collect()) }
+ fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
}
impl<A: ToJson> ToJson for Vec<A> {
- fn to_json(&self) -> Json { Array(self.iter().map(|elt| elt.to_json()).collect()) }
+ fn to_json(&self) -> Json { Json::Array(self.iter().map(|elt| elt.to_json()).collect()) }
}
impl<A: ToJson> ToJson for TreeMap<string::String, A> {
for (key, value) in self.iter() {
d.insert((*key).clone(), value.to_json());
}
- Object(d)
+ Json::Object(d)
}
}
for (key, value) in self.iter() {
d.insert((*key).clone(), value.to_json());
}
- Object(d)
+ Json::Object(d)
}
}
impl<A:ToJson> ToJson for Option<A> {
fn to_json(&self) -> Json {
match *self {
- None => Null,
+ None => Json::Null,
Some(ref value) => value.to_json()
}
}
use self::DecodeEnum::*;
use self::test::Bencher;
use {Encodable, Decodable};
- use super::{Array, Encoder, Decoder, Error, Boolean, I64, U64, F64, String, Null,
- PrettyEncoder, Object, Json, from_str, ParseError, ExpectedError,
- MissingFieldError, UnknownVariantError, DecodeResult, DecoderError,
- JsonEvent, Parser, StackElement,
- ObjectStart, ObjectEnd, ArrayStart, ArrayEnd, BooleanValue, U64Value,
- F64Value, StringValue, NullValue, SyntaxError, Key, Index, Stack,
- InvalidSyntax, InvalidNumber, EOFWhileParsingObject, EOFWhileParsingArray,
- EOFWhileParsingValue, EOFWhileParsingString, KeyMustBeAString, ExpectedColon,
- TrailingCharacters, TrailingComma};
+ use super::Json::*;
+ use super::ErrorCode::*;
+ use super::ParserError::*;
+ use super::DecoderError::*;
+ use super::JsonEvent::*;
+ use super::ParserState::*;
+ use super::StackElement::*;
+ use super::InternalStackElement::*;
+ use super::{PrettyEncoder, Json, from_str, DecodeResult, DecoderError, JsonEvent, Parser,
+ StackElement, Stack, Encoder, Decoder};
use std::{i64, u64, f32, f64, io};
use std::collections::TreeMap;
use std::num::Float;