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

feat(serde_v8): Serialize integers as BigInt #15692

Merged
merged 6 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Serialize as number with safe integer bounds
  • Loading branch information
Suficio committed Aug 30, 2022
commit a89a146f0881380be6a22207b644826d259e4c62
21 changes: 19 additions & 2 deletions serde_v8/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ macro_rules! forward_to {
};
}

const MAX_SAFE_INTEGER: i64 = 9007199254740991;
const MIN_SAFE_INTEGER: i64 = -9007199254740991;

impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
type Ok = v8::Local<'a, v8::Value>;
type Error = Error;
Expand Down Expand Up @@ -410,11 +413,25 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
}

fn serialize_i64(self, v: i64) -> JsResult<'a> {
Ok(v8::BigInt::new_from_i64(&mut self.scope.borrow_mut(), v).into())
let s = &mut self.scope.borrow_mut();
// If i64 can fit in max safe integer bounds then serialize as v8::Number
// otherwise serialize as v8::BigInt
if v <= MAX_SAFE_INTEGER && v >= MIN_SAFE_INTEGER {
Ok(v8::Number::new(s, v as _).into())
} else {
Ok(v8::BigInt::new_from_i64(s, v).into())
}
}

fn serialize_u64(self, v: u64) -> JsResult<'a> {
Ok(v8::BigInt::new_from_u64(&mut self.scope.borrow_mut(), v).into())
let s = &mut self.scope.borrow_mut();
// If u64 can fit in max safe integer bounds then serialize as v8::Number
// otherwise serialize as v8::BigInt
if v <= (MAX_SAFE_INTEGER as u64) {
Ok(v8::Number::new(s, v as _).into())
} else {
Ok(v8::BigInt::new_from_u64(s, v).into())
}
}

fn serialize_f64(self, v: f64) -> JsResult<'a> {
Expand Down
24 changes: 22 additions & 2 deletions serde_v8/tests/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,13 @@ sertest!(ser_option_some, Some(true), "x === true");
sertest!(ser_option_null, None as Option<bool>, "x === null");
sertest!(ser_unit_null, (), "x === null");
sertest!(ser_bool, true, "x === true");
sertest!(ser_u64, 32, "x === 32");
sertest!(ser_u64, 9007199254740991_u64, "x === 9007199254740991");
sertest!(ser_big_int, 9007199254740992_i64, "x === 9007199254740992n");
sertest!(
ser_neg_big_int,
-9007199254740992_i64,
"x === -9007199254740992n"
);
sertest!(ser_f64, 12345.0, "x === 12345.0");
sertest!(ser_string, "Hello", "x === 'Hello'");
sertest!(ser_bytes, b"\x01\x02\x03", "arrEqual(x, [1, 2, 3])");
Expand Down Expand Up @@ -145,7 +151,21 @@ sertest!(
////
sertest!(ser_json_bool, json!(true), "x === true");
sertest!(ser_json_null, json!(null), "x === null");
sertest!(ser_json_int, json!(123), "x === 123");
sertest!(
ser_json_int,
json!(9007199254740991_u64),
"x === 9007199254740991"
);
sertest!(
ser_json_big_int,
json!(9007199254740992_i64),
"x === 9007199254740992n"
);
sertest!(
ser_json_neg_big_int,
json!(-9007199254740992_i64),
"x === -9007199254740992n"
);
sertest!(ser_json_f64, json!(123.45), "x === 123.45");
sertest!(ser_json_string, json!("Hello World"), "x === 'Hello World'");
sertest!(ser_json_obj_empty, json!({}), "objEqual(x, {})");
Expand Down