Skip to content

Commit 2ae68ef

Browse files
committed
add examples, add convert from/to json
1 parent 1ca4ef4 commit 2ae68ef

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ name = "bson"
1313
[dependencies]
1414
chrono = "*"
1515
byteorder = "*"
16+
rustc-serialize = "*"

examples/decode.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extern crate bson;
2+
3+
use std::fs::File;
4+
5+
fn main() {
6+
let mut f = File::open("examples/test.bson").unwrap();
7+
8+
while let Ok(decoded) = bson::Decoder::new(&mut f).decode_document() {
9+
println!("{:?}", decoded);
10+
}
11+
}

examples/test.bson

316 Bytes
Binary file not shown.

src/bson.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ use std::collections::BTreeMap;
2323
use std::string;
2424

2525
use chrono::{DateTime, UTC};
26+
use rustc_serialize::json;
27+
use rustc_serialize::hex::ToHex;
2628

2729
use spec::BinarySubtype;
2830

@@ -48,3 +50,64 @@ pub enum Bson {
4850

4951
pub type Array = Vec<Bson>;
5052
pub type Document = BTreeMap<String, Bson>;
53+
54+
impl Bson {
55+
pub fn to_json(&self) -> json::Json {
56+
match self {
57+
&Bson::FloatingPoint(v) => json::Json::F64(v),
58+
&Bson::String(ref v) => json::Json::String(v.clone()),
59+
&Bson::Array(ref v) =>
60+
json::Json::Array(v.iter().map(|x| x.to_json()).collect()),
61+
&Bson::Document(ref v) =>
62+
json::Json::Object(v.iter().map(|(k, v)| (k.clone(), v.to_json())).collect()),
63+
&Bson::Boolean(v) => json::Json::Boolean(v),
64+
&Bson::Null => json::Json::Null,
65+
&Bson::RegExp(ref pat, ref opt) => {
66+
let mut re = json::Object::new();
67+
re.insert("pattern".to_string(), json::Json::String(pat.clone()));
68+
re.insert("options".to_string(), json::Json::String(opt.clone()));
69+
70+
json::Json::Object(re)
71+
},
72+
&Bson::JavaScriptCode(ref code) => json::Json::String(code.clone()),
73+
&Bson::JavaScriptCodeWithScope(ref code, ref scope) => {
74+
let mut obj = json::Object::new();
75+
obj.insert("code".to_string(), json::Json::String(code.clone()));
76+
77+
let scope_obj =
78+
scope.iter().map(|(k, v)| (k.clone(), v.to_json())).collect();
79+
80+
obj.insert("scope".to_string(), json::Json::Object(scope_obj));
81+
82+
json::Json::Object(obj)
83+
},
84+
&Bson::Deprecated => json::Json::String("deprecated".to_string()),
85+
&Bson::I32(v) => json::Json::I64(v as i64),
86+
&Bson::I64(v) => json::Json::I64(v),
87+
&Bson::TimeStamp(v) => json::Json::I64(v),
88+
&Bson::Binary(t, ref v) => {
89+
let mut obj = json::Object::new();
90+
let tval: u8 = From::from(t);
91+
obj.insert("type".to_string(), json::Json::I64(tval as i64));
92+
obj.insert("data".to_string(), json::Json::String(v[..].to_hex()));
93+
94+
json::Json::Object(obj)
95+
},
96+
&Bson::ObjectId(v) => json::Json::String(v.to_hex()),
97+
&Bson::UtcDatetime(ref v) => json::Json::String(v.to_string()),
98+
}
99+
}
100+
101+
pub fn from_json(j: &json::Json) -> Bson {
102+
match j {
103+
&json::Json::I64(x) => Bson::I64(x),
104+
&json::Json::U64(x) => Bson::I64(x as i64),
105+
&json::Json::F64(x) => Bson::FloatingPoint(x),
106+
&json::Json::String(ref x) => Bson::String(x.clone()),
107+
&json::Json::Boolean(x) => Bson::Boolean(x),
108+
&json::Json::Array(ref x) => Bson::Array(x.iter().map(|x| Bson::from_json(x)).collect()),
109+
&json::Json::Object(ref x) => Bson::Document(x.iter().map(|(k, v)| (k.clone(), Bson::from_json(v))).collect()),
110+
&json::Json::Null => Bson::Null,
111+
}
112+
}
113+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#![feature(core)]
2323

24+
extern crate rustc_serialize;
2425
extern crate chrono;
2526
extern crate byteorder;
2627

0 commit comments

Comments
 (0)