pub struct JsonNumber { /* private fields */ }Expand description
A JSON number that preserves its exact, validated source representation.
Parsing never silently rounds or truncates: the original token is kept
verbatim and conversions to i64/u64/f64 are explicit and fallible.
Equality is structural over the representation — 1.0, 1, and 1e0
are distinct JsonNumbers. Compare numerically by converting first.
Implementations§
Source§impl JsonNumber
impl JsonNumber
Sourcepub fn new(input: &str) -> Result<Self, JsonNumberError>
pub fn new(input: &str) -> Result<Self, JsonNumberError>
Creates a number from a string, validating it against the strict JSON
number grammar (no leading +, no leading zeros, no NaN/Infinity).
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true if the representation has no fraction or exponent.
Sourcepub fn to_i64(&self) -> Result<i64, JsonNumberError>
pub fn to_i64(&self) -> Result<i64, JsonNumberError>
Converts to i64, or fails if the value is not an integer or is out of
range.
Sourcepub fn to_u64(&self) -> Result<u64, JsonNumberError>
pub fn to_u64(&self) -> Result<u64, JsonNumberError>
Converts to u64, or fails if the value is not a non-negative integer or
is out of range.
Examples found in repository?
9fn main() {
10 // Treat this as untrusted input: parse under a conservative limit profile.
11 let input = r#"{ "service": "api", "port": 8080, "tags": ["a", "b"], "enabled": true }"#;
12 let limits = JsonLimits::conservative();
13
14 let value = match parse_with_limits(input.as_bytes(), limits) {
15 Ok(value) => value,
16 Err(err) => {
17 eprintln!("rejected: {err}");
18 return;
19 }
20 };
21
22 if let JsonValue::Object(object) = &value {
23 if let Some(port) = object.get("port").and_then(JsonValue::as_number) {
24 println!("port = {}", port.to_u64().unwrap());
25 }
26 if let Some(tags) = object.get("tags").and_then(JsonValue::as_array) {
27 println!("tags = {}", tags.len());
28 }
29 }
30
31 // Deterministic, member-order-preserving serialization.
32 println!("compact = {}", to_compact_string(&value));
33
34 // Strict by default: these are all rejected.
35 for bad in [r#"{"a":1,"a":2}"#, "{ /* c */ }", "[1,]", "NaN"] {
36 match parse_with_limits(bad.as_bytes(), limits) {
37 Ok(_) => println!("accepted (unexpected): {bad}"),
38 Err(err) => println!("rejected {bad:?}: {}", err.kind_str()),
39 }
40 }
41}Sourcepub fn to_f64(&self) -> Result<f64, JsonNumberError>
pub fn to_f64(&self) -> Result<f64, JsonNumberError>
Converts to f64. Fails only if the magnitude overflows f64 to
infinity; ordinary rounding of the decimal value is not an error.
Sourcepub fn try_from_f64(value: f64) -> Result<Self, JsonNumberError>
pub fn try_from_f64(value: f64) -> Result<Self, JsonNumberError>
Builds a number from an f64. Fails if the value is NaN or infinite.
Trait Implementations§
Source§impl Clone for JsonNumber
impl Clone for JsonNumber
Source§fn clone(&self) -> JsonNumber
fn clone(&self) -> JsonNumber
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for JsonNumber
impl Debug for JsonNumber
impl Eq for JsonNumber
Source§impl Hash for JsonNumber
impl Hash for JsonNumber
Source§impl PartialEq for JsonNumber
impl PartialEq for JsonNumber
Source§fn eq(&self, other: &JsonNumber) -> bool
fn eq(&self, other: &JsonNumber) -> bool
self and other values to be equal, and is used by ==.