mod codegen;
mod parser;
mod value;
mod error;
pub mod iterators;
pub use error::JsonError;
pub use value::JsonValue;
pub use value::JsonValue::Null;
pub type JsonResult<T> = Result<T, JsonError>;
pub use parser::parse;
use codegen::Generator;
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::fmt;
pub type Array = Vec<JsonValue>;
pub type Object = BTreeMap<String, JsonValue>;
impl JsonValue {
pub fn dump(&self) -> String {
let mut gen = Generator::new(true, 0);
gen.write_json(self);
gen.consume()
}
pub fn pretty(&self, spaces: u16) -> String {
let mut gen = Generator::new(false, spaces);
gen.write_json(self);
gen.consume()
}
}
impl fmt::Display for JsonValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if f.alternate() {
f.write_str(&self.pretty(4))
} else {
match *self {
JsonValue::String(ref value) => value.fmt(f),
JsonValue::Number(ref value) => value.fmt(f),
JsonValue::Boolean(ref value) => value.fmt(f),
JsonValue::Null => f.write_str("null"),
_ => f.write_str(&self.dump())
}
}
}
}
#[deprecated(since="0.5.0", note="Use `value.dump(0)` instead")]
pub fn stringify_ref(root: &JsonValue) -> String {
root.dump()
}
pub fn stringify<T>(root: T) -> String where T: Into<JsonValue> {
let root: JsonValue = root.into();
root.dump()
}
pub fn stringify_pretty<T>(root: T, spaces: u16) -> String where T: Into<JsonValue> {
let root: JsonValue = root.into();
root.pretty(spaces)
}
#[macro_export]
macro_rules! array {
[] => ($crate::JsonValue::new_array());
[ $( $item:expr ),* ] => ({
let mut array = Vec::new();
$(
array.push($item.into());
)*
$crate::JsonValue::Array(array)
})
}
#[macro_export]
macro_rules! object {
{} => ($crate::JsonValue::new_object());
{ $( $key:expr => $value:expr ),* } => ({
use std::collections::BTreeMap;
let mut object = BTreeMap::new();
$(
object.insert($key.into(), $value.into());
)*
$crate::JsonValue::Object(object)
})
}
macro_rules! implement_extras {
($from:ty) => {
impl Into<JsonValue> for Option<$from> {
fn into(self) -> JsonValue {
match self {
Some(value) => value.into(),
None => Null,
}
}
}
impl Into<JsonValue> for Vec<$from> {
fn into(mut self) -> JsonValue {
JsonValue::Array(self.drain(..)
.map(|value| value.into())
.collect::<Vec<JsonValue>>()
)
}
}
impl Into<JsonValue> for Vec<Option<$from>> {
fn into(mut self) -> JsonValue {
JsonValue::Array(self.drain(..)
.map(|item| item.into())
.collect::<Vec<JsonValue>>()
)
}
}
}
}
macro_rules! implement {
($to:ident, $from:ty as $wanted:ty) => {
impl Into<JsonValue> for $from {
fn into(self) -> JsonValue {
JsonValue::$to(self as $wanted)
}
}
implement_extras!($from);
};
($to:ident, $from:ty) => {
impl Into<JsonValue> for $from {
fn into(self) -> JsonValue {
JsonValue::$to(self)
}
}
implement_extras!($from);
}
}
impl<'a> Into<JsonValue> for &'a str {
fn into(self) -> JsonValue {
JsonValue::String(self.to_string())
}
}
impl<'a> Into<JsonValue> for Option<&'a str> {
fn into(self) -> JsonValue {
match self {
Some(value) => value.into(),
None => Null,
}
}
}
impl Into<JsonValue> for HashMap<String, JsonValue> {
fn into(mut self) -> JsonValue {
let mut object = BTreeMap::new();
for (key, value) in self.drain() {
object.insert(key, value);
}
JsonValue::Object(object)
}
}
impl Into<JsonValue> for Option<HashMap<String, JsonValue>> {
fn into(self) -> JsonValue {
match self {
Some(value) => value.into(),
None => Null,
}
}
}
impl Into<JsonValue> for Option<JsonValue> {
fn into(self) -> JsonValue {
match self {
Some(value) => value,
None => Null,
}
}
}
implement!(String, String);
implement!(Number, isize as f64);
implement!(Number, usize as f64);
implement!(Number, i8 as f64);
implement!(Number, i16 as f64);
implement!(Number, i32 as f64);
implement!(Number, i64 as f64);
implement!(Number, u8 as f64);
implement!(Number, u16 as f64);
implement!(Number, u32 as f64);
implement!(Number, u64 as f64);
implement!(Number, f32 as f64);
implement!(Number, f64);
implement!(Object, Object);
implement!(Array, Array);
implement!(Boolean, bool);