Skip to main content

Number

Struct Number 

Source
pub struct Number<'json, 'p> { /* private fields */ }
Expand description

A JSON number.

Implementations§

Source§

impl<'json, 'p> Number<'json, 'p>

Source

pub fn get(&mut self) -> Result<ParsedNumber<'_>, ParseNumberError>

Try to parse the number.

§Errors

If parsing fails, this will return a ParseNumberError.

Examples found in repository?
examples/format.rs (line 36)
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match &mut *self.0.borrow_mut() {
29            Any::String(string) => {
30                let parsed = string.get().expect("failed to parse string");
31                let raw = parsed.unescaped();
32                write!(f, "{raw:?}")
33            }
34
35            Any::Number(number) => {
36                let parsed = number.get().expect("failed to parse number");
37                <number::ParsedNumber as fmt::Display>::fmt(&parsed, f)
38            }
39
40            Any::Object(object) => {
41                let mut map = f.debug_map();
42
43                while let Some((key, value)) = object.next().expect("failed to parse an object") {
44                    let raw_key = key.unescaped();
45                    map.entry(&raw_key, &Format(RefCell::new(value)));
46                }
47
48                map.finish()
49            }
50
51            Any::Array(array) => {
52                let mut list = f.debug_list();
53
54                while let Some(value) = array.next().expect("failed to parse an array") {
55                    list.entry(&Format(RefCell::new(value)));
56                }
57
58                list.finish()
59            }
60
61            Any::Literal(literal) => {
62                let parsed = literal.get().expect("failed to parse literal");
63                <literal::ParsedLiteral as fmt::Display>::fmt(&parsed, f)
64            }
65        }
66    }
More examples
Hide additional examples
examples/debug.rs (line 24)
3fn main() {
4    let json = r#"{
5        "one": 1,
6        "array": [true, false, null],
7        "object": {"pi": 3.14, "exp": 1e5, "ignore": "this"}
8    }"#;
9
10    let mut document = Document::new(json);
11
12    // root
13    let mut root = document
14        .next()
15        .expect("failed to parse document")
16        .and_then(Any::object)
17        .expect("failed to get an object from the document");
18
19    // "one"
20    let element = root.next().expect("failed to parse object");
21    let Some((key, Any::Number(mut one))) = element else {
22        panic!("failed to get a number from the object");
23    };
24    let one = one.get().expect("failed to parse number");
25
26    assert_eq!(key, "one");
27    assert_eq!(one.as_u8(), Some(1));
28
29    // "array"
30    let element = root.next().expect("failed to parse object");
31    let Some((key, Any::Array(mut array))) = element else {
32        panic!("failed to get an array from the object");
33    };
34
35    assert_eq!(key, "array");
36
37    // "array" -> 0
38    let mut r#true = array
39        .next()
40        .expect("failed to parse array")
41        .and_then(Any::literal)
42        .expect("failed to get a true value from the array");
43
44    // debug print the true literal
45    println!("{true:#?}");
46
47    let r#true = r#true.get().expect("failed to parse a true value");
48    assert_eq!(r#true, true);
49
50    // skip the rest of "array"
51    array.finish().expect("failed to parse array");
52
53    // skip the rest of root
54    root.finish().expect("failed to parse object");
55
56    // finish document
57    document.finish().expect("failed to parse document");
58}
examples/no_std.rs (line 26)
5fn main() {
6    let json = r#"{
7        "one": 1,
8        "array": [true, false, null],
9        "object": {"pi": 3.14, "exp": 1e5, "ignore": "this"}
10    }"#;
11
12    let mut document = Document::new(json);
13
14    // root
15    let mut root = document
16        .next()
17        .expect("failed to parse document")
18        .and_then(Any::object)
19        .expect("failed to get an object from the document");
20
21    // "one"
22    let element = root.next().expect("failed to parse object");
23    let Some((key, Any::Number(mut one))) = element else {
24        panic!("failed to get a number from the object");
25    };
26    let one = one.get().expect("failed to parse number");
27
28    assert_eq!(key, "one");
29    assert_eq!(one.as_u8(), Some(1));
30
31    // "array"
32    let element = root.next().expect("failed to parse object");
33    let Some((key, Any::Array(mut array))) = element else {
34        panic!("failed to get an array from the object");
35    };
36
37    assert_eq!(key, "array");
38
39    // "array" -> 0
40    let r#true = array
41        .next()
42        .expect("failed to parse array")
43        .and_then(Any::literal)
44        .expect("failed to get a true value from the array")
45        .get()
46        .expect("failed to parse a true value");
47
48    assert_eq!(r#true, true);
49
50    // "array" -> 1
51    let r#false = array
52        .next()
53        .expect("failed to parse array")
54        .and_then(Any::literal)
55        .expect("failed to get a false value from the array")
56        .get()
57        .expect("failed to parse a false value");
58
59    assert_eq!(r#false, false);
60
61    // "array" -> 2
62    let null = array
63        .next()
64        .expect("failed to parse array")
65        .and_then(Any::literal)
66        .expect("failed to get a null value from the array")
67        .get()
68        .expect("failed to parse a null value");
69
70    assert_eq!(null, None);
71
72    // finish "array"
73    let array_element = array.next().expect("failed to parse array");
74    assert!(array_element.is_none());
75
76    // "object"
77    let element = root.next().expect("failed to parse object");
78    let Some((key, Any::Object(mut object))) = element else {
79        panic!("failed to get an object from the object");
80    };
81
82    assert_eq!(key, "object");
83
84    // "object" -> "pi"
85    let object_element = object.next().expect("failed to parse inner object");
86    let Some((key, Any::Number(mut pi))) = object_element else {
87        panic!("failed to get a number from the inner object");
88    };
89    let pi = pi.get().expect("failed to parse a number");
90
91    assert_eq!(key, "pi");
92    assert_eq!(pi, 3.14);
93
94    // "object" -> "exp"
95    let object_element = object.next().expect("failed to parse inner object");
96    let Some((key, Any::Number(mut exp))) = object_element else {
97        panic!("failed to get a number from the inner object");
98    };
99    let exp = exp.get().expect("failed to parse a number");
100
101    assert_eq!(key, "exp");
102    assert_eq!(exp.as_f32(), 1e5);
103
104    // skip the rest of "object"
105    object.finish().expect("failed to parse inner object");
106
107    // finish root
108    root.finish().expect("failed to parse object");
109
110    // finish document
111    document.finish().expect("failed to parse document");
112}
Source

pub fn finish(&mut self) -> Result<(), ParseNumberError>

Finish parsing the number so that the parent can continue.

If Self::get has been called, this is not needed.

§Errors

If parsing fails in this string, the error is returned as a ParseNumberError.

Trait Implementations§

Source§

impl<'json, 'p> Debug for Number<'json, 'p>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'json, 'p> !RefUnwindSafe for Number<'json, 'p>

§

impl<'json, 'p> !Send for Number<'json, 'p>

§

impl<'json, 'p> !Sync for Number<'json, 'p>

§

impl<'json, 'p> !UnwindSafe for Number<'json, 'p>

§

impl<'json, 'p> Freeze for Number<'json, 'p>

§

impl<'json, 'p> Unpin for Number<'json, 'p>

§

impl<'json, 'p> UnsafeUnpin for Number<'json, 'p>

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.