Skip to content

wip: attempt to replace json.parse#3109

Draft
peter-jerry-ye wants to merge 1 commit intomainfrom
zihang/json
Draft

wip: attempt to replace json.parse#3109
peter-jerry-ye wants to merge 1 commit intomainfrom
zihang/json

Conversation

@peter-jerry-ye
Copy link
Collaborator

@peter-jerry-ye peter-jerry-ye commented Jan 5, 2026

This is an attempt to demonstrate using JSON.parse directly in MoonBit for json parsing.

The new implementation relies on Reviver has context parameter. Engines without this will fail to parse large numbers correctly, losing precision during parsing. The core-js polyfill exist though, but it would not benefit from the size shrink a lot since it would reintroduce a whole parsing mechanism.

The present implementation of MoonBit gives a lot of detailed errors, while the JSON.parse simply throws SyntaxError, with the randomly customized error messages. The new implementation introduces a new suberror case called SyntaxError which is for JS backend. It might be possible to add ensure(input : StringView) -> Unit raise ParseError which preserves the MoonBit parsing mechanism for reporting precise errors, and it will be eliminated if not used.

The new implementation discovered some test cases where MoonBit and JS behavior diverge, notably on handling some unicode strings, which we will investigate furthur.

This PR can shrink roughly 200 kB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant