Debian Codegraph @

src/extjson/mod.rs

raw
1 //! Deserialization and serialization of [MongoDB Extended JSON v2](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/)
2 //!
3 //! ## Overview of Extended JSON
4 //!
5 //! MongoDB Extended JSON (abbreviated extJSON) is format of JSON that allows for the encoding of
6 //! BSON type information. Normal JSON cannot unambiguously represent all BSON types losslessly, so
7 //! an extension was designed to include conventions for representing those types.
8 //!
9 //! For example, a BSON binary is represented by the following format:
10 //! ```text
11 //! {
12 //! "$binary": {
13 //! "base64": <base64 encoded payload as a string>,
14 //! "subType": <subtype as a one or two character hex string>,
15 //! }
16 //! }
17 //! ```
18 //! For more information on extJSON and the complete list of translations, see the [official MongoDB documentation](https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/).
19 //!
20 //! All MongoDB drivers and BSON libraries interpret and produce extJSON, so it can serve as a
21 //! useful tool for communicating between applications where raw BSON bytes cannot be used (e.g. via
22 //! JSON REST APIs). It's also useful for representing BSON data as a string.
23 //!
24 //! ### Canonical and Relaxed Modes
25 //!
26 //! There are two modes of extJSON: "Canonical" and "Relaxed". They are the same except for the
27 //! following differences:
28 //! - In relaxed mode, all BSON numbers are represented by the JSON number type, rather than the
29 //! object
30 //! notation.
31 //! - In relaxed mode, the string in the datetime object notation is RFC 3339 (ISO-8601) formatted
32 //! (if the date is after 1970).
33 //!
34 //! e.g.
35 //! ```rust
36 //! # use bson::bson;
37 //! let doc = bson!({ "x": 5, "d": bson::DateTime::now() });
38 //!
39 //! println!("relaxed: {}", doc.clone().into_relaxed_extjson());
40 //! // relaxed: "{"x":5,"d":{"$date":"2020-06-01T22:19:13.075Z"}}"
41 //!
42 //! println!("canonical: {}", doc.into_canonical_extjson());
43 //! // canonical: {"x":{"$numberInt":"5"},"d":{"$date":{"$numberLong":"1591050020711"}}}
44 //! ```
45 //!
46 //! Canonical mode is useful when BSON values need to be round tripped without losing any type
47 //! information. Relaxed mode is more useful when debugging or logging BSON data.
48 //!
49 //! ## Deserializing Extended JSON
50 //!
51 //! Extended JSON can be deserialized using [`Bson`](../enum.Bson.html)'s
52 //! `TryFrom<serde_json::Value>` implementation. This implementation accepts both canonical and
53 //! relaxed extJSON, and the two modes can even be mixed within a single representation.
54 //!
55 //! e.g.
56 //! ```rust
57 //! # use bson::Bson;
58 //! # use serde_json::json;
59 //! # use std::convert::{TryFrom, TryInto};
60 //! let json_doc = json!({ "x": 5i32, "y": { "$numberInt": "5" }, "z": { "subdoc": "hello" } });
61 //! let bson: Bson = json_doc.try_into().unwrap(); // Bson::Document(...)
62 //!
63 //! let json_date = json!({ "$date": { "$numberLong": "1590972160292" } });
64 //! let bson_date: Bson = json_date.try_into().unwrap(); // Bson::DateTime(...)
65 //!
66 //! let invalid_ext_json = json!({ "$numberLong": 5 });
67 //! Bson::try_from(invalid_ext_json).expect_err("5 should be a string");
68 //! ```
69 //!
70 //! ## Serializing to Extended JSON
71 //!
72 //! Extended JSON can be created via [`Bson`](../enum.Bson.html)'s `Into<serde_json::Value>`
73 //! implementation (which will create relaxed extJSON),
74 //! [`Bson::into_relaxed_extjson`](../enum.Bson.html#method.into_relaxed_extjson), and
75 //! [`Bson::into_canonical_extjson`](../enum.Bson.html#method.into_canonical_extjson).
76 //!
77 //! e.g.
78 //! ```rust
79 //! # use bson::{bson, oid};
80 //! let doc = bson!({ "x": 5i32, "_id": oid::ObjectId::new() });
81 //!
82 //! let relaxed_extjson: serde_json::Value = doc.clone().into();
83 //! println!("{}", relaxed_extjson); // { "x": 5, "_id": { "$oid": <hexstring> } }
84 //!
85 //! let relaxed_extjson = doc.clone().into_relaxed_extjson();
86 //! println!("{}", relaxed_extjson); // { "x": 5, "_id": { "$oid": <hexstring> } }
87 //!
88 //! let canonical_extjson = doc.into_canonical_extjson();
89 //! println!("{}", canonical_extjson); // { "x": { "$numberInt": "5" }, "_id": { "$oid": <hexstring> } }
90 //! ```
91
92 pub mod de;
93 pub(crate) mod models;