Skip to content

Commit ccb8fbd

Browse files
authored
Merge pull request #59 from CosmWasm/54-serialize-128bit-numbers
Serialize / Deserialize u128 and i128 as numbers instead of strings
2 parents be32160 + 73bd5ec commit ccb8fbd

File tree

4 files changed

+61
-95
lines changed

4 files changed

+61
-95
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- Serialize / deserialize `u128`/`i128` types as numbers instead of strings
13+
([#59]).<br/> **Please note:** this breaks deserialization of `u128`/`i128`
14+
serialized with older versions of `serde-json-wasm`.
15+
16+
[#59]: https://github.com/CosmWasm/serde-json-wasm/pull/59
17+
1018
## [0.5.1] - 2023-04-11
1119

1220
### Added

src/de/map.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,7 @@ impl<'de, 'a> de::Deserializer<'de> for MapKey<'a, 'de> {
160160
where
161161
V: Visitor<'de>,
162162
{
163-
// default implementation includes string unparsing
164-
self.de.deserialize_i128(visitor)
163+
deserialize_signed_key!(self, visitor, i128, visit_i128)
165164
}
166165

167166
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, Self::Error>
@@ -196,7 +195,7 @@ impl<'de, 'a> de::Deserializer<'de> for MapKey<'a, 'de> {
196195
where
197196
V: Visitor<'de>,
198197
{
199-
self.de.deserialize_u128(visitor)
198+
deserialize_unsigned_key!(self, visitor, u128, visit_u128)
200199
}
201200

202201
fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value, Self::Error>

src/de/mod.rs

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -358,25 +358,7 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
358358
where
359359
V: Visitor<'de>,
360360
{
361-
match self.parse_whitespace().ok_or(Error::EofWhileParsingValue)? {
362-
b'"' => self.eat_char(),
363-
_ => return Err(Error::InvalidType),
364-
};
365-
366-
let result = match self.peek() {
367-
// after rust merged or-patterns feature, these two clause can be merged.
368-
// error[E0658]: or-patterns syntax is experimental
369-
Some(b'0'..=b'9') => deserialize_signed!(self, visitor, i128, visit_i128),
370-
Some(b'-') => deserialize_signed!(self, visitor, i128, visit_i128),
371-
_ => return Err(Error::InvalidType),
372-
};
373-
match self.peek() {
374-
Some(b'"') => {
375-
self.eat_char();
376-
result
377-
}
378-
_ => Err(Error::InvalidType),
379-
}
361+
deserialize_signed!(self, visitor, i128, visit_i128)
380362
}
381363

382364
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value>
@@ -411,25 +393,7 @@ impl<'a, 'de> de::Deserializer<'de> for &'a mut Deserializer<'de> {
411393
where
412394
V: Visitor<'de>,
413395
{
414-
match self.parse_whitespace().ok_or(Error::EofWhileParsingValue)? {
415-
b'"' => {
416-
self.eat_char();
417-
}
418-
_ => return Err(Error::InvalidType),
419-
};
420-
421-
let result = match self.peek() {
422-
Some(b'-') => return Err(Error::InvalidNumber),
423-
Some(b'0'..=b'9') => deserialize_unsigned!(self, visitor, u128, visit_u128),
424-
_ => return Err(Error::InvalidType),
425-
};
426-
match self.peek() {
427-
Some(b'"') => {
428-
self.eat_char();
429-
result
430-
}
431-
_ => Err(Error::InvalidType),
432-
}
396+
deserialize_unsigned!(self, visitor, u128, visit_u128)
433397
}
434398

435399
fn deserialize_f32<V>(self, _visitor: V) -> Result<V::Value>
@@ -755,43 +719,49 @@ mod tests {
755719

756720
#[test]
757721
fn integer128() {
758-
assert_eq!(from_str::<i128>(r#"0"#), Err(crate::de::Error::InvalidType));
759-
assert_eq!(from_str::<i128>(r#""0""#), Ok(0));
760-
assert_eq!(from_str::<i128>(r#""1""#), Ok(1));
761-
assert_eq!(from_str::<i128>(r#""-1""#), Ok(-1));
722+
assert_eq!(
723+
from_str::<i128>(r#""0""#),
724+
Err(crate::de::Error::InvalidType)
725+
);
726+
assert_eq!(from_str::<i128>(r#"0"#), Ok(0));
727+
assert_eq!(from_str::<i128>(r#"1"#), Ok(1));
728+
assert_eq!(from_str::<i128>(r#"-1"#), Ok(-1));
762729
// max i128
763730
assert_eq!(
764-
from_str::<i128>(r#""170141183460469231731687303715884105727""#),
731+
from_str::<i128>(r#"170141183460469231731687303715884105727"#),
765732
Ok(170141183460469231731687303715884105727)
766733
);
767734
assert_eq!(
768-
from_str::<i128>(r#""170141183460469231731687303715884105728""#),
735+
from_str::<i128>(r#"170141183460469231731687303715884105728"#),
769736
Err(crate::de::Error::InvalidNumber)
770737
);
771738
// min i128
772739
assert_eq!(
773-
from_str::<i128>(r#""-170141183460469231731687303715884105728""#),
740+
from_str::<i128>(r#"-170141183460469231731687303715884105728"#),
774741
Ok(-170141183460469231731687303715884105728)
775742
);
776743
assert_eq!(
777-
from_str::<i128>(r#""-170141183460469231731687303715884105729""#),
744+
from_str::<i128>(r#"-170141183460469231731687303715884105729"#),
778745
Err(crate::de::Error::InvalidNumber)
779746
);
780747

781-
assert_eq!(from_str::<u128>(r#"0"#), Err(crate::de::Error::InvalidType));
782-
assert_eq!(from_str::<u128>(r#""0""#), Ok(0));
783-
assert_eq!(from_str::<u128>(r#""1""#), Ok(1));
784748
assert_eq!(
785-
from_str::<u128>(r#""-1""#),
749+
from_str::<u128>(r#""0""#),
750+
Err(crate::de::Error::InvalidType)
751+
);
752+
assert_eq!(from_str::<u128>(r#"0"#), Ok(0));
753+
assert_eq!(from_str::<u128>(r#"1"#), Ok(1));
754+
assert_eq!(
755+
from_str::<u128>(r#"-1"#),
786756
Err(crate::de::Error::InvalidNumber)
787757
);
788758
// max u128
789759
assert_eq!(
790-
from_str::<u128>(r#""340282366920938463463374607431768211455""#),
760+
from_str::<u128>(r#"340282366920938463463374607431768211455"#),
791761
Ok(340282366920938463463374607431768211455)
792762
);
793763
assert_eq!(
794-
from_str::<u128>(r#""340282366920938463463374607431768211456""#),
764+
from_str::<u128>(r#"340282366920938463463374607431768211456"#),
795765
Err(crate::de::Error::InvalidNumber)
796766
)
797767
}

src/ser/mod.rs

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ impl<'a> ser::Serializer for &'a mut Serializer {
194194

195195
fn serialize_i128(self, v: i128) -> Result<Self::Ok> {
196196
// -170141183460469231731687303715884105728
197-
self.buf.push(b'"');
198-
let res: Result<Self::Ok> = serialize_signed!(self, 40, v, i128, u128);
199-
res?;
200-
self.buf.push(b'"');
201-
Ok(())
197+
serialize_signed!(self, 40, v, i128, u128)
202198
}
203199

204200
fn serialize_u8(self, v: u8) -> Result<Self::Ok> {
@@ -223,11 +219,7 @@ impl<'a> ser::Serializer for &'a mut Serializer {
223219

224220
fn serialize_u128(self, v: u128) -> Result<Self::Ok> {
225221
// 340282366920938463463374607431768211455
226-
self.buf.push(b'"');
227-
let res: Result<Self::Ok> = serialize_unsigned!(self, 39, v);
228-
res?;
229-
self.buf.push(b'"');
230-
Ok(())
222+
serialize_unsigned!(self, 39, v)
231223
}
232224

233225
fn serialize_f32(self, _v: f32) -> Result<Self::Ok> {
@@ -632,66 +624,64 @@ mod tests {
632624
"-9223372036854775808"
633625
);
634626

635-
assert_eq!(to_string::<u128>(&0).unwrap(), r#""0""#);
636-
assert_eq!(to_string::<u128>(&1).unwrap(), r#""1""#);
637-
assert_eq!(to_string::<u128>(&456789).unwrap(), r#""456789""#);
638-
assert_eq!(to_string::<u128>(&4294967295).unwrap(), r#""4294967295""#);
639-
assert_eq!(to_string::<u128>(&4294967296).unwrap(), r#""4294967296""#);
627+
assert_eq!(to_string::<u128>(&0).unwrap(), r#"0"#);
628+
assert_eq!(to_string::<u128>(&1).unwrap(), r#"1"#);
629+
assert_eq!(to_string::<u128>(&456789).unwrap(), r#"456789"#);
630+
assert_eq!(to_string::<u128>(&4294967295).unwrap(), r#"4294967295"#);
631+
assert_eq!(to_string::<u128>(&4294967296).unwrap(), r#"4294967296"#);
640632
assert_eq!(
641633
to_string::<u128>(&9007199254740991).unwrap(),
642-
r#""9007199254740991""#
634+
r#"9007199254740991"#
643635
); // Number.MAX_SAFE_INTEGER
644636
assert_eq!(
645637
to_string::<u128>(&9007199254740992).unwrap(),
646-
r#""9007199254740992""#
638+
r#"9007199254740992"#
647639
); // Number.MAX_SAFE_INTEGER+1
648640
assert_eq!(
649641
to_string::<u128>(&9223372036854775807).unwrap(),
650-
r#""9223372036854775807""#
642+
r#"9223372036854775807"#
651643
);
652644
assert_eq!(
653645
to_string::<u128>(&9223372036854775808).unwrap(),
654-
r#""9223372036854775808""#
646+
r#"9223372036854775808"#
655647
);
656648
assert_eq!(
657649
to_string::<u128>(&u128::MAX).unwrap(),
658-
r#""340282366920938463463374607431768211455""#
650+
r#"340282366920938463463374607431768211455"#
659651
);
660-
// Currently failing, see https://github.com/CosmWasm/serde-json-wasm/issues/54
661-
// assert_serde_json_serialize_eq!(&u128::MAX);
662-
663-
assert_eq!(to_string::<i128>(&0).unwrap(), r#""0""#);
664-
assert_eq!(to_string::<i128>(&1).unwrap(), r#""1""#);
665-
assert_eq!(to_string::<i128>(&456789).unwrap(), r#""456789""#);
666-
assert_eq!(to_string::<i128>(&4294967295).unwrap(), r#""4294967295""#);
667-
assert_eq!(to_string::<i128>(&4294967296).unwrap(), r#""4294967296""#);
652+
assert_serde_json_serialize_eq!(&u128::MAX);
653+
654+
assert_eq!(to_string::<i128>(&0).unwrap(), r#"0"#);
655+
assert_eq!(to_string::<i128>(&1).unwrap(), r#"1"#);
656+
assert_eq!(to_string::<i128>(&456789).unwrap(), r#"456789"#);
657+
assert_eq!(to_string::<i128>(&4294967295).unwrap(), r#"4294967295"#);
658+
assert_eq!(to_string::<i128>(&4294967296).unwrap(), r#"4294967296"#);
668659
assert_eq!(
669660
to_string::<i128>(&9007199254740991).unwrap(),
670-
r#""9007199254740991""#
661+
r#"9007199254740991"#
671662
); // Number.MAX_SAFE_INTEGER
672663
assert_eq!(
673664
to_string::<i128>(&9007199254740992).unwrap(),
674-
r#""9007199254740992""#
665+
r#"9007199254740992"#
675666
); // Number.MAX_SAFE_INTEGER+1
676667
assert_eq!(
677668
to_string::<i128>(&9223372036854775807).unwrap(),
678-
r#""9223372036854775807""#
669+
r#"9223372036854775807"#
679670
);
680671
assert_eq!(
681672
to_string::<i128>(&9223372036854775808).unwrap(),
682-
r#""9223372036854775808""#
673+
r#"9223372036854775808"#
683674
);
684675
assert_eq!(
685676
to_string::<i128>(&i128::MAX).unwrap(),
686-
r#""170141183460469231731687303715884105727""#
677+
r#"170141183460469231731687303715884105727"#
687678
);
688-
assert_eq!(to_string::<i128>(&-1).unwrap(), r#""-1""#);
679+
assert_eq!(to_string::<i128>(&-1).unwrap(), r#"-1"#);
689680
assert_eq!(
690681
to_string::<i128>(&i128::MIN).unwrap(),
691-
r#""-170141183460469231731687303715884105728""#
682+
r#"-170141183460469231731687303715884105728"#
692683
);
693-
// Currently failing, see https://github.com/CosmWasm/serde-json-wasm/issues/54
694-
// assert_serde_json_serialize_eq!(&i128::MIN);
684+
assert_serde_json_serialize_eq!(&i128::MIN);
695685
}
696686

697687
#[test]
@@ -725,10 +715,9 @@ mod tests {
725715

726716
assert_eq!(
727717
to_string(&pair).unwrap(),
728-
r#"["340282366920938463463374607431768211455","340282366920938463463374607431768211455"]"#
718+
r#"[340282366920938463463374607431768211455,340282366920938463463374607431768211455]"#
729719
);
730-
// Currently failing, see https://github.com/CosmWasm/serde-json-wasm/issues/54
731-
// assert_serde_json_serialize_eq!(&pair);
720+
assert_serde_json_serialize_eq!(&pair);
732721
}
733722

734723
#[test]

0 commit comments

Comments
 (0)