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

support stringify BigInt #587

Merged
merged 2 commits into from
Sep 13, 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
7 changes: 7 additions & 0 deletions llrt_core/src/json/stringify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,13 @@ fn write_primitive(context: &mut StringifyContext, add_comma: bool) -> Result<Pr
return Ok(PrimitiveStatus::Ignored);
}

if matches!(type_of, Type::BigInt) {
return Err(Exception::throw_type(
context.ctx,
"Do not know how to serialize a BigInt",
));
}

if let Some(include_keys_replacer) = include_keys_replacer {
let key = get_key_or_index(context.itoa_buffer, key, index);
if !include_keys_replacer.contains(key) {
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,12 @@ describe("JSON Stringified", () => {
const exception = new Error("error");
expect(JSON.stringify(exception)).toEqual("{}");
});

it("should throw an Error when stringify BigInt", () => {
expect(() =>
JSON.stringify({
v: 1n,
})
).toThrow(/Do not know how to serialize a BigInt/);
});
});
Loading