jsonway/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#![crate_type = "rlib"]
#![deny(warnings)]
#![deny(bad_style)]

extern crate serde;
extern crate serde_json;

pub use object_builder::ObjectBuilder;
pub use array_builder::ArrayBuilder;
pub use serializer::{Serializer, ObjectSerializer, ObjectScopeSerializer};
pub use array_serializer::ArraySerializer;

pub mod array_builder;
pub mod object_builder;
pub mod serializer;
pub mod array_serializer;

/// ```rust
/// let json = jsonway::object(|json| {
///     json.set("first_name", "Luke");
///     json.set("last_name", "Skywalker");
///
///     json.object("info", |json| {
///         json.set("homeworld", "Tatooine");
///         json.set("born", "19 BBY");
///         json.set("died", "Between 45 ABY and 137 ABY");
///     });
///
///     json.array("masters", |json| {
///         json.push("Obi-Wan Kenobi");
///         json.push("Yoda");
///         json.push("Joruus C'baoth (Briefly)");
///         json.push("Darth Sidious (Briefly)");
///     });
/// }).unwrap();
///
/// assert_eq!(json.get("first_name").unwrap().as_str().unwrap(), "Luke");
/// assert_eq!(json.get("last_name").unwrap().as_str().unwrap(), "Skywalker");
///
/// assert!(json.get("info").unwrap().is_object());
/// assert!(json.get("masters").unwrap().is_array());
/// ```

/// Create and return new ListBuilder
pub fn array<F>(builder: F) -> ArrayBuilder where F: FnOnce(&mut ArrayBuilder) {
    ArrayBuilder::build(builder)
}

/// Create and return new ObjectBuilder
pub fn object<F>(builder: F) -> ObjectBuilder where F: FnOnce(&mut ObjectBuilder) {
    ObjectBuilder::build(builder)
}