Function wson::parse

source · []
pub fn parse<'a>(input: &'a str) -> Result<Value, Box<dyn Error + 'a>>
Expand description

Parse json

use nom::error::{ErrorKind, Error};
use nom::Err;
use wson::number::Number;
use wson::{parse, Value};
use std::collections::HashMap;


// the parser will parse "3"
let actual = parse("3")?;
assert_eq!(actual, Value::Number(Number::PositiveInteger(3)));

// the parser will parse " 3 "
let actual = parse(" 3 ")?;
assert_eq!(actual, Value::Number(Number::PositiveInteger(3)));

// the parser will parse "3.2E-1"
let actual = parse("3.2E-1")?;
assert_eq!(actual, Value::Number(Number::Float(0.32)));

// the parser will parse "null"
let actual = parse("null")?;
assert_eq!(actual, Value::Null);

// the parser will parse "true"
let actual = parse("true")?;
assert_eq!(actual, Value::True);

// the parser will parse "false"
let actual = parse("false")?;
assert_eq!(actual, Value::False);

// the parser will parse "\"hello\""
let actual = parse("\"hello\"")?;
assert_eq!(actual, Value::String("hello".to_string()));

// the parser will parse "{\"title\": \"TITLE1\", \"revision\": 12}"
let value = parse("{\"title\": \"TITLE1\", \"revision\": 12}")?;
assert_eq!(value, Value::Object(HashMap::from([
  ("title".to_string(), Value::String("TITLE1".to_string())),
  ("revision".to_string(), Value::Number(Number::PositiveInteger(12)))
])));