Skip to content

Commit e2598bb

Browse files
committed
use json macro
1 parent 455a003 commit e2598bb

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

src/bson.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use chrono::offset::TimeZone;
2929
use oid;
3030
use ordered::OrderedDocument;
3131
use rustc_serialize::hex::{FromHex, ToHex};
32-
use serde_json::{self, Value};
32+
use serde_json:: Value;
3333
use spec::{ElementType, BinarySubtype};
3434

3535
/// Possible BSON value types.
@@ -269,13 +269,11 @@ impl Bson {
269269
/// Convert this value to the best approximate `Json`.
270270
pub fn to_json(&self) -> Value {
271271
match self {
272-
&Bson::FloatingPoint(v) => v.into(),
273-
&Bson::String(ref v) => Value::String(v.clone()),
274-
&Bson::Array(ref v) =>
275-
Value::Array(v.iter().map(|x| x.to_json()).collect()),
276-
&Bson::Document(ref v) =>
277-
Value::Object(v.iter().map(|(k, v)| (k.clone(), v.to_json())).collect::<serde_json::Map<String, Value>>()),
278-
&Bson::Boolean(v) => Value::Bool(v),
272+
&Bson::FloatingPoint(v) => json!(v),
273+
&Bson::String(ref v) => json!(v),
274+
&Bson::Array(ref v) => json!(v),
275+
&Bson::Document(ref v) => json!(v),
276+
&Bson::Boolean(v) => json!(v),
279277
&Bson::Null => Value::Null,
280278
&Bson::RegExp(ref pat, ref opt) => json!({
281279
"$regex": pat,
@@ -312,12 +310,8 @@ impl Bson {
312310
"$numberLong": (v.timestamp() * 1000) + ((v.nanosecond() / 1000000) as i64)
313311
}
314312
}),
315-
&Bson::Symbol(ref v) => {
316-
// FIXME: Don't know what is the best way to encode Symbol type
317-
json!({
318-
"$symbol": v
319-
})
320-
}
313+
// FIXME: Don't know what is the best way to encode Symbol type
314+
&Bson::Symbol(ref v) => json!({"$symbol": v})
321315
}
322316
}
323317

@@ -327,9 +321,9 @@ impl Bson {
327321
&Value::Number(ref x) =>
328322
x.as_i64().map(Bson::from)
329323
.or(x.as_u64().map(Bson::from))
330-
.expect("blah"),
331-
&Value::String(ref x) => Bson::String(x.clone()),
332-
&Value::Bool(x) => Bson::Boolean(x),
324+
.expect(&format!("Invalid number value: {}", x)),
325+
&Value::String(ref x) => x.into(),
326+
&Value::Bool(x) => x.into(),
333327
&Value::Array(ref x) => Bson::Array(x.iter().map(Bson::from_json).collect()),
334328
&Value::Object(ref x) => {
335329
Bson::from_extended_document(x.iter()

0 commit comments

Comments
 (0)