Skip to content

Commit 57c3c84

Browse files
authored
Merge pull request #30 from maciejhirsz/dev
0.7.3 Closes #28
2 parents 5151e6b + 9f4ef4e commit 57c3c84

File tree

6 files changed

+214
-71
lines changed

6 files changed

+214
-71
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "json"
3-
version = "0.7.2"
3+
version = "0.7.3"
44
authors = ["Maciej Hirsz <maciej.hirsz@gmail.com>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"

src/codegen.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use JsonValue;
22

33
pub trait Generator {
4+
fn current_index(&self) -> usize;
5+
46
fn new_line(&mut self) {}
57

68
fn write(&mut self, slice: &[u8]);
@@ -34,31 +36,35 @@ pub trait Generator {
3436
self.write_char(b'"');
3537
}
3638

37-
fn write_digits_from_u64(&mut self, mut num: u64, length: &mut u8) {
39+
fn write_digits_from_u64(&mut self, mut num: u64) {
3840
let digit = (num % 10) as u8;
3941
if num > 9 {
4042
num /= 10;
41-
self.write_digits_from_u64(num, length);
43+
self.write_digits_from_u64(num);
4244
}
43-
*length += 1;
4445
self.write_char(digit + b'0');
4546
}
4647

4748
fn write_number(&mut self, mut num: f64) {
48-
let mut length = 0;
4949
if num < 0.0 {
5050
num = -num;
5151
self.write_char(b'-');
5252
}
5353

54-
self.write_digits_from_u64(num as u64, &mut length);
54+
let start = self.current_index();
55+
56+
self.write_digits_from_u64(num as u64);
5557

5658
let mut fract = num.fract();
59+
5760
if fract < 1e-16 {
5861
return;
5962
}
6063

64+
let mut length = self.current_index() - start;
65+
6166
fract *= 10.0;
67+
6268
self.write_char(b'.');
6369
self.write_char((fract as u8) + b'0');
6470
fract = fract.fract();
@@ -76,7 +82,8 @@ pub trait Generator {
7682
match *json {
7783
JsonValue::String(ref string) => self.write_string(string),
7884
JsonValue::Number(ref number) => self.write_number(*number),
79-
JsonValue::Boolean(ref value) => self.write(if *value { b"true" } else { b"false" }),
85+
JsonValue::Boolean(true) => self.write(b"true"),
86+
JsonValue::Boolean(false) => self.write(b"false"),
8087
JsonValue::Null => self.write(b"null"),
8188
JsonValue::Array(ref array) => {
8289
self.write_char(b'[');
@@ -135,6 +142,10 @@ impl DumpGenerator {
135142
}
136143

137144
impl Generator for DumpGenerator {
145+
fn current_index(&self) -> usize {
146+
self.code.len()
147+
}
148+
138149
fn write(&mut self, slice: &[u8]) {
139150
self.code.extend_from_slice(slice);
140151
}
@@ -169,6 +180,10 @@ impl PrettyGenerator {
169180
}
170181

171182
impl Generator for PrettyGenerator {
183+
fn current_index(&self) -> usize {
184+
self.code.len()
185+
}
186+
172187
fn new_line(&mut self) {
173188
self.code.push(b'\n');
174189
for _ in 0..(self.dent * self.spaces_per_indent) {

src/lib.rs

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
//! some keys under some conditions. Mapping that to idiomatic Rust structs
1616
//! introduces friction.
1717
//!
18-
//! This crate intends to avoid that friction by extensively using static dispatch
19-
//! and hiding type information behind enums, while still giving you all the
20-
//! guarantees of safe Rust code.
18+
//! This crate intends to avoid that friction.
2119
//!
22-
//! ```
23-
//! let data = json::parse(r#"
20+
//! ```rust
21+
//! # #[macro_use] extern crate json;
22+
//! # fn main() {
23+
//! let parsed = json::parse(r#"
2424
//!
2525
//! {
2626
//! "code": 200,
@@ -36,51 +36,64 @@
3636
//!
3737
//! "#).unwrap();
3838
//!
39-
//! assert!(data["code"] == 200);
40-
//! assert!(data["success"] == true);
41-
//! assert!(data["payload"]["features"].is_array());
42-
//! assert!(data["payload"]["features"][0] == "awesome");
43-
//! assert!(data["payload"]["features"].contains("easyAPI"));
39+
//! let instantiated = object!{
40+
//! "code" => 200,
41+
//! "success" => true,
42+
//! "payload" => object!{
43+
//! "features" => array![
44+
//! "awesome",
45+
//! "easyAPI",
46+
//! "lowLearningCurve"
47+
//! ]
48+
//! }
49+
//! };
4450
//!
45-
//! // Error resilient: non-existent values default to null
46-
//! assert!(data["this"]["does"]["not"]["exist"].is_null());
51+
//! assert_eq!(parsed, instantiated);
52+
//! # }
4753
//! ```
4854
//!
49-
//! ## Create JSON data without defining structs
55+
//! ## First class citizen
5056
//!
51-
//! ```
52-
//! #[macro_use]
53-
//! extern crate json;
57+
//! Using macros and easy indexing, it's easy to work with the data.
5458
//!
55-
//! fn main() {
56-
//! let data = object!{
57-
//! "a" => "bar",
58-
//! "b" => array![1, false, "foo"]
59-
//! };
59+
//! ```rust
60+
//! # #[macro_use] extern crate json;
61+
//! # fn main() {
62+
//! let mut data = object!{
63+
//! "foo" => false,
64+
//! "bar" => json::Null,
65+
//! "answer" => 42,
66+
//! "list" => array![json::Null, "world", true]
67+
//! };
6068
//!
61-
//! assert_eq!(data.dump(), r#"{"a":"bar","b":[1,false,"foo"]}"#);
62-
//! }
63-
//! ```
69+
//! // Partial equality is implemented for most raw types:
70+
//! assert!(data["foo"] == false);
6471
//!
65-
//! ## Mutate simply by assigning new values
72+
//! // And it's type aware, `null` and `false` are different values:
73+
//! assert!(data["bar"] != false);
6674
//!
67-
//! ```
68-
//! let mut data = json::parse(r#"
75+
//! // But you can use any Rust number types:
76+
//! assert!(data["answer"] == 42);
77+
//! assert!(data["answer"] == 42.0);
78+
//! assert!(data["answer"] == 42isize);
6979
//!
70-
//! {
71-
//! "name": "Bob",
72-
//! "isAwesome": false
73-
//! }
80+
//! // Access nested structures, arrays and objects:
81+
//! assert!(data["list"][0].is_null());
82+
//! assert!(data["list"][1] == "world");
83+
//! assert!(data["list"][2] == true);
7484
//!
75-
//! "#).unwrap();
85+
//! // Error resilient - accessing properties that don't exist yield null:
86+
//! assert!(data["this"]["does"]["not"]["exist"].is_null());
7687
//!
77-
//! data["isAwesome"] = true.into();
78-
//! data["likes"] = "Rust".into();
88+
//! // Mutate by assigning:
89+
//! data["list"][0] = "Hello".into();
7990
//!
80-
//! assert_eq!(data.dump(), r#"{"isAwesome":true,"likes":"Rust","name":"Bob"}"#);
91+
//! // Use the `dump` method to serialize the data:
92+
//! assert_eq!(data.dump(), r#"{"answer":42,"bar":null,"foo":false,"list":["Hello","world",true]}"#);
8193
//!
82-
//! // Pretty print the output
94+
//! // Or pretty print it out:
8395
//! println!("{:#}", data);
96+
//! # }
8497
//! ```
8598
//!
8699
//! ## Serialize with `json::stringify(value)`

0 commit comments

Comments
 (0)