78
78
//! data_vector: Vec<u8>,
79
79
//! }
80
80
//!
81
- //! fn main() {
82
- //! let object = TestStruct {
83
- //! data_int: 1,
84
- //! data_str: "homura".to_string(),
85
- //! data_vector: vec![2,3,4,5],
86
- //! };
81
+ //! let object = TestStruct {
82
+ //! data_int: 1,
83
+ //! data_str: "homura".to_string(),
84
+ //! data_vector: vec![2,3,4,5],
85
+ //! };
87
86
//!
88
- //! // Serialize using `json::encode`
89
- //! let encoded = json::encode(&object).unwrap();
87
+ //! // Serialize using `json::encode`
88
+ //! let encoded = json::encode(&object).unwrap();
90
89
//!
91
- //! // Deserialize using `json::decode`
92
- //! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
93
- //! }
90
+ //! // Deserialize using `json::decode`
91
+ //! let decoded: TestStruct = json::decode(&encoded[..]).unwrap();
94
92
//! ```
95
93
//!
96
94
//! ## Using the `ToJson` trait
125
123
//! val: Json,
126
124
//! }
127
125
//!
128
- //! fn main() {
129
- //! let num = ComplexNum { a: 0.0001, b: 12.539 };
130
- //! let data: String = json::encode(&ComplexNumRecord{
131
- //! uid: 1,
132
- //! dsc: "test".to_string(),
133
- //! val: num.to_json(),
134
- //! }).unwrap();
135
- //! println!("data: {}", data);
136
- //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
137
- //! }
126
+ //! let num = ComplexNum { a: 0.0001, b: 12.539 };
127
+ //! let data: String = json::encode(&ComplexNumRecord{
128
+ //! uid: 1,
129
+ //! dsc: "test".to_string(),
130
+ //! val: num.to_json(),
131
+ //! }).unwrap();
132
+ //! println!("data: {}", data);
133
+ //! // data: {"uid":1,"dsc":"test","val":"0.0001+12.539i"};
138
134
//! ```
139
135
//!
140
136
//! ### Verbose example of `ToJson` usage
164
160
//! }
165
161
//! }
166
162
//!
167
- //! fn main() {
168
- //! // Serialize using `ToJson`
169
- //! let input_data = TestStruct {
170
- //! data_int: 1,
171
- //! data_str: "madoka".to_string(),
172
- //! data_vector: vec![2,3,4,5],
173
- //! };
174
- //! let json_obj: Json = input_data.to_json();
175
- //! let json_str: String = json_obj.to_string();
163
+ //! // Serialize using `ToJson`
164
+ //! let input_data = TestStruct {
165
+ //! data_int: 1,
166
+ //! data_str: "madoka".to_string(),
167
+ //! data_vector: vec![2,3,4,5],
168
+ //! };
169
+ //! let json_obj: Json = input_data.to_json();
170
+ //! let json_str: String = json_obj.to_string();
176
171
//!
177
- //! // Deserialize like before
178
- //! let decoded: TestStruct = json::decode(&json_str).unwrap();
179
- //! }
172
+ //! // Deserialize like before
173
+ //! let decoded: TestStruct = json::decode(&json_str).unwrap();
180
174
//! ```
181
175
182
176
use self :: DecoderError :: * ;
@@ -1269,34 +1263,22 @@ impl Json {
1269
1263
1270
1264
/// Returns `true` if the Json value is a `Number`.
1271
1265
pub fn is_number ( & self ) -> bool {
1272
- match * self {
1273
- Json :: I64 ( _) | Json :: U64 ( _) | Json :: F64 ( _) => true ,
1274
- _ => false ,
1275
- }
1266
+ matches ! ( * self , Json :: I64 ( _) | Json :: U64 ( _) | Json :: F64 ( _) )
1276
1267
}
1277
1268
1278
1269
/// Returns `true` if the Json value is a `i64`.
1279
1270
pub fn is_i64 ( & self ) -> bool {
1280
- match * self {
1281
- Json :: I64 ( _) => true ,
1282
- _ => false ,
1283
- }
1271
+ matches ! ( * self , Json :: I64 ( _) )
1284
1272
}
1285
1273
1286
1274
/// Returns `true` if the Json value is a `u64`.
1287
1275
pub fn is_u64 ( & self ) -> bool {
1288
- match * self {
1289
- Json :: U64 ( _) => true ,
1290
- _ => false ,
1291
- }
1276
+ matches ! ( * self , Json :: U64 ( _) )
1292
1277
}
1293
1278
1294
1279
/// Returns `true` if the Json value is a `f64`.
1295
1280
pub fn is_f64 ( & self ) -> bool {
1296
- match * self {
1297
- Json :: F64 ( _) => true ,
1298
- _ => false ,
1299
- }
1281
+ matches ! ( * self , Json :: F64 ( _) )
1300
1282
}
1301
1283
1302
1284
/// If the Json value is a number, returns or cast it to a `i64`;
@@ -1416,6 +1398,7 @@ enum ParserState {
1416
1398
/// structure of the JSON stream.
1417
1399
///
1418
1400
/// An example is `foo.bar[3].x`.
1401
+ #[ derive( Default ) ]
1419
1402
pub struct Stack {
1420
1403
stack : Vec < InternalStackElement > ,
1421
1404
str_buffer : Vec < u8 > ,
@@ -1442,7 +1425,7 @@ enum InternalStackElement {
1442
1425
1443
1426
impl Stack {
1444
1427
pub fn new ( ) -> Stack {
1445
- Stack { stack : Vec :: new ( ) , str_buffer : Vec :: new ( ) }
1428
+ Self :: default ( )
1446
1429
}
1447
1430
1448
1431
/// Returns The number of elements in the Stack.
@@ -1547,10 +1530,7 @@ impl Stack {
1547
1530
1548
1531
// Used by Parser to test whether the top-most element is an index.
1549
1532
fn last_is_index ( & self ) -> bool {
1550
- match self . stack . last ( ) {
1551
- Some ( InternalIndex ( _) ) => true ,
1552
- _ => false ,
1553
- }
1533
+ matches ! ( self . stack. last( ) , Some ( InternalIndex ( _) ) )
1554
1534
}
1555
1535
1556
1536
// Used by Parser to increment the index of the top-most element.
0 commit comments