Skip to content

Commit e47d9d1

Browse files
committed
0.7.2 convenience methods (#24)
1 parent 604effa commit e47d9d1

File tree

6 files changed

+142
-74
lines changed

6 files changed

+142
-74
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.1"
3+
version = "0.7.2"
44
authors = ["Maciej Hirsz <maciej.hirsz@gmail.com>"]
55
description = "JSON implementation in Rust"
66
repository = "https://github.com/maciejhirsz/json-rust"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88

99
Parse and serialize [JSON](http://json.org/) with ease.
1010

11-
**[Complete Documentation](http://terhix.com/doc/json/) - [Cargo](https://crates.io/crates/json) - [Repository](https://github.com/maciejhirsz/json-rust)**
11+
**[Complete Documentation](http://terhix.com/doc/json/) -**
12+
**[Cargo](https://crates.io/crates/json) -**
13+
**[Repository](https://github.com/maciejhirsz/json-rust)**
1214

1315
## Why?
1416

src/codegen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub trait Generator {
3636

3737
fn write_digits_from_u64(&mut self, mut num: u64, length: &mut u8) {
3838
let digit = (num % 10) as u8;
39-
num /= 10;
40-
if num > 0 {
39+
if num > 9 {
40+
num /= 10;
4141
self.write_digits_from_u64(num, length);
4242
}
4343
*length += 1;

src/lib.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
//!
55
//! Parse and serialize [JSON](http://json.org/) with ease.
66
//!
7-
//! **[Complete Documentation](http://terhix.com/doc/json/) - [Cargo](https://crates.io/crates/json) - [Repository](https://github.com/maciejhirsz/json-rust)**
7+
//! **[Complete Documentation](http://terhix.com/doc/json/) -**
8+
//! **[Cargo](https://crates.io/crates/json) -**
9+
//! **[Repository](https://github.com/maciejhirsz/json-rust)**
810
//!
911
//! ## Why?
1012
//!
@@ -294,6 +296,28 @@ macro_rules! object {
294296
})
295297
}
296298

299+
macro_rules! partial_eq {
300+
($to:ident, $from:ty, $self_cast:expr => $from_cast:expr) => {
301+
impl PartialEq<$from> for JsonValue {
302+
fn eq(&self, other: &$from) -> bool {
303+
match *self {
304+
JsonValue::$to(ref value) => value == $from_cast,
305+
_ => false
306+
}
307+
}
308+
}
309+
310+
impl<'a> PartialEq<JsonValue> for $from {
311+
fn eq(&self, other: &JsonValue) -> bool {
312+
match *other {
313+
JsonValue::$to(ref value) => value == $self_cast,
314+
_ => false
315+
}
316+
}
317+
}
318+
}
319+
}
320+
297321
macro_rules! implement_extras {
298322
($from:ty) => {
299323
impl From<Option<$from>> for JsonValue {
@@ -307,18 +331,20 @@ macro_rules! implement_extras {
307331

308332
impl From<Vec<$from>> for JsonValue {
309333
fn from(mut val: Vec<$from>) -> JsonValue {
310-
JsonValue::Array(val.drain(..)
311-
.map(|value| value.into())
312-
.collect::<Vec<JsonValue>>()
334+
JsonValue::Array(
335+
val.drain(..)
336+
.map(|value| value.into())
337+
.collect()
313338
)
314339
}
315340
}
316341

317342
impl From<Vec<Option<$from>>> for JsonValue {
318343
fn from(mut val: Vec<Option<$from>>) -> JsonValue {
319-
JsonValue::Array(val.drain(..)
320-
.map(|item| item.into())
321-
.collect::<Vec<JsonValue>>()
344+
JsonValue::Array(
345+
val.drain(..)
346+
.map(|item| item.into())
347+
.collect()
322348
)
323349
}
324350
}

src/parser.rs

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ pub enum Token {
1919
}
2020

2121
macro_rules! expect_char {
22-
($tok:ident, $ch:pat) => {
23-
match $tok.source.next() {
24-
Some($ch) => {},
25-
Some(ch) => return Err(JsonError::unexpected_character(ch)),
26-
None => return Err(JsonError::UnexpectedEndOfJson)
27-
}
22+
($tok:ident, $( $ch:pat ),*) => {
23+
$(
24+
match $tok.source.next() {
25+
Some($ch) => {},
26+
Some(ch) => return Err(JsonError::unexpected_character(ch)),
27+
None => return Err(JsonError::UnexpectedEndOfJson)
28+
}
29+
)*
2830
}
2931
}
3032

@@ -205,22 +207,15 @@ impl<'a> Tokenizer<'a> {
205207
b'"' => Token::String(try!(self.read_string())),
206208
b'0' ... b'9' | b'-' => Token::Number(try!(self.read_number(ch))),
207209
b't' => {
208-
expect_char!(self, b'r');
209-
expect_char!(self, b'u');
210-
expect_char!(self, b'e');
210+
expect_char!(self, b'r', b'u', b'e');
211211
Token::Boolean(true)
212212
},
213213
b'f' => {
214-
expect_char!(self, b'a');
215-
expect_char!(self, b'l');
216-
expect_char!(self, b's');
217-
expect_char!(self, b'e');
214+
expect_char!(self, b'a', b'l', b's', b'e');
218215
Token::Boolean(false)
219216
},
220217
b'n' => {
221-
expect_char!(self, b'u');
222-
expect_char!(self, b'l');
223-
expect_char!(self, b'l');
218+
expect_char!(self, b'u', b'l', b'l');
224219
Token::Null
225220
},
226221
// whitespace
@@ -326,13 +321,15 @@ impl<'a> Parser<'a> {
326321

327322
fn value_from(&mut self, token: Token) -> JsonResult<JsonValue> {
328323
Ok(match token {
329-
Token::String(value) => JsonValue::String(value),
330-
Token::Number(value) => JsonValue::Number(value),
331-
Token::Boolean(value) => JsonValue::Boolean(value),
332-
Token::Null => JsonValue::Null,
333-
Token::BracketOn => return self.array(),
334-
Token::BraceOn => return self.object(),
335-
token => return Err(JsonError::unexpected_token(token))
324+
Token::String(value) => JsonValue::String(value),
325+
Token::Number(value) => JsonValue::Number(value),
326+
Token::Boolean(value) => JsonValue::Boolean(value),
327+
Token::Null => JsonValue::Null,
328+
Token::BracketOn => return self.array(),
329+
Token::BraceOn => return self.object(),
330+
token => {
331+
return Err(JsonError::unexpected_token(token))
332+
}
336333
})
337334
}
338335

src/value.rs

Lines changed: 83 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,52 @@ impl JsonValue {
6262
}
6363
}
6464

65-
/// Deprecated because the return type is planned to change to
66-
/// `Option<String>` eventually down the road.
65+
pub fn is_number(&self) -> bool {
66+
match *self {
67+
JsonValue::Number(_) => true,
68+
_ => false,
69+
}
70+
}
71+
72+
pub fn is_boolean(&self) -> bool {
73+
match *self {
74+
JsonValue::Boolean(_) => true,
75+
_ => false
76+
}
77+
}
78+
79+
pub fn is_null(&self) -> bool {
80+
match *self {
81+
JsonValue::Null => true,
82+
_ => false,
83+
}
84+
}
85+
86+
pub fn is_object(&self) -> bool {
87+
match *self {
88+
JsonValue::Object(_) => true,
89+
_ => false,
90+
}
91+
}
92+
93+
pub fn is_array(&self) -> bool {
94+
match *self {
95+
JsonValue::Array(_) => true,
96+
_ => false,
97+
}
98+
}
99+
100+
pub fn is_empty(&self) -> bool {
101+
match *self {
102+
JsonValue::String(ref string) => string.is_empty(),
103+
JsonValue::Number(ref float) => !float.is_normal(),
104+
JsonValue::Boolean(ref boolean) => !boolean,
105+
JsonValue::Null => true,
106+
JsonValue::Array(ref vec) => vec.is_empty(),
107+
JsonValue::Object(ref btree) => btree.is_empty(),
108+
}
109+
}
110+
67111
#[deprecated(since="0.6.1", note="Use `as_str` instead")]
68112
pub fn as_string(&self) -> JsonResult<&String> {
69113
match *self {
@@ -79,13 +123,6 @@ impl JsonValue {
79123
}
80124
}
81125

82-
pub fn is_number(&self) -> bool {
83-
match *self {
84-
JsonValue::Number(_) => true,
85-
_ => false,
86-
}
87-
}
88-
89126
#[deprecated(since="0.6.1", note="Use `as_f64` instead")]
90127
pub fn as_number(&self) -> JsonResult<&f64> {
91128
match *self {
@@ -145,10 +182,10 @@ impl JsonValue {
145182
self.as_f64().and_then(|value| f64_to_singed!(isize, value))
146183
}
147184

148-
pub fn is_boolean(&self) -> bool {
185+
pub fn as_bool(&self) -> Option<bool> {
149186
match *self {
150-
JsonValue::Boolean(_) => true,
151-
_ => false
187+
JsonValue::Boolean(ref value) => Some(*value),
188+
_ => None
152189
}
153190
}
154191

@@ -160,34 +197,6 @@ impl JsonValue {
160197
}
161198
}
162199

163-
pub fn as_bool(&self) -> Option<bool> {
164-
match *self {
165-
JsonValue::Boolean(ref value) => Some(*value),
166-
_ => None
167-
}
168-
}
169-
170-
pub fn is_null(&self) -> bool {
171-
match *self {
172-
JsonValue::Null => true,
173-
_ => false,
174-
}
175-
}
176-
177-
pub fn is_object(&self) -> bool {
178-
match *self {
179-
JsonValue::Object(_) => true,
180-
_ => false,
181-
}
182-
}
183-
184-
pub fn is_array(&self) -> bool {
185-
match *self {
186-
JsonValue::Array(_) => true,
187-
_ => false,
188-
}
189-
}
190-
191200
/// Works on `JsonValue::Object` - create or override key with value.
192201
#[must_use]
193202
#[deprecated(since="0.6.0", note="Use `object[key] = value.into()` instead")]
@@ -262,6 +271,17 @@ impl JsonValue {
262271
}
263272
}
264273

274+
/// Works on `JsonValue::Array` - remove and return last element from
275+
/// an array. On failure returns a null.
276+
pub fn pop(&mut self) -> JsonValue {
277+
match *self {
278+
JsonValue::Array(ref mut vec) => {
279+
vec.pop().unwrap_or(JsonValue::Null)
280+
},
281+
_ => JsonValue::Null
282+
}
283+
}
284+
265285
/// Works on `JsonValue::Array` - gets a reference to a value at index.
266286
/// For most purposes consider using `array[index]` instead.
267287
#[deprecated(since="0.6.0", note="Use `array[index]` instead")]
@@ -358,6 +378,29 @@ impl JsonValue {
358378
_ => EntriesMut::None
359379
}
360380
}
381+
382+
/// Works on `JsonValue::Object` - remove a key and return the value it held.
383+
/// If the key was not present, the method is called on anything but an
384+
/// object, it will return a null.
385+
pub fn remove(&mut self, key: &str) -> JsonValue {
386+
match *self {
387+
JsonValue::Object(ref mut btree) => {
388+
btree.remove(key).unwrap_or(JsonValue::Null)
389+
},
390+
_ => JsonValue::Null
391+
}
392+
}
393+
394+
/// When called on an array or an object, will wipe them clean. When called
395+
/// on a string will clear the string. Numbers and booleans become null.
396+
pub fn clear(&mut self) {
397+
match *self {
398+
JsonValue::String(ref mut string) => string.clear(),
399+
JsonValue::Object(ref mut btree) => btree.clear(),
400+
JsonValue::Array(ref mut vec) => vec.clear(),
401+
_ => *self = JsonValue::Null,
402+
}
403+
}
361404
}
362405

363406
/// Implements indexing by `usize` to easily access array members:

0 commit comments

Comments
 (0)