Skip to main content

JSONDocument

Struct JSONDocument 

Source
pub struct JSONDocument {
    pub value: Option<JSONValue>,
}

Fields§

§value: Option<JSONValue>

Implementations§

Source§

impl JSONDocument

Source

pub fn new() -> JSONDocument

Examples found in repository?
examples/print.rs (line 13)
3fn main() {
4    let data = r#"{
5    "name": "John Doe",
6    "age": 43,
7    "props": { "weight": 76, "height": 2.3 },
8    "primes": [ 11, 13, 17, 19, 23 ],
9    "colors": [ "red", "blue" ]
10  }"#;
11
12    let json = String::from(data);
13    let mut doc = JSONDocument::new();
14    match doc.parse_string(json) {
15        Ok(v) => println!("print: {}", v.to_string()),
16        Err(err) => print!("err: {}", err),
17    }
18}
More examples
Hide additional examples
examples/parse.rs (line 13)
3fn main() {
4    let data = r#"{
5    "name": "John Doe",
6    "age": 43,
7    "props": { "weight": 76, "height": 2.3 },
8    "primes": [ 11, 13, 17, 19, 23 ],
9    "colors": [ "red", "blue" ]
10  }"#;
11
12    let json = String::from(data);
13    let mut doc = JSONDocument::new();
14    match doc.parse_string(json) {
15        Ok(ref mut v) => {
16            println!("name: {}", v.get("name").unwrap()); // John Doe
17            println!("age: {}", v.get("age").unwrap()); // 43
18            match v {
19                JSONValue::Object(hm) => {
20                    *hm.get_mut("age").unwrap() = JSONValue::Number(45f64);
21                }
22                _ => {}
23            };
24            println!("age: {}", v.get("age").unwrap()); // 45
25        }
26        Err(err) => print!("err: {}", err),
27    }
28}
Source

pub fn parse_string(&mut self, content: String) -> Result<JSONValue, JSONError>

Examples found in repository?
examples/print.rs (line 14)
3fn main() {
4    let data = r#"{
5    "name": "John Doe",
6    "age": 43,
7    "props": { "weight": 76, "height": 2.3 },
8    "primes": [ 11, 13, 17, 19, 23 ],
9    "colors": [ "red", "blue" ]
10  }"#;
11
12    let json = String::from(data);
13    let mut doc = JSONDocument::new();
14    match doc.parse_string(json) {
15        Ok(v) => println!("print: {}", v.to_string()),
16        Err(err) => print!("err: {}", err),
17    }
18}
More examples
Hide additional examples
examples/parse.rs (line 14)
3fn main() {
4    let data = r#"{
5    "name": "John Doe",
6    "age": 43,
7    "props": { "weight": 76, "height": 2.3 },
8    "primes": [ 11, 13, 17, 19, 23 ],
9    "colors": [ "red", "blue" ]
10  }"#;
11
12    let json = String::from(data);
13    let mut doc = JSONDocument::new();
14    match doc.parse_string(json) {
15        Ok(ref mut v) => {
16            println!("name: {}", v.get("name").unwrap()); // John Doe
17            println!("age: {}", v.get("age").unwrap()); // 43
18            match v {
19                JSONValue::Object(hm) => {
20                    *hm.get_mut("age").unwrap() = JSONValue::Number(45f64);
21                }
22                _ => {}
23            };
24            println!("age: {}", v.get("age").unwrap()); // 45
25        }
26        Err(err) => print!("err: {}", err),
27    }
28}
Source

pub fn parse_file(&mut self, file: File) -> Result<JSONValue, JSONError>

Source

pub fn to_string(&mut self) -> Result<String, JSONError>

Source

pub fn pretify(&mut self) -> String

Trait Implementations§

Source§

impl Debug for JSONDocument

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.