-// Copyright 2017 Serde Developers
-//
-// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
use error::Error;
use serde::de::{self, Unexpected, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// For any Number on which `is_i64` returns true, `as_i64` is guaranteed to
/// return the integer value.
///
- /// ```rust
- /// # #[macro_use]
- /// # extern crate serde_json;
+ /// ```edition2018
+ /// # use serde_json::json;
/// #
- /// # fn main() {
/// let big = i64::max_value() as u64 + 10;
/// let v = json!({ "a": 64, "b": big, "c": 256.0 });
///
///
/// // Numbers with a decimal point are not considered integers.
/// assert!(!v["c"].is_i64());
- /// # }
/// ```
#[inline]
pub fn is_i64(&self) -> bool {
/// For any Number on which `is_u64` returns true, `as_u64` is guaranteed to
/// return the integer value.
///
- /// ```rust
- /// # #[macro_use]
- /// # extern crate serde_json;
+ /// ```edition2018
+ /// # use serde_json::json;
/// #
- /// # fn main() {
/// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
///
/// assert!(v["a"].is_u64());
///
/// // Numbers with a decimal point are not considered integers.
/// assert!(!v["c"].is_u64());
- /// # }
/// ```
#[inline]
pub fn is_u64(&self) -> bool {
/// Currently this function returns true if and only if both `is_i64` and
/// `is_u64` return false but this is not a guarantee in the future.
///
- /// ```rust
- /// # #[macro_use]
- /// # extern crate serde_json;
+ /// ```edition2018
+ /// # use serde_json::json;
/// #
- /// # fn main() {
/// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
///
/// assert!(v["a"].is_f64());
/// // Integers.
/// assert!(!v["b"].is_f64());
/// assert!(!v["c"].is_f64());
- /// # }
/// ```
#[inline]
pub fn is_f64(&self) -> bool {
/// If the `Number` is an integer, represent it as i64 if possible. Returns
/// None otherwise.
///
- /// ```rust
- /// # #[macro_use]
- /// # extern crate serde_json;
+ /// ```edition2018
+ /// # use serde_json::json;
/// #
- /// # fn main() {
/// let big = i64::max_value() as u64 + 10;
/// let v = json!({ "a": 64, "b": big, "c": 256.0 });
///
/// assert_eq!(v["a"].as_i64(), Some(64));
/// assert_eq!(v["b"].as_i64(), None);
/// assert_eq!(v["c"].as_i64(), None);
- /// # }
/// ```
#[inline]
pub fn as_i64(&self) -> Option<i64> {
/// If the `Number` is an integer, represent it as u64 if possible. Returns
/// None otherwise.
///
- /// ```rust
- /// # #[macro_use]
- /// # extern crate serde_json;
+ /// ```edition2018
+ /// # use serde_json::json;
/// #
- /// # fn main() {
/// let v = json!({ "a": 64, "b": -64, "c": 256.0 });
///
/// assert_eq!(v["a"].as_u64(), Some(64));
/// assert_eq!(v["b"].as_u64(), None);
/// assert_eq!(v["c"].as_u64(), None);
- /// # }
/// ```
#[inline]
pub fn as_u64(&self) -> Option<u64> {
/// Represents the number as f64 if possible. Returns None otherwise.
///
- /// ```rust
- /// # #[macro_use]
- /// # extern crate serde_json;
+ /// ```edition2018
+ /// # use serde_json::json;
/// #
- /// # fn main() {
/// let v = json!({ "a": 256.0, "b": 64, "c": -64 });
///
/// assert_eq!(v["a"].as_f64(), Some(256.0));
/// assert_eq!(v["b"].as_f64(), Some(64.0));
/// assert_eq!(v["c"].as_f64(), Some(-64.0));
- /// # }
/// ```
#[inline]
pub fn as_f64(&self) -> Option<f64> {
/// Converts a finite `f64` to a `Number`. Infinite or NaN values are not JSON
/// numbers.
///
- /// ```rust
+ /// ```edition2018
/// # use std::f64;
/// #
/// # use serde_json::Number;
impl_from_unsigned!(u8, u16, u32, u64, usize);
impl_from_signed!(i8, i16, i32, i64, isize);
+#[cfg(feature = "arbitrary_precision")]
+serde_if_integer128! {
+ impl From<i128> for Number {
+ fn from(i: i128) -> Self {
+ Number { n: i.to_string() }
+ }
+ }
+
+ impl From<u128> for Number {
+ fn from(u: u128) -> Self {
+ Number { n: u.to_string() }
+ }
+ }
+}
+
impl Number {
#[cfg(not(feature = "arbitrary_precision"))]
// Not public API. Should be pub(crate).