Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 128bit integer serialization #820

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions olpc-cjson/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ edition = "2018"
serde = "1"
serde_json = "1"
unicode-normalization = "0.1"

[dev-dependencies]
serde_derive = "1"
30 changes: 30 additions & 0 deletions olpc-cjson/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ impl Formatter for CanonicalFormatter {
wrapper!(write_i16, i16);
wrapper!(write_i32, i32);
wrapper!(write_i64, i64);
wrapper!(write_i128, i128);
wrapper!(write_u8, u8);
wrapper!(write_u16, u16);
wrapper!(write_u32, u32);
wrapper!(write_u64, u64);
wrapper!(write_u128, u128);

fn write_f32<W: Write + ?Sized>(&mut self, _writer: &mut W, _value: f32) -> Result<()> {
float_err!()
Expand Down Expand Up @@ -468,4 +470,32 @@ mod tests {
];
assert_eq!(expected, encoded);
}

#[test]
fn encode_u128_i128() {
#[derive(serde_derive::Serialize)]
struct Object {
u128: u128,
i128: i128,
}

let value = Object {
u128: u128::MAX,
i128: i128::MIN,
};

let mut buf = Vec::new();
let mut ser = Serializer::with_formatter(&mut buf, CanonicalFormatter::new());
value.serialize(&mut ser).unwrap();

let expected = [
123, 34, 105, 49, 50, 56, 34, 58, 45, 49, 55, 48, 49, 52, 49, 49, 56, 51, 52, 54, 48,
52, 54, 57, 50, 51, 49, 55, 51, 49, 54, 56, 55, 51, 48, 51, 55, 49, 53, 56, 56, 52, 49,
48, 53, 55, 50, 56, 44, 34, 117, 49, 50, 56, 34, 58, 51, 52, 48, 50, 56, 50, 51, 54,
54, 57, 50, 48, 57, 51, 56, 52, 54, 51, 52, 54, 51, 51, 55, 52, 54, 48, 55, 52, 51, 49,
55, 54, 56, 50, 49, 49, 52, 53, 53, 125,
];

assert_eq!(buf, expected);
}
}