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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use alloc::borrow::Cow;
use alloc::string::String;
use alloc::vec::Vec;
use super::array::parse_array;
use super::boolean::parse_false;
use super::boolean::parse_true;
use super::error::*;
use super::null::parse_null;
use super::number::parse_number;
use super::object::parse_object;
use super::string::parse_string;
#[cfg(not(feature = "preserve_order"))]
pub type Object<'a> = alloc::collections::BTreeMap<String, Value<'a>>;
#[cfg(feature = "preserve_order")]
pub type Object<'a> = indexmap::IndexMap<String, Value<'a>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Number<'a> {
Float(&'a [u8], &'a [u8]),
Integer(&'a [u8], &'a [u8]),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value<'a> {
Null,
String(Cow<'a, str>),
Number(Number<'a>),
Bool(bool),
Object(Object<'a>),
Array(Vec<Value<'a>>),
}
pub fn parse(mut json: &[u8]) -> Result<Value, Error> {
parse_value(&mut json)
}
pub fn parse_value<'b, 'a>(values: &'b mut &'a [u8]) -> Result<Value<'a>, Error> {
skip_unused(values);
let token = current_token(values)?;
match token {
b'{' => parse_object(values).map(Value::Object),
b'[' => parse_array(values).map(Value::Array),
b'"' => parse_string(values).map(Value::String),
b'n' => parse_null(values).map(|_| Value::Null),
b't' => parse_true(values).map(|_| Value::Bool(true)),
b'f' => parse_false(values).map(|_| Value::Bool(false)),
b'0'..=b'9' | b'-' => parse_number(values).map(Value::Number),
other => Err(Error::InvalidToken(other)),
}
}
#[inline]
pub fn skip_unused(values: &mut &[u8]) {
while let [first, rest @ ..] = values {
if !matches!(first, b'\n' | b' ' | b'\r' | b'\t') {
break;
}
*values = rest;
}
}
#[inline]
pub fn current_token(values: &[u8]) -> Result<u8, Error> {
if let Some(t) = values.get(0) {
Ok(*t)
} else {
Err(Error::InvalidEOF)
}
}