use alloc::string::{String, ToString};
use alloc::vec::Vec;
use serde::ser;
use self::map::SerializeMap;
use self::seq::SerializeSeq;
use self::struct_::SerializeStruct;
mod map;
mod seq;
mod struct_;
pub type Result<T> = ::core::result::Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
BufferFull,
Custom(String),
}
impl From<()> for Error {
fn from(_: ()) -> Error {
Error::BufferFull
}
}
impl From<u8> for Error {
fn from(_: u8) -> Error {
Error::BufferFull
}
}
impl ser::StdError for Error {}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::BufferFull => write!(f, "Buffer is full"),
Error::Custom(msg) => write!(f, "{}", &msg),
}
}
}
pub struct Serializer {
buf: Vec<u8>,
}
static INITIAL_CAPACITY: usize = 1024;
impl Serializer {
fn new() -> Self {
Serializer {
buf: Vec::with_capacity(INITIAL_CAPACITY),
}
}
}
macro_rules! serialize_unsigned {
($self:ident, $N:expr, $v:expr) => {{
let mut buf = [0u8; $N];
let mut v = $v;
let mut i = $N - 1;
loop {
buf[i] = (v % 10) as u8 + b'0';
v /= 10;
if v == 0 {
break;
} else {
i -= 1;
}
}
$self.buf.extend_from_slice(&buf[i..]);
Ok(())
}};
}
pub(crate) use serialize_unsigned;
macro_rules! serialize_signed {
($self:ident, $N:expr, $v:expr, $ixx:ident, $uxx:ident) => {{
let v = $v;
let (signed, mut v) = if v == $ixx::min_value() {
(true, $ixx::max_value() as $uxx + 1)
} else if v < 0 {
(true, -v as $uxx)
} else {
(false, v as $uxx)
};
let mut buf = [0u8; $N];
let mut i = $N - 1;
loop {
buf[i] = (v % 10) as u8 + b'0';
v /= 10;
i -= 1;
if v == 0 {
break;
}
}
if signed {
buf[i] = b'-';
} else {
i += 1;
}
$self.buf.extend_from_slice(&buf[i..]);
Ok(())
}};
}
pub(crate) use serialize_signed;
fn hex_4bit(c: u8) -> u8 {
if c <= 9 {
0x30 + c
} else {
0x41 + (c - 10)
}
}
fn hex(c: u8) -> (u8, u8) {
(hex_4bit(c >> 4), hex_4bit(c & 0x0F))
}
impl<'a> ser::Serializer for &'a mut Serializer {
type Ok = ();
type Error = Error;
type SerializeSeq = SerializeSeq<'a>;
type SerializeTuple = SerializeSeq<'a>;
type SerializeTupleStruct = Unreachable;
type SerializeTupleVariant = SerializeSeq<'a>;
type SerializeMap = SerializeMap<'a>;
type SerializeStruct = SerializeStruct<'a>;
type SerializeStructVariant = SerializeStruct<'a>;
fn serialize_bool(self, v: bool) -> Result<Self::Ok> {
if v {
self.buf.extend_from_slice(b"true");
} else {
self.buf.extend_from_slice(b"false");
}
Ok(())
}
fn serialize_i8(self, v: i8) -> Result<Self::Ok> {
serialize_signed!(self, 4, v, i8, u8)
}
fn serialize_i16(self, v: i16) -> Result<Self::Ok> {
serialize_signed!(self, 6, v, i16, u16)
}
fn serialize_i32(self, v: i32) -> Result<Self::Ok> {
serialize_signed!(self, 11, v, i32, u32)
}
fn serialize_i64(self, v: i64) -> Result<Self::Ok> {
serialize_signed!(self, 20, v, i64, u64)
}
fn serialize_i128(self, v: i128) -> Result<Self::Ok> {
serialize_signed!(self, 40, v, i128, u128)
}
fn serialize_u8(self, v: u8) -> Result<Self::Ok> {
serialize_unsigned!(self, 3, v)
}
fn serialize_u16(self, v: u16) -> Result<Self::Ok> {
serialize_unsigned!(self, 5, v)
}
fn serialize_u32(self, v: u32) -> Result<Self::Ok> {
serialize_unsigned!(self, 10, v)
}
fn serialize_u64(self, v: u64) -> Result<Self::Ok> {
serialize_unsigned!(self, 20, v)
}
fn serialize_u128(self, v: u128) -> Result<Self::Ok> {
serialize_unsigned!(self, 39, v)
}
fn serialize_f32(self, _v: f32) -> Result<Self::Ok> {
unreachable!()
}
fn serialize_f64(self, _v: f64) -> Result<Self::Ok> {
unreachable!()
}
fn serialize_char(self, _v: char) -> Result<Self::Ok> {
unreachable!()
}
fn serialize_str(self, v: &str) -> Result<Self::Ok> {
self.buf.push(b'"');
let mut encoding_tmp = [0u8; 4];
for c in v.chars() {
match c {
'\\' => {
self.buf.push(b'\\');
self.buf.push(b'\\');
}
'"' => {
self.buf.push(b'\\');
self.buf.push(b'"');
}
'\u{0008}' => {
self.buf.push(b'\\');
self.buf.push(b'b');
}
'\u{0009}' => {
self.buf.push(b'\\');
self.buf.push(b't');
}
'\u{000A}' => {
self.buf.push(b'\\');
self.buf.push(b'n');
}
'\u{000C}' => {
self.buf.push(b'\\');
self.buf.push(b'f');
}
'\u{000D}' => {
self.buf.push(b'\\');
self.buf.push(b'r');
}
'\u{0000}'..='\u{001F}' => {
self.buf.push(b'\\');
self.buf.push(b'u');
self.buf.push(b'0');
self.buf.push(b'0');
let (hex1, hex2) = hex(c as u8);
self.buf.push(hex1);
self.buf.push(hex2);
}
_ => {
if c.len_utf8() == 1 {
self.buf.push(c as u8);
} else {
let encoded = c.encode_utf8(&mut encoding_tmp as &mut [u8]);
self.buf.extend_from_slice(encoded.as_bytes());
}
}
}
}
self.buf.push(b'"');
Ok(())
}
fn serialize_bytes(self, _v: &[u8]) -> Result<Self::Ok> {
unreachable!()
}
fn serialize_none(self) -> Result<Self::Ok> {
self.buf.extend_from_slice(b"null");
Ok(())
}
fn serialize_some<T: ?Sized>(self, value: &T) -> Result<Self::Ok>
where
T: ser::Serialize,
{
value.serialize(self)
}
fn serialize_unit(self) -> Result<Self::Ok> {
self.buf.extend_from_slice(b"null");
Ok(())
}
fn serialize_unit_struct(self, _name: &'static str) -> Result<Self::Ok> {
self.buf.extend_from_slice(b"null");
Ok(())
}
fn serialize_unit_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
) -> Result<Self::Ok> {
self.serialize_str(variant)
}
fn serialize_newtype_struct<T: ?Sized>(self, _name: &'static str, value: &T) -> Result<Self::Ok>
where
T: ser::Serialize,
{
value.serialize(&mut *self)
}
fn serialize_newtype_variant<T: ?Sized>(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
value: &T,
) -> Result<Self::Ok>
where
T: ser::Serialize,
{
self.buf.push(b'{');
self.serialize_str(variant)?;
self.buf.push(b':');
value.serialize(&mut *self)?;
self.buf.push(b'}');
Ok(())
}
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq> {
self.buf.push(b'[');
Ok(SerializeSeq::new(self))
}
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple> {
self.serialize_seq(Some(_len))
}
fn serialize_tuple_struct(
self,
_name: &'static str,
_len: usize,
) -> Result<Self::SerializeTupleStruct> {
unreachable!()
}
fn serialize_tuple_variant(
self,
_name: &'static str,
_variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeTupleVariant> {
self.buf.push(b'{');
self.serialize_str(variant)?;
self.buf.push(b':');
self.serialize_tuple(len)
}
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap> {
self.buf.push(b'{');
Ok(SerializeMap::new(self))
}
fn serialize_struct(self, _name: &'static str, _len: usize) -> Result<Self::SerializeStruct> {
self.buf.push(b'{');
Ok(SerializeStruct::new(self))
}
fn serialize_struct_variant(
self,
name: &'static str,
_variant_index: u32,
variant: &'static str,
len: usize,
) -> Result<Self::SerializeStructVariant> {
self.buf.push(b'{');
self.serialize_str(variant)?;
self.buf.push(b':');
self.serialize_struct(name, len)
}
}
pub fn to_string<T>(value: &T) -> Result<String>
where
T: ser::Serialize + ?Sized,
{
let mut ser = Serializer::new();
value.serialize(&mut ser)?;
Ok(unsafe { String::from_utf8_unchecked(ser.buf) })
}
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>>
where
T: ser::Serialize + ?Sized,
{
let mut ser = Serializer::new();
value.serialize(&mut ser)?;
Ok(ser.buf)
}
impl ser::Error for Error {
fn custom<T: core::fmt::Display>(msg: T) -> Self {
Error::Custom(msg.to_string())
}
}
pub enum Unreachable {}
impl ser::SerializeTupleStruct for Unreachable {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, _value: &T) -> Result<()> {
unreachable!()
}
fn end(self) -> Result<Self::Ok> {
unreachable!()
}
}
impl ser::SerializeTupleVariant for Unreachable {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, _value: &T) -> Result<()> {
unreachable!()
}
fn end(self) -> Result<Self::Ok> {
unreachable!()
}
}
impl ser::SerializeMap for Unreachable {
type Ok = ();
type Error = Error;
fn serialize_key<T: ?Sized>(&mut self, _key: &T) -> Result<()>
where
T: ser::Serialize,
{
unreachable!()
}
fn serialize_value<T: ?Sized>(&mut self, _value: &T) -> Result<()>
where
T: ser::Serialize,
{
unreachable!()
}
fn end(self) -> Result<Self::Ok> {
unreachable!()
}
}
impl ser::SerializeStructVariant for Unreachable {
type Ok = ();
type Error = Error;
fn serialize_field<T: ?Sized>(&mut self, _key: &'static str, _value: &T) -> Result<()>
where
T: ser::Serialize,
{
unreachable!()
}
fn end(self) -> Result<Self::Ok> {
unreachable!()
}
}
#[cfg(test)]
mod tests {
use super::to_string;
use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use std::collections::HashMap;
use serde::{Serialize, Serializer};
use serde_derive::{Deserialize, Serialize};
#[macro_export]
macro_rules! assert_serde_json_serialize_eq {
($target:expr,) => {
assert_serde_json_serialize_eq!($target);
};
($target:expr) => {
assert_eq!(
crate::to_string($target).unwrap(),
::serde_json::to_string($target).unwrap(),
"Serialization does not match serde_json"
);
};
}
#[test]
fn bool() {
assert_eq!(to_string(&true).unwrap(), "true");
assert_eq!(to_string(&false).unwrap(), "false");
}
#[test]
fn number() {
assert_eq!(to_string::<u8>(&0).unwrap(), "0");
assert_eq!(to_string::<u8>(&1).unwrap(), "1");
assert_eq!(to_string::<u8>(&u8::MAX).unwrap(), "255");
assert_eq!(to_string::<i8>(&0).unwrap(), "0");
assert_eq!(to_string::<i8>(&1).unwrap(), "1");
assert_eq!(to_string::<i8>(&127).unwrap(), "127");
assert_eq!(to_string::<i8>(&-1).unwrap(), "-1");
assert_eq!(to_string::<i8>(&i8::MIN).unwrap(), "-128");
assert_eq!(to_string::<u16>(&0).unwrap(), "0");
assert_eq!(to_string::<u16>(&1).unwrap(), "1");
assert_eq!(to_string::<u16>(&550).unwrap(), "550");
assert_eq!(to_string::<u16>(&u16::MAX).unwrap(), "65535");
assert_eq!(to_string::<i16>(&0).unwrap(), "0");
assert_eq!(to_string::<i16>(&1).unwrap(), "1");
assert_eq!(to_string::<i16>(&550).unwrap(), "550");
assert_eq!(to_string::<i16>(&i16::MAX).unwrap(), "32767");
assert_eq!(to_string::<i16>(&-1).unwrap(), "-1");
assert_eq!(to_string::<i16>(&i16::MIN).unwrap(), "-32768");
assert_eq!(to_string::<u32>(&0).unwrap(), "0");
assert_eq!(to_string::<u32>(&1).unwrap(), "1");
assert_eq!(to_string::<u32>(&456789).unwrap(), "456789");
assert_eq!(to_string::<u32>(&u32::MAX).unwrap(), "4294967295");
assert_eq!(to_string::<i32>(&0).unwrap(), "0");
assert_eq!(to_string::<i32>(&1).unwrap(), "1");
assert_eq!(to_string::<i32>(&456789).unwrap(), "456789");
assert_eq!(to_string::<i32>(&i32::MAX).unwrap(), "2147483647");
assert_eq!(to_string::<i32>(&-1).unwrap(), "-1");
assert_eq!(to_string::<i32>(&i32::MIN).unwrap(), "-2147483648");
assert_eq!(to_string::<u64>(&0).unwrap(), "0");
assert_eq!(to_string::<u64>(&1).unwrap(), "1");
assert_eq!(to_string::<u64>(&456789).unwrap(), "456789");
assert_eq!(to_string::<u64>(&4294967295).unwrap(), "4294967295");
assert_eq!(to_string::<u64>(&4294967296).unwrap(), "4294967296");
assert_eq!(
to_string::<u64>(&9007199254740991).unwrap(),
"9007199254740991"
); assert_eq!(
to_string::<u64>(&9007199254740992).unwrap(),
"9007199254740992"
); assert_eq!(to_string::<u64>(&u64::MAX).unwrap(), "18446744073709551615");
assert_eq!(to_string::<i64>(&0).unwrap(), "0");
assert_eq!(to_string::<i64>(&1).unwrap(), "1");
assert_eq!(to_string::<i64>(&456789).unwrap(), "456789");
assert_eq!(to_string::<i64>(&4294967295).unwrap(), "4294967295");
assert_eq!(to_string::<i64>(&4294967296).unwrap(), "4294967296");
assert_eq!(
to_string::<i64>(&9007199254740991).unwrap(),
"9007199254740991"
); assert_eq!(
to_string::<i64>(&9007199254740992).unwrap(),
"9007199254740992"
); assert_eq!(to_string::<i64>(&i64::MAX).unwrap(), "9223372036854775807");
assert_eq!(to_string::<i64>(&-1).unwrap(), "-1");
assert_eq!(to_string::<i64>(&i64::MIN).unwrap(), "-9223372036854775808");
assert_eq!(to_string::<u128>(&0).unwrap(), r#"0"#);
assert_eq!(to_string::<u128>(&1).unwrap(), r#"1"#);
assert_eq!(to_string::<u128>(&456789).unwrap(), r#"456789"#);
assert_eq!(to_string::<u128>(&4294967295).unwrap(), r#"4294967295"#);
assert_eq!(to_string::<u128>(&4294967296).unwrap(), r#"4294967296"#);
assert_eq!(
to_string::<u128>(&9007199254740991).unwrap(),
r#"9007199254740991"#
); assert_eq!(
to_string::<u128>(&9007199254740992).unwrap(),
r#"9007199254740992"#
); assert_eq!(
to_string::<u128>(&9223372036854775807).unwrap(),
r#"9223372036854775807"#
);
assert_eq!(
to_string::<u128>(&9223372036854775808).unwrap(),
r#"9223372036854775808"#
);
assert_eq!(
to_string::<u128>(&u128::MAX).unwrap(),
r#"340282366920938463463374607431768211455"#
);
assert_serde_json_serialize_eq!(&u128::MAX);
assert_eq!(to_string::<i128>(&0).unwrap(), r#"0"#);
assert_eq!(to_string::<i128>(&1).unwrap(), r#"1"#);
assert_eq!(to_string::<i128>(&456789).unwrap(), r#"456789"#);
assert_eq!(to_string::<i128>(&4294967295).unwrap(), r#"4294967295"#);
assert_eq!(to_string::<i128>(&4294967296).unwrap(), r#"4294967296"#);
assert_eq!(
to_string::<i128>(&9007199254740991).unwrap(),
r#"9007199254740991"#
); assert_eq!(
to_string::<i128>(&9007199254740992).unwrap(),
r#"9007199254740992"#
); assert_eq!(
to_string::<i128>(&9223372036854775807).unwrap(),
r#"9223372036854775807"#
);
assert_eq!(
to_string::<i128>(&9223372036854775808).unwrap(),
r#"9223372036854775808"#
);
assert_eq!(
to_string::<i128>(&i128::MAX).unwrap(),
r#"170141183460469231731687303715884105727"#
);
assert_eq!(to_string::<i128>(&-1).unwrap(), r#"-1"#);
assert_eq!(
to_string::<i128>(&i128::MIN).unwrap(),
r#"-170141183460469231731687303715884105728"#
);
assert_serde_json_serialize_eq!(&i128::MIN);
}
#[test]
fn array() {
assert_eq!(to_string::<[u8]>(&[]).unwrap(), "[]");
assert_eq!(to_string(&[0, 1, 2]).unwrap(), "[0,1,2]");
}
#[test]
fn tuple() {
type Pair = (i64, i64);
type Wrapped = (i64,); type Unit = ();
let pair: Pair = (1, 2);
assert_eq!(to_string(&pair).unwrap(), "[1,2]");
assert_serde_json_serialize_eq!(&pair);
let wrapped: Wrapped = (5,);
assert_eq!(to_string(&wrapped).unwrap(), "[5]");
assert_serde_json_serialize_eq!(&wrapped);
#[allow(clippy::let_unit_value)]
let unit: Unit = ();
assert_eq!(to_string(&unit).unwrap(), "null");
assert_serde_json_serialize_eq!(&unit);
type BigPair = (u128, u128);
let pair: BigPair = (u128::MAX, u128::MAX);
assert_eq!(
to_string(&pair).unwrap(),
r#"[340282366920938463463374607431768211455,340282366920938463463374607431768211455]"#
);
assert_serde_json_serialize_eq!(&pair);
}
#[test]
fn enum_variants_unit_like() {
#[allow(dead_code)]
#[derive(Serialize)]
enum Op {
Enter,
Exit,
}
assert_eq!(to_string(&Op::Exit).unwrap(), r#""Exit""#);
assert_serde_json_serialize_eq!(&Op::Exit);
#[derive(Serialize)]
enum Order {
Unordered = 1,
Ordered = 42,
}
assert_eq!(to_string(&Order::Unordered).unwrap(), r#""Unordered""#);
assert_serde_json_serialize_eq!(&Order::Unordered);
assert_eq!(to_string(&Order::Ordered).unwrap(), r#""Ordered""#);
assert_serde_json_serialize_eq!(&Order::Ordered);
}
#[test]
fn enum_variants_tuple_like_structs() {
#[derive(Serialize)]
enum Op {
Exit(),
Square(i32),
Add(i64, i64),
}
assert_eq!(to_string(&Op::Exit()).unwrap(), r#"{"Exit":[]}"#);
assert_serde_json_serialize_eq!(&Op::Exit());
assert_eq!(to_string(&Op::Square(2)).unwrap(), r#"{"Square":2}"#);
assert_serde_json_serialize_eq!(&Op::Square(2));
assert_eq!(to_string(&Op::Add(3, 4)).unwrap(), r#"{"Add":[3,4]}"#);
assert_serde_json_serialize_eq!(&Op::Add(3, 4));
}
#[test]
fn enum_variants_c_like_structs() {
#[derive(Serialize)]
enum Op {
Exit {},
Square { input: i32 },
Add { a: i64, b: i64 },
}
assert_eq!(to_string(&Op::Exit {}).unwrap(), r#"{"Exit":{}}"#);
assert_serde_json_serialize_eq!(&Op::Exit {});
assert_eq!(
to_string(&Op::Square { input: 2 }).unwrap(),
r#"{"Square":{"input":2}}"#
);
assert_serde_json_serialize_eq!(&Op::Square { input: 2 });
assert_eq!(
to_string(&Op::Add { a: 3, b: 4 }).unwrap(),
r#"{"Add":{"a":3,"b":4}}"#
);
assert_serde_json_serialize_eq!(&Op::Add { a: 3, b: 4 });
}
#[test]
fn enum_mixed() {
#[derive(Serialize)]
enum Animal {
Ant,
#[serde(rename = "kitty")]
Cat,
Dog(),
Horse {},
Zebra {
height: u32,
},
}
assert_eq!(to_string(&Animal::Ant).unwrap(), r#""Ant""#);
assert_eq!(to_string(&Animal::Cat).unwrap(), r#""kitty""#);
assert_eq!(to_string(&Animal::Dog()).unwrap(), r#"{"Dog":[]}"#);
assert_eq!(to_string(&Animal::Horse {}).unwrap(), r#"{"Horse":{}}"#);
assert_eq!(
to_string(&Animal::Zebra { height: 273 }).unwrap(),
r#"{"Zebra":{"height":273}}"#
);
}
#[test]
fn str() {
assert_eq!(to_string("hello").unwrap(), r#""hello""#);
assert_eq!(to_string("").unwrap(), r#""""#);
assert_eq!(to_string("ä").unwrap(), r#""ä""#);
assert_eq!(to_string("৬").unwrap(), r#""৬""#);
assert_eq!(to_string("\u{A0}").unwrap(), r#"" ""#); assert_eq!(to_string("ℝ").unwrap(), r#""ℝ""#); assert_eq!(to_string("💣").unwrap(), r#""💣""#); assert_eq!(to_string("foo\"bar").unwrap(), r#""foo\"bar""#);
assert_eq!(to_string("foo\\bar").unwrap(), r#""foo\\bar""#);
assert_eq!(to_string(" \u{0008} ").unwrap(), r#"" \b ""#);
assert_eq!(to_string(" \u{0009} ").unwrap(), r#"" \t ""#);
assert_eq!(to_string(" \u{000A} ").unwrap(), r#"" \n ""#);
assert_eq!(to_string(" \u{000C} ").unwrap(), r#"" \f ""#);
assert_eq!(to_string(" \u{000D} ").unwrap(), r#"" \r ""#);
assert_eq!(to_string(" \u{0000} ").unwrap(), r#"" \u0000 ""#);
assert_eq!(to_string(" \u{0001} ").unwrap(), r#"" \u0001 ""#);
assert_eq!(to_string(" \u{0007} ").unwrap(), r#"" \u0007 ""#);
assert_eq!(to_string(" \u{000e} ").unwrap(), r#"" \u000E ""#);
assert_eq!(to_string(" \u{001D} ").unwrap(), r#"" \u001D ""#);
assert_eq!(to_string(" \u{001f} ").unwrap(), r#"" \u001F ""#);
}
#[test]
fn collect_str_can_be_used_in_custom_seralize_impl() {
struct SpecialType {
count: u32,
on: bool,
}
impl Serialize for SpecialType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&format_args!("{}-{}", self.count, self.on))
}
}
let value = SpecialType {
count: 123,
on: false,
};
assert_eq!(to_string(&value).unwrap(), r#""123-false""#);
}
#[test]
fn newtype() {
#[derive(Serialize)]
struct Address(String);
#[derive(Serialize)]
struct CommentId(u32);
assert_eq!(
to_string(&Address("home".to_string())).unwrap(),
r#""home""#
);
assert_serde_json_serialize_eq!(&Address("home".to_string()));
assert_eq!(to_string(&CommentId(42)).unwrap(), r#"42"#);
assert_serde_json_serialize_eq!(&CommentId(42));
}
#[test]
fn struct_bool() {
#[derive(Serialize)]
struct Led {
led: bool,
}
assert_eq!(to_string(&Led { led: true }).unwrap(), r#"{"led":true}"#);
assert_serde_json_serialize_eq!(&Led { led: true });
}
#[test]
fn struct_i8() {
#[derive(Serialize)]
struct Temperature {
temperature: i8,
}
assert_eq!(
to_string(&Temperature { temperature: 127 }).unwrap(),
r#"{"temperature":127}"#
);
assert_eq!(
to_string(&Temperature { temperature: 20 }).unwrap(),
r#"{"temperature":20}"#
);
assert_eq!(
to_string(&Temperature { temperature: -17 }).unwrap(),
r#"{"temperature":-17}"#
);
assert_eq!(
to_string(&Temperature { temperature: -128 }).unwrap(),
r#"{"temperature":-128}"#
);
assert_serde_json_serialize_eq!(&Temperature { temperature: -128 });
}
#[test]
fn struct_option() {
#[derive(Serialize)]
struct Property<'a> {
description: Option<&'a str>,
}
assert_eq!(
to_string(&Property {
description: Some("An ambient temperature sensor"),
})
.unwrap(),
r#"{"description":"An ambient temperature sensor"}"#
);
assert_serde_json_serialize_eq!(&Property {
description: Some("An ambient temperature sensor"),
});
assert_eq!(
to_string(&Property { description: None }).unwrap(),
r#"{"description":null}"#
);
assert_serde_json_serialize_eq!(&Property { description: None });
}
#[test]
fn struct_u8() {
#[derive(Serialize)]
struct Temperature {
temperature: u8,
}
assert_eq!(
to_string(&Temperature { temperature: 20 }).unwrap(),
r#"{"temperature":20}"#
);
assert_serde_json_serialize_eq!(&Temperature { temperature: 20 });
}
#[test]
fn struct_() {
#[derive(Serialize)]
struct Nothing;
assert_eq!(to_string(&Nothing).unwrap(), r#"null"#);
assert_serde_json_serialize_eq!(&Nothing);
#[derive(Serialize)]
struct Empty {}
assert_eq!(to_string(&Empty {}).unwrap(), r#"{}"#);
assert_serde_json_serialize_eq!(&Empty {});
#[derive(Serialize)]
struct Tuple {
a: bool,
b: bool,
}
assert_eq!(
to_string(&Tuple { a: true, b: false }).unwrap(),
r#"{"a":true,"b":false}"#
);
assert_serde_json_serialize_eq!(&Tuple { a: true, b: false });
}
#[test]
fn struct_with_flatten() {
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Pagination {
limit: u64,
offset: u64,
total: u64,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
struct Users {
users: Vec<String>,
#[serde(flatten)]
pagination: Pagination,
}
let users = Users {
users: vec!["joe".into(), "alice".into()],
pagination: Pagination {
offset: 100,
limit: 20,
total: 102,
},
};
assert_eq!(
to_string(&users).unwrap(),
r#"{"users":["joe","alice"],"limit":20,"offset":100,"total":102}"#
);
assert_serde_json_serialize_eq!(&users);
}
#[test]
fn btree_map() {
let empty = BTreeMap::<(), ()>::new();
assert_eq!(to_string(&empty).unwrap(), r#"{}"#);
assert_serde_json_serialize_eq!(&empty);
let mut map = BTreeMap::<&str, ()>::new();
map.insert("set_element", ());
assert_eq!(to_string(&map).unwrap(), r#"{"set_element":null}"#);
assert_serde_json_serialize_eq!(&map);
let mut two_values = BTreeMap::new();
two_values.insert("my_name", "joseph");
two_values.insert("her_name", "aline");
assert_eq!(
to_string(&two_values).unwrap(),
r#"{"her_name":"aline","my_name":"joseph"}"#
);
assert_serde_json_serialize_eq!(&two_values);
let mut nested_map = BTreeMap::new();
nested_map.insert("two_entries", two_values.clone());
two_values.remove("my_name");
nested_map.insert("one_entry", two_values);
assert_eq!(
to_string(&nested_map).unwrap(),
r#"{"one_entry":{"her_name":"aline"},"two_entries":{"her_name":"aline","my_name":"joseph"}}"#
);
assert_serde_json_serialize_eq!(&nested_map);
}
#[test]
fn hash_map() {
let empty = HashMap::<(), ()>::new();
assert_eq!(to_string(&empty).unwrap(), r#"{}"#);
assert_serde_json_serialize_eq!(&empty);
let mut map = HashMap::new();
map.insert("my_age", 28);
assert_eq!(to_string(&map).unwrap(), r#"{"my_age":28}"#);
assert_serde_json_serialize_eq!(&map);
#[derive(Debug, Serialize, PartialEq, Eq, Hash)]
pub struct NewType(String);
let mut map = HashMap::new();
map.insert(NewType(String::from("my_age")), 44);
assert_eq!(to_string(&map).unwrap(), r#"{"my_age":44}"#);
assert_serde_json_serialize_eq!(&map);
#[derive(Debug, Serialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum MyResult {
Err,
}
let mut map = HashMap::new();
map.insert(MyResult::Err, 404);
assert_eq!(to_string(&map).unwrap(), r#"{"err":404}"#);
assert_serde_json_serialize_eq!(&map);
let mut two_values = HashMap::new();
two_values.insert("my_name", "joseph");
two_values.insert("her_name", "aline");
let serialized = to_string(&two_values).unwrap();
assert!(
serialized == r#"{"her_name":"aline","my_name":"joseph"}"#
|| serialized == r#"{"my_name":"joseph","her_name":"aline"}"#
);
assert_serde_json_serialize_eq!(&two_values);
}
#[test]
fn number_key() {
let mut map = HashMap::new();
map.insert(10i8, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
let mut map = HashMap::new();
map.insert(10i16, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
let mut map = HashMap::new();
map.insert(10i32, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
let mut map = HashMap::new();
map.insert(10i64, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
let mut map = HashMap::new();
map.insert(10i128, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
let mut map = HashMap::new();
map.insert(10u8, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
let mut map = HashMap::new();
map.insert(10u16, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
let mut map = HashMap::new();
map.insert(10u32, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
let mut map = HashMap::new();
map.insert(10u64, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
let mut map = HashMap::new();
map.insert(10u128, "my_age");
assert_eq!(to_string(&map).unwrap(), r#"{"10":"my_age"}"#);
assert_serde_json_serialize_eq!(&map);
}
#[test]
fn invalid_json_key() {
use crate::ser::map::key_must_be_a_string;
#[derive(Debug, Serialize, PartialEq, Eq, Hash)]
#[serde(rename_all = "lowercase")]
pub enum MyResult {
Unit(()),
Ok(Response),
}
#[derive(Debug, Serialize, PartialEq, Eq, Hash)]
pub struct Response {
pub log: Option<String>,
pub count: i64,
pub list: Vec<u32>,
}
let mut map = HashMap::new();
map.insert(MyResult::Unit(()), "my_age");
assert_eq!(
to_string(&map).unwrap_err().to_string(),
key_must_be_a_string().to_string()
);
let mut map = HashMap::new();
map.insert(
MyResult::Ok(Response {
log: None,
count: 1,
list: vec![6],
}),
"my_age",
);
assert_eq!(
to_string(&map).unwrap_err().to_string(),
key_must_be_a_string().to_string()
);
let mut map = HashMap::new();
map.insert(
Response {
log: None,
count: 1,
list: vec![6],
},
"my_age",
);
assert_eq!(
to_string(&map).unwrap_err().to_string(),
key_must_be_a_string().to_string()
);
}
#[test]
fn serialize_embedded_enum() {
use serde_derive::Deserialize;
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum MyResult {
Unit(()),
Ok(Response),
Err(String),
}
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
pub struct Response {
pub log: Option<String>,
pub count: i64,
pub list: Vec<u32>,
}
let err_input = MyResult::Err("some error".to_string());
let json = to_string(&err_input).expect("encode err enum");
assert_eq!(json, r#"{"err":"some error"}"#);
let loaded = crate::from_str(&json).expect("re-load err enum");
assert_eq!(err_input, loaded);
assert_serde_json_serialize_eq!(&err_input);
let unit = MyResult::Unit(());
let json = to_string(&unit).expect("encode unit enum");
assert_eq!(json, r#"{"unit":null}"#);
let loaded = crate::from_str(&json).expect("re-load unit enum");
assert_eq!(unit, loaded);
assert_serde_json_serialize_eq!(&unit);
let empty_list = MyResult::Ok(Response {
log: Some("log message".to_string()),
count: 137,
list: Vec::new(),
});
let json = to_string(&empty_list).expect("encode ok enum");
assert_eq!(
json,
r#"{"ok":{"log":"log message","count":137,"list":[]}}"#
);
let loaded = crate::from_str(&json).expect("re-load ok enum");
assert_eq!(empty_list, loaded);
assert_serde_json_serialize_eq!(&empty_list);
let full_list = MyResult::Ok(Response {
log: None,
count: 137,
list: vec![18u32, 34, 12],
});
let json = to_string(&full_list).expect("encode ok enum");
assert_eq!(json, r#"{"ok":{"log":null,"count":137,"list":[18,34,12]}}"#);
let loaded = crate::from_str(&json).expect("re-load ok enum");
assert_eq!(full_list, loaded);
assert_serde_json_serialize_eq!(&full_list);
}
}