Improve JSON5 parser API with strongly-typed JSON5Value sealed class #39
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.
This PR introduces a new strongly-typed API for JSON5 parsing while maintaining backward compatibility with the existing API.
Changes
New JSON5Value Sealed Class
Added a comprehensive
JSON5Valuesealed class hierarchy that represents all possible JSON5 values according to the JSON5 specification:JSON5Value.Object- JSON5 objects (key-value maps)JSON5Value.Array- JSON5 arrays (ordered lists)JSON5Value.String- String valuesJSON5Value.Numberhierarchy:Integer- Whole numbersDecimal- Floating-point numbersHexadecimal- Hexadecimal numbers (0x prefix)PositiveInfinity- Positive infinityNegativeInfinity- Negative infinityNaN- Not a NumberJSON5Value.Boolean- Boolean valuesJSON5Value.Null- Null valuesUpdated API Methods
New strongly-typed parse method:
Backward compatibility method:
Both methods also support reviver functions.
Helper Methods
Added
JSON5Value.from(value: Any?)companion method to convert existing Kotlin objects to strongly-typed JSON5 values.Usage Examples
New Strongly-Typed API
Backward Compatibility
Breaking Changes
parse()method now returnsJSON5Valueinstead ofAny?.Migration: Replace
JSON5.parse()withJSON5.parseToAny()to preserve existing behavior.Testing
parseToAny()for compatibilityFixes #38.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.