Add toJSON method support for custom serialization#237
Merged
johannschopplich merged 5 commits intotoon-format:mainfrom Dec 4, 2025
Merged
Add toJSON method support for custom serialization#237johannschopplich merged 5 commits intotoon-format:mainfrom
toJSON method support for custom serialization#237johannschopplich merged 5 commits intotoon-format:mainfrom
Conversation
Collaborator
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds support for the
toJSONmethod during TOON encoding, allowing objects to customize their serialization behavior. This aligns TOON withJSON.stringify()behavior, where objects implementingtoJSON()can control how they are represented when serialized.The implementation checks for the presence of a
toJSONmethod before applying type-specific normalization, recursively normalizes the returned value, and maintains precedence over built-in type handling (Date, Array, Set, Map).Type of Change
Changes Made
toJSONmethod detection and invocation innormalizeValue()functiontoJSONis called early in normalization to take precedence over built-in type handlerstoJSONis recursively normalized to handle nestedtoJSONimplementationsSPEC Compliance
Testing
Test coverage includes:
toJSONmethod invocationtoJSONtoJSON(returned object also hastoJSON)toJSONfrom prototypes/classesreplacerfunctionPre-submission Checklist
Breaking Changes
Additional Context
This feature brings TOON encoding behavior closer to
JSON.stringify(), making it easier for developers familiar with JSON serialization patterns to use custom serialization with TOON. Classes and objects that already implementtoJSON()for JSON serialization will now automatically work with TOON encoding.Key implementation detail: The
toJSONcheck is placed before other type-specific normalizations to ensure it takes precedence, matching the behavior ofJSON.stringify().