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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::*;
use std::convert::TryFrom;

impl Serialize for bool {
    #[inline]
    fn json_write<W>(&self, writer: &mut W) -> Result
    where
        W: Write,
    {
        if *self {
            writer.write_all(b"true")
        } else {
            writer.write_all(b"false")
        }
    }
}

impl<'input> Deserialize<'input> for bool {
    #[inline]
    fn from_tape(tape: &mut Tape<'input>) -> simd_json::Result<Self>
    where
        Self: std::marker::Sized + 'input,
    {
        if let Some(simd_json::Node::Static(simd_json::StaticNode::Bool(r))) = tape.next() {
            Ok(r)
        } else {
            Err(simd_json::Error::generic(
                simd_json::ErrorType::ExpectedBoolean,
            ))
        }
    }
}

macro_rules! itoa {
    ($t:ty) => {
        impl Serialize for $t {
            #[inline]
            fn json_write<W>(&self, writer: &mut W) -> io::Result<()>
            where
                W: Write,
            {
                itoa::write(writer, *self).map(|_| ())
            }
        }

        impl<'input> Deserialize<'input> for $t {
            #[inline]
            fn from_tape(tape: &mut Tape<'input>) -> simd_json::Result<Self>
            where
                Self: std::marker::Sized + 'input,
            {
                match tape.next() {
                    Some(simd_json::Node::Static(simd_json::StaticNode::I64(i))) => {
                        <$t>::try_from(i).map_err(|_| {
                            simd_json::Error::generic(simd_json::ErrorType::ExpectedInteger)
                        })
                    }
                    Some(simd_json::Node::Static(simd_json::StaticNode::U64(i))) => {
                        <$t>::try_from(i).map_err(|_| {
                            simd_json::Error::generic(simd_json::ErrorType::ExpectedInteger)
                        })
                    }
                    #[cfg(feature = "128bit")]
                    Some(simd_json::Node::Static(simd_json::StaticNode::U128(i))) => {
                        <$t>::try_from(i).map_err(|_| {
                            simd_json::Error::generic(simd_json::ErrorType::ExpectedInteger)
                        })
                    }
                    #[cfg(feature = "128bit")]
                    Some(simd_json::Node::Static(simd_json::StaticNode::UI28(i))) => {
                        <$t>::try_from(i).map_err(|_| {
                            simd_json::Error::generic(simd_json::ErrorType::ExpectedInteger)
                        })
                    }
                    _ => Err(simd_json::Error::generic(
                        simd_json::ErrorType::ExpectedInteger,
                    )),
                }
            }
        }
    };
}

itoa!(i8);
itoa!(u8);
itoa!(i16);
itoa!(u16);
itoa!(i32);
itoa!(u32);
itoa!(i64);
itoa!(u64);
itoa!(usize);
itoa!(i128);
itoa!(u128);

macro_rules! ryu {
    ($t:ty) => {
        impl Serialize for $t {
            #[inline]
            fn json_write<W>(&self, writer: &mut W) -> io::Result<()>
            where
                W: Write,
            {
                let mut buffer = ryu::Buffer::new();
                let s = buffer.format_finite(*self);
                writer.write_all(s.as_bytes())
            }
        }
    };
}
ryu!(f64);
ryu!(f32);

impl<'input> Deserialize<'input> for f64 {
    #[inline]
    fn from_tape(tape: &mut Tape<'input>) -> simd_json::Result<Self>
    where
        Self: std::marker::Sized + 'input,
    {
        match tape.next() {
            Some(simd_json::Node::Static(simd_json::StaticNode::F64(i))) => Ok(i),
            Some(simd_json::Node::Static(simd_json::StaticNode::I64(i))) => Ok(i as f64),
            Some(simd_json::Node::Static(simd_json::StaticNode::U64(i))) => Ok(i as f64),
            #[cfg(feature = "128bit")]
            Some(simd_json::Node::Static(simd_json::StaticNode::U128(i))) => Ok(i as f64),
            #[cfg(feature = "128bit")]
            Some(simd_json::Node::Static(simd_json::StaticNode::UI28(i))) => Ok(i as f64),
            _ => Err(simd_json::Error::generic(
                simd_json::ErrorType::ExpectedFloat,
            )),
        }
    }
}

impl<'input> Deserialize<'input> for f32 {
    #[inline]
    fn from_tape(tape: &mut Tape<'input>) -> simd_json::Result<Self>
    where
        Self: std::marker::Sized + 'input,
    {
        match tape.next() {
            Some(simd_json::Node::Static(simd_json::StaticNode::F64(i))) => Ok(i as f32),
            Some(simd_json::Node::Static(simd_json::StaticNode::I64(i))) => Ok(i as f32),
            Some(simd_json::Node::Static(simd_json::StaticNode::U64(i))) => Ok(i as f32),
            #[cfg(feature = "128bit")]
            Some(simd_json::Node::Static(simd_json::StaticNode::U128(i))) => Ok(i as f32),
            #[cfg(feature = "128bit")]
            Some(simd_json::Node::Static(simd_json::StaticNode::UI28(i))) => Ok(i as f32),
            _ => Err(simd_json::Error::generic(
                simd_json::ErrorType::ExpectedString,
            )),
        }
    }
}