Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.12.0 #164

Merged
merged 1 commit into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[package]
name = "json"
version = "0.11.15"
version = "0.12.0"
authors = ["Maciej Hirsz <hello@maciej.codes>"]
description = "JSON implementation in Rust"
repository = "https://github.com/maciejhirsz/json-rust"
documentation = "https://docs.rs/json/"
license = "MIT/Apache-2.0"
edition = "2018"
66 changes: 33 additions & 33 deletions src/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::ptr;
use std::io::Write;
use JsonValue;
use number::Number;
use object::Object;
use std::io;

use util::print_dec;
use crate::JsonValue;
use crate::number::Number;
use crate::object::Object;
use crate::util::print_dec;

const QU: u8 = b'"';
const BS: u8 = b'\\';
Expand Down Expand Up @@ -66,35 +66,35 @@ pub trait Generator {

#[inline(never)]
fn write_string_complex(&mut self, string: &str, mut start: usize) -> io::Result<()> {
try!(self.write(string[ .. start].as_bytes()));
self.write(string[ .. start].as_bytes())?;

for (index, ch) in string.bytes().enumerate().skip(start) {
let escape = ESCAPED[ch as usize];
if escape > 0 {
try!(self.write(string[start .. index].as_bytes()));
try!(self.write(&[b'\\', escape]));
self.write(string[start .. index].as_bytes())?;
self.write(&[b'\\', escape])?;
start = index + 1;
}
if escape == b'u' {
try!(write!(self.get_writer(), "{:04x}", ch));
write!(self.get_writer(), "{:04x}", ch)?;
}
}
try!(self.write(string[start ..].as_bytes()));
self.write(string[start ..].as_bytes())?;

self.write_char(b'"')
}

#[inline(always)]
fn write_string(&mut self, string: &str) -> io::Result<()> {
try!(self.write_char(b'"'));
self.write_char(b'"')?;

for (index, ch) in string.bytes().enumerate() {
if ESCAPED[ch as usize] > 0 {
return self.write_string_complex(string, index)
}
}

try!(self.write(string.as_bytes()));
self.write(string.as_bytes())?;
self.write_char(b'"')
}

Expand All @@ -116,30 +116,30 @@ pub trait Generator {

#[inline(always)]
fn write_object(&mut self, object: &Object) -> io::Result<()> {
try!(self.write_char(b'{'));
self.write_char(b'{')?;
let mut iter = object.iter();

if let Some((key, value)) = iter.next() {
self.indent();
try!(self.new_line());
try!(self.write_string(key));
try!(self.write_min(b": ", b':'));
try!(self.write_json(value));
self.new_line()?;
self.write_string(key)?;
self.write_min(b": ", b':')?;
self.write_json(value)?;
} else {
try!(self.write_char(b'}'));
self.write_char(b'}')?;
return Ok(());
}

for (key, value) in iter {
try!(self.write_char(b','));
try!(self.new_line());
try!(self.write_string(key));
try!(self.write_min(b": ", b':'));
try!(self.write_json(value));
self.write_char(b',')?;
self.new_line()?;
self.write_string(key)?;
self.write_min(b": ", b':')?;
self.write_json(value)?;
}

self.dedent();
try!(self.new_line());
self.new_line()?;
self.write_char(b'}')
}

Expand All @@ -152,26 +152,26 @@ pub trait Generator {
JsonValue::Boolean(true) => self.write(b"true"),
JsonValue::Boolean(false) => self.write(b"false"),
JsonValue::Array(ref array) => {
try!(self.write_char(b'['));
self.write_char(b'[')?;
let mut iter = array.iter();

if let Some(item) = iter.next() {
self.indent();
try!(self.new_line());
try!(self.write_json(item));
self.new_line()?;
self.write_json(item)?;
} else {
try!(self.write_char(b']'));
self.write_char(b']')?;
return Ok(());
}

for item in iter {
try!(self.write_char(b','));
try!(self.new_line());
try!(self.write_json(item));
self.write_char(b',')?;
self.new_line()?;
self.write_json(item)?;
}

self.dedent();
try!(self.new_line());
self.new_line()?;
self.write_char(b']')
},
JsonValue::Object(ref object) => {
Expand Down Expand Up @@ -345,9 +345,9 @@ impl<'a, W> Generator for PrettyWriterGenerator<'a, W> where W: Write {
}

fn new_line(&mut self) -> io::Result<()> {
try!(self.write_char(b'\n'));
self.write_char(b'\n')?;
for _ in 0..(self.dent * self.spaces_per_indent) {
try!(self.write_char(b' '));
self.write_char(b' ')?;
}
Ok(())
}
Expand Down
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub mod iterators {
pub use Error as JsonError;

#[deprecated(since="0.9.0", note="use `json::Result` instead")]
pub use Result as JsonResult;
pub use crate::Result as JsonResult;

pub use parser::parse;

Expand Down Expand Up @@ -280,7 +280,8 @@ macro_rules! array {
[] => ($crate::JsonValue::new_array());

[ $( $item:expr ),* ] => ({
let mut array = Vec::new();
let size = 0 $( + {let _ = $item; 1} )*;
let mut array = Vec::with_capacity(size);

$(
array.push($item.into());
Expand Down Expand Up @@ -326,7 +327,8 @@ macro_rules! object {
{ $( $key:expr => $value:expr, )* } => ({
use $crate::object::Object;

let mut object = Object::new();
let size = 0 $( + {let _ = $key; 1} )*;
let mut object = Object::with_capacity(size);

$(
object.insert($key, $value.into());
Expand Down
4 changes: 2 additions & 2 deletions src/number.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{ ops, fmt, f32, f64 };
use std::num::FpCategory;
use util::grisu2;
use util::print_dec;
use crate::util::grisu2;
use crate::util::print_dec;

/// NaN value represented in `Number` type. NaN is equal to itself.
pub const NAN: Number = Number {
Expand Down
18 changes: 16 additions & 2 deletions src/object.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{ ptr, mem, str, slice, fmt };
use std::ops::{ Index, IndexMut, Deref };
use std::iter::FromIterator;

use codegen::{ DumpGenerator, Generator, PrettyGenerator };
use value::JsonValue;
use crate::codegen::{ DumpGenerator, Generator, PrettyGenerator };
use crate::value::JsonValue;

const KEY_BUF_LEN: usize = 32;
static NULL: JsonValue = JsonValue::Null;
Expand Down Expand Up @@ -539,6 +540,19 @@ impl Clone for Object {
}
}

impl<K: AsRef<str>, V: Into<JsonValue>> FromIterator<(K, V)> for Object {
fn from_iter<I: IntoIterator<Item=(K, V)>>(iter: I) -> Self {
let iter = iter.into_iter();
let mut object = Object::with_capacity(iter.size_hint().0);

for (key, value) in iter {
object.insert(key.as_ref(), value.into());
}

object
}
}

// Because keys can inserted in different order, the safe way to
// compare `Object`s is to iterate over one and check if the other
// has all the same keys.
Expand Down
Loading