1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-25 16:49:18 -05:00

feat(serde_v8): Serialize integers as BigInt (#15692)

This commit is contained in:
Jakub Łabor 2022-09-01 12:51:28 +02:00 committed by GitHub
parent 0abf5a412b
commit 37b32fa348
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 4 deletions

View file

@ -377,6 +377,9 @@ macro_rules! forward_to {
};
}
const MAX_SAFE_INTEGER: i64 = (1 << 53) - 1;
const MIN_SAFE_INTEGER: i64 = -MAX_SAFE_INTEGER;
impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
type Ok = v8::Local<'a, v8::Value>;
type Error = Error;
@ -399,8 +402,6 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
serialize_u16(u16, serialize_u32, 'a);
serialize_f32(f32, serialize_f64, 'a);
serialize_u64(u64, serialize_f64, 'a);
serialize_i64(i64, serialize_f64, 'a);
}
fn serialize_i32(self, v: i32) -> JsResult<'a> {
@ -411,6 +412,28 @@ impl<'a, 'b, 'c> ser::Serializer for Serializer<'a, 'b, 'c> {
Ok(v8::Integer::new_from_unsigned(&mut self.scope.borrow_mut(), v).into())
}
fn serialize_i64(self, v: i64) -> JsResult<'a> {
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 (MIN_SAFE_INTEGER..=MAX_SAFE_INTEGER).contains(&v) {
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> {
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> {
Ok(v8::Number::new(&mut self.scope.borrow_mut(), v).into())
}

View file

@ -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])");
@ -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, {})");