[][src]Enum json_minimal::Json

pub enum Json {
    OBJECT {
        name: String,
        value: Box<Json>,
    },
    JSON(Vec<Json>),
    ARRAY(Vec<Json>),
    STRING(String),
    NUMBER(f64),
    BOOL(bool),
    NULL,
}

Variants

OBJECT

Fields of OBJECT

name: Stringvalue: Box<Json>
JSON(Vec<Json>)
ARRAY(Vec<Json>)
STRING(String)
NUMBER(f64)
BOOL(bool)
NULL

Implementations

impl Json[src]

pub fn new() -> Json[src]

Construct a new Json::JSON

Example

use json_minimal::*;

let mut json = Json::new();

pub fn add(&mut self, value: Json) -> &mut Json[src]

Add any Json variant to a Json variant of type Json::JSON, Json::ARRAY or a Json::OBJECT (holding a Json::JSON,Json::ARRAY,Json::OBJECT (holding a Json::JSON,Json::...)).

Panics!

Will panic if the conditions stated above are not met OR if an attempt is made to add a Json::JSON to a Json::JSON without wrapping it in a Json::OBJECT first.

Example

    use json_minimal::*;
     
    let mut json = Json::new();

    json
        .add(
            Json::OBJECT {
                name: String::from("Greeting"),

                value: Box::new(
                    Json::STRING( String::from("Hello, world!") )
                )
            }
        )
    ;

See the tutorial on github for more.

pub fn get(&self, search: &str) -> Option<&Json>[src]

Get the Json with the requested name if it exists.

Panics

This function will panic if called on a Json variant other than Json::JSON or Json::OBJECT, as only these two variants may hold Json::OBJECT (which has a name field).

Example

use json_minimal::*;

let mut json = Json::new();

json
    .add(
        Json::OBJECT {
            name: String::from("Greeting"),

            value: Box::new(
                Json::STRING( String::from("Hello, world!") )
            )
        }
    )
;

match json.get("Greeting") {
    Some(json) => {
        match json {
            Json::OBJECT { name, value } => {
                match value.unbox() { // See `unbox()` below
                    Json::STRING(val) => {
                        assert_eq!("Hello, world!",val);
                    },
                    _ => {
                        panic!("I expected this to be a `Json::STRING`!!!");
                    }
                }   
            },
            _ => {
                panic!("This shouldn't happen!!!");
            }
        }
    },
    None => {
        panic!("Not found!!!");
    }
}

pub fn get_mut(&mut self, search: &str) -> Option<&mut Json>[src]

Same as get above, but the references are mutable. Use unbox_mut() (see below) with this one.

Panics

This function will panic if called on a Json variant other than Json::JSON or Json::OBJECT, as only these two variants may hold Json::OBJECT which has a name field.

pub fn unbox(&self) -> &Json[src]

Enables matching the contents of a Box.

pub fn unbox_mut(&mut self) -> &mut Json[src]

Idem.

pub fn print(&self) -> String[src]

Returns a String of the form: {"Json":"Value",...} but can also be called on 'standalone objects' which could result in "Object":{"Stuff":...} or "Json":true.

pub fn parse(input: &[u8]) -> Result<Json, (usize, &'static str)>[src]

Parses the given bytes if a json structure is found. It even works with \"Hello\":\"World\" (doesn't have to be like {...}), i.e. it can return any of the variants in the Json enum. The error is returned in the form (last position, what went wrong). Unfortunately the error description are minimal (basically "Error parsing ...type...").

Example

use json_minimal::*;

match Json::parse(b"{\"Greeting\":\"Hello, world!\"}") {
    Ok(json) => {
         
        match json.get("Greeting") {
            Some(json) => {
                match json {
                    Json::OBJECT { name, value } => {
                        match value.unbox() {
                            Json::STRING(val) => {
                                assert_eq!(val,"Hello, world!");
                            },
                            json => {
                                panic!("Expected Json::STRING but found {:?}!!!",json);
                            }
                        }
                    }
                    json => {
                        panic!("Expected Json::OBJECT but found {:?}!!!",json);
                    }
                }
            },
            None => {
                panic!("Greeting was not found!!!");
            }
        }
    },
    Err( (pos,msg) ) => {
        panic!("`{}` at position `{}`!!!",msg,pos);
    }
}

See the tutorial on github for more.

Trait Implementations

impl Debug for Json[src]

Auto Trait Implementations

impl RefUnwindSafe for Json

impl Send for Json

impl Sync for Json

impl Unpin for Json

impl UnwindSafe for Json

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.