use error::Error;
use serde::de::{self, Unexpected, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt::{self, Debug, Display};
#[cfg(feature = "arbitrary_precision")]
use itoa;
#[cfg(feature = "arbitrary_precision")]
use ryu;
#[cfg(feature = "arbitrary_precision")]
use serde::de::{IntoDeserializer, MapAccess};
use de::ParserNumber;
#[cfg(feature = "arbitrary_precision")]
use error::ErrorCode;
#[cfg(feature = "arbitrary_precision")]
#[doc(hidden)]
pub const TOKEN: &'static str = "$serde_json::private::Number";
#[derive(Clone, PartialEq)]
pub struct Number {
n: N,
}
#[cfg(not(feature = "arbitrary_precision"))]
#[derive(Copy, Clone, PartialEq)]
enum N {
PosInt(u64),
NegInt(i64),
Float(f64),
}
#[cfg(feature = "arbitrary_precision")]
type N = String;
impl Number {
#[inline]
pub fn is_i64(&self) -> bool {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(v) => v <= i64::max_value() as u64,
N::NegInt(_) => true,
N::Float(_) => false,
}
#[cfg(feature = "arbitrary_precision")]
self.as_i64().is_some()
}
#[inline]
pub fn is_u64(&self) -> bool {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(_) => true,
N::NegInt(_) | N::Float(_) => false,
}
#[cfg(feature = "arbitrary_precision")]
self.as_u64().is_some()
}
#[inline]
pub fn is_f64(&self) -> bool {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::Float(_) => true,
N::PosInt(_) | N::NegInt(_) => false,
}
#[cfg(feature = "arbitrary_precision")]
{
for c in self.n.chars() {
if c == '.' || c == 'e' || c == 'E' {
return self.n.parse::<f64>().ok().map_or(false, |f| f.is_finite());
}
}
false
}
}
#[inline]
pub fn as_i64(&self) -> Option<i64> {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(n) => {
if n <= i64::max_value() as u64 {
Some(n as i64)
} else {
None
}
}
N::NegInt(n) => Some(n),
N::Float(_) => None,
}
#[cfg(feature = "arbitrary_precision")]
self.n.parse().ok()
}
#[inline]
pub fn as_u64(&self) -> Option<u64> {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(n) => Some(n),
N::NegInt(_) | N::Float(_) => None,
}
#[cfg(feature = "arbitrary_precision")]
self.n.parse().ok()
}
#[inline]
pub fn as_f64(&self) -> Option<f64> {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
N::PosInt(n) => Some(n as f64),
N::NegInt(n) => Some(n as f64),
N::Float(n) => Some(n),
}
#[cfg(feature = "arbitrary_precision")]
self.n.parse().ok()
}
#[inline]
pub fn from_f64(f: f64) -> Option<Number> {
if f.is_finite() {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::Float(f)
}
#[cfg(feature = "arbitrary_precision")]
{
ryu::Buffer::new().format(f).to_owned()
}
};
Some(Number { n: n })
} else {
None
}
}
#[cfg(feature = "arbitrary_precision")]
#[doc(hidden)]
#[inline]
pub fn from_string_unchecked(n: String) -> Self {
Number { n: n }
}
}
impl fmt::Display for Number {
#[cfg(not(feature = "arbitrary_precision"))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
match self.n {
N::PosInt(u) => Display::fmt(&u, formatter),
N::NegInt(i) => Display::fmt(&i, formatter),
N::Float(f) => Display::fmt(&f, formatter),
}
}
#[cfg(feature = "arbitrary_precision")]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
Display::fmt(&self.n, formatter)
}
}
impl Debug for Number {
#[cfg(not(feature = "arbitrary_precision"))]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let mut debug = formatter.debug_tuple("Number");
match self.n {
N::PosInt(i) => {
debug.field(&i);
}
N::NegInt(i) => {
debug.field(&i);
}
N::Float(f) => {
debug.field(&f);
}
}
debug.finish()
}
#[cfg(feature = "arbitrary_precision")]
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Number({})", &self.n)
}
}
impl Serialize for Number {
#[cfg(not(feature = "arbitrary_precision"))]
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self.n {
N::PosInt(u) => serializer.serialize_u64(u),
N::NegInt(i) => serializer.serialize_i64(i),
N::Float(f) => serializer.serialize_f64(f),
}
}
#[cfg(feature = "arbitrary_precision")]
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
use serde::ser::SerializeStruct;
let mut s = serializer.serialize_struct(TOKEN, 1)?;
s.serialize_field(TOKEN, &self.n)?;
s.end()
}
}
impl<'de> Deserialize<'de> for Number {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Number, D::Error>
where
D: Deserializer<'de>,
{
struct NumberVisitor;
impl<'de> Visitor<'de> for NumberVisitor {
type Value = Number;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a JSON number")
}
#[inline]
fn visit_i64<E>(self, value: i64) -> Result<Number, E> {
Ok(value.into())
}
#[inline]
fn visit_u64<E>(self, value: u64) -> Result<Number, E> {
Ok(value.into())
}
#[inline]
fn visit_f64<E>(self, value: f64) -> Result<Number, E>
where
E: de::Error,
{
Number::from_f64(value).ok_or_else(|| de::Error::custom("not a JSON number"))
}
#[cfg(feature = "arbitrary_precision")]
#[inline]
fn visit_map<V>(self, mut visitor: V) -> Result<Number, V::Error>
where
V: de::MapAccess<'de>,
{
let value = visitor.next_key::<NumberKey>()?;
if value.is_none() {
return Err(de::Error::invalid_type(Unexpected::Map, &self));
}
let v: NumberFromString = visitor.next_value()?;
Ok(v.value)
}
}
deserializer.deserialize_any(NumberVisitor)
}
}
#[cfg(feature = "arbitrary_precision")]
struct NumberKey;
#[cfg(feature = "arbitrary_precision")]
impl<'de> de::Deserialize<'de> for NumberKey {
fn deserialize<D>(deserializer: D) -> Result<NumberKey, D::Error>
where
D: de::Deserializer<'de>,
{
struct FieldVisitor;
impl<'de> de::Visitor<'de> for FieldVisitor {
type Value = ();
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a valid number field")
}
fn visit_str<E>(self, s: &str) -> Result<(), E>
where
E: de::Error,
{
if s == TOKEN {
Ok(())
} else {
Err(de::Error::custom("expected field with custom name"))
}
}
}
deserializer.deserialize_identifier(FieldVisitor)?;
Ok(NumberKey)
}
}
#[cfg(feature = "arbitrary_precision")]
pub struct NumberFromString {
pub value: Number,
}
#[cfg(feature = "arbitrary_precision")]
impl<'de> de::Deserialize<'de> for NumberFromString {
fn deserialize<D>(deserializer: D) -> Result<NumberFromString, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
impl<'de> de::Visitor<'de> for Visitor {
type Value = NumberFromString;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("string containing a number")
}
fn visit_str<E>(self, s: &str) -> Result<NumberFromString, E>
where
E: de::Error,
{
let n = try!(s.parse().map_err(de::Error::custom));
Ok(NumberFromString { value: n })
}
}
deserializer.deserialize_str(Visitor)
}
}
#[cfg(feature = "arbitrary_precision")]
fn invalid_number() -> Error {
Error::syntax(ErrorCode::InvalidNumber, 0, 0)
}
macro_rules! deserialize_any {
(@expand [$($num_string:tt)*]) => {
#[cfg(not(feature = "arbitrary_precision"))]
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
match self.n {
N::PosInt(u) => visitor.visit_u64(u),
N::NegInt(i) => visitor.visit_i64(i),
N::Float(f) => visitor.visit_f64(f),
}
}
#[cfg(feature = "arbitrary_precision")]
#[inline]
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where V: Visitor<'de>
{
if let Some(u) = self.as_u64() {
return visitor.visit_u64(u);
} else if let Some(i) = self.as_i64() {
return visitor.visit_i64(i);
} else if let Some(f) = self.as_f64() {
if f.to_string() == self.n {
return visitor.visit_f64(f);
}
}
visitor.visit_map(NumberDeserializer {
number: Some(self.$($num_string)*),
})
}
};
(owned) => {
deserialize_any!(@expand [n]);
};
(ref) => {
deserialize_any!(@expand [n.clone()]);
};
}
macro_rules! deserialize_number {
($deserialize:ident => $visit:ident) => {
#[cfg(not(feature = "arbitrary_precision"))]
fn $deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'de>,
{
self.deserialize_any(visitor)
}
#[cfg(feature = "arbitrary_precision")]
fn $deserialize<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: de::Visitor<'de>,
{
visitor.$visit(self.n.parse().map_err(|_| invalid_number())?)
}
}
}
impl<'de> Deserializer<'de> for Number {
type Error = Error;
deserialize_any!(owned);
deserialize_number!(deserialize_i8 => visit_i8);
deserialize_number!(deserialize_i16 => visit_i16);
deserialize_number!(deserialize_i32 => visit_i32);
deserialize_number!(deserialize_i64 => visit_i64);
deserialize_number!(deserialize_u8 => visit_u8);
deserialize_number!(deserialize_u16 => visit_u16);
deserialize_number!(deserialize_u32 => visit_u32);
deserialize_number!(deserialize_u64 => visit_u64);
deserialize_number!(deserialize_f32 => visit_f32);
deserialize_number!(deserialize_f64 => visit_f64);
serde_if_integer128! {
deserialize_number!(deserialize_i128 => visit_i128);
deserialize_number!(deserialize_u128 => visit_u128);
}
forward_to_deserialize_any! {
bool char str string bytes byte_buf option unit unit_struct
newtype_struct seq tuple tuple_struct map struct enum identifier
ignored_any
}
}
impl<'de, 'a> Deserializer<'de> for &'a Number {
type Error = Error;
deserialize_any!(ref);
deserialize_number!(deserialize_i8 => visit_i8);
deserialize_number!(deserialize_i16 => visit_i16);
deserialize_number!(deserialize_i32 => visit_i32);
deserialize_number!(deserialize_i64 => visit_i64);
deserialize_number!(deserialize_u8 => visit_u8);
deserialize_number!(deserialize_u16 => visit_u16);
deserialize_number!(deserialize_u32 => visit_u32);
deserialize_number!(deserialize_u64 => visit_u64);
deserialize_number!(deserialize_f32 => visit_f32);
deserialize_number!(deserialize_f64 => visit_f64);
serde_if_integer128! {
deserialize_number!(deserialize_i128 => visit_i128);
deserialize_number!(deserialize_u128 => visit_u128);
}
forward_to_deserialize_any! {
bool char str string bytes byte_buf option unit unit_struct
newtype_struct seq tuple tuple_struct map struct enum identifier
ignored_any
}
}
#[cfg(feature = "arbitrary_precision")]
#[doc(hidden)]
pub struct NumberDeserializer {
pub number: Option<String>,
}
#[cfg(feature = "arbitrary_precision")]
impl<'de> MapAccess<'de> for NumberDeserializer {
type Error = Error;
fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>, Error>
where
K: de::DeserializeSeed<'de>,
{
if self.number.is_none() {
return Ok(None);
}
seed.deserialize(NumberFieldDeserializer).map(Some)
}
fn next_value_seed<V>(&mut self, seed: V) -> Result<V::Value, Error>
where
V: de::DeserializeSeed<'de>,
{
seed.deserialize(self.number.take().unwrap().into_deserializer())
}
}
#[cfg(feature = "arbitrary_precision")]
struct NumberFieldDeserializer;
#[cfg(feature = "arbitrary_precision")]
impl<'de> Deserializer<'de> for NumberFieldDeserializer {
type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Error>
where
V: de::Visitor<'de>,
{
visitor.visit_borrowed_str(TOKEN)
}
forward_to_deserialize_any! {
bool u8 u16 u32 u64 u128 i8 i16 i32 i64 i128 f32 f64 char str string seq
bytes byte_buf map struct option unit newtype_struct ignored_any
unit_struct tuple_struct tuple enum identifier
}
}
impl From<ParserNumber> for Number {
fn from(value: ParserNumber) -> Self {
let n = match value {
ParserNumber::F64(f) => {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::Float(f)
}
#[cfg(feature = "arbitrary_precision")]
{
f.to_string()
}
}
ParserNumber::U64(u) => {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::PosInt(u)
}
#[cfg(feature = "arbitrary_precision")]
{
u.to_string()
}
}
ParserNumber::I64(i) => {
#[cfg(not(feature = "arbitrary_precision"))]
{
N::NegInt(i)
}
#[cfg(feature = "arbitrary_precision")]
{
i.to_string()
}
}
#[cfg(feature = "arbitrary_precision")]
ParserNumber::String(s) => s,
};
Number { n: n }
}
}
macro_rules! impl_from_unsigned {
(
$($ty:ty),*
) => {
$(
impl From<$ty> for Number {
#[inline]
fn from(u: $ty) -> Self {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{ N::PosInt(u as u64) }
#[cfg(feature = "arbitrary_precision")]
{
itoa::Buffer::new().format(u).to_owned()
}
};
Number { n: n }
}
}
)*
};
}
macro_rules! impl_from_signed {
(
$($ty:ty),*
) => {
$(
impl From<$ty> for Number {
#[inline]
fn from(i: $ty) -> Self {
let n = {
#[cfg(not(feature = "arbitrary_precision"))]
{
if i < 0 {
N::NegInt(i as i64)
} else {
N::PosInt(i as u64)
}
}
#[cfg(feature = "arbitrary_precision")]
{
itoa::Buffer::new().format(i).to_owned()
}
};
Number { n: n }
}
}
)*
};
}
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"))]
#[doc(hidden)]
#[cold]
pub fn unexpected(&self) -> Unexpected {
match self.n {
N::PosInt(u) => Unexpected::Unsigned(u),
N::NegInt(i) => Unexpected::Signed(i),
N::Float(f) => Unexpected::Float(f),
}
}
#[cfg(feature = "arbitrary_precision")]
#[doc(hidden)]
#[cold]
pub fn unexpected(&self) -> Unexpected {
Unexpected::Other("number")
}
}