Skip to content

Add toJSON method support for custom serialization#237

Merged
johannschopplich merged 5 commits intotoon-format:mainfrom
ViliamKopecky:main
Dec 4, 2025
Merged

Add toJSON method support for custom serialization#237
johannschopplich merged 5 commits intotoon-format:mainfrom
ViliamKopecky:main

Conversation

@ViliamKopecky
Copy link
Copy Markdown
Contributor

Description

Adds support for the toJSON method during TOON encoding, allowing objects to customize their serialization behavior. This aligns TOON with JSON.stringify() behavior, where objects implementing toJSON() can control how they are represented when serialized.

The implementation checks for the presence of a toJSON method 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

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Refactoring (no functional changes)
  • Performance improvement
  • Test coverage improvement

Changes Made

  • Added toJSON method detection and invocation in normalizeValue() function
  • toJSON is called early in normalization to take precedence over built-in type handlers
  • Result of toJSON is recursively normalized to handle nested toJSON implementations
  • Added comprehensive test suite with 18 test cases covering various scenarios

SPEC Compliance

  • This PR implements/fixes spec compliance
  • Spec section(s) affected:
  • Spec version:

Testing

  • All existing tests pass
  • Added new tests for changes
  • Tests cover edge cases and spec compliance

Test coverage includes:

  • Basic toJSON method invocation
  • Return types: primitives, arrays, null, undefined, objects
  • Nested objects and array elements with toJSON
  • Recursive toJSON (returned object also has toJSON)
  • Precedence over Date, Array, Set, Map normalization
  • Inherited toJSON from prototypes/classes
  • Round-trip encoding/decoding
  • Deeply nested structures
  • Integration with replacer function
  • Different encoding options (indent, delimiter)

Pre-submission Checklist

  • My code follows the project's coding standards
  • I have run code formatting/linting tools
  • I have added tests that prove my fix/feature works
  • New and existing tests pass locally
  • I have updated documentation if needed
  • I have reviewed the TOON specification for relevant sections

Breaking Changes

  • No breaking changes
  • Breaking changes (describe migration path below)

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 implement toJSON() for JSON serialization will now automatically work with TOON encoding.

Key implementation detail: The toJSON check is placed before other type-specific normalizations to ensure it takes precedence, matching the behavior of JSON.stringify().

@johannschopplich johannschopplich merged commit a4538b4 into toon-format:main Dec 4, 2025
@johannschopplich
Copy link
Copy Markdown
Collaborator

Thanks!

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.

2 participants