Add JSONValue type, helpers, and usage example#25
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9b69cbee48
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| } else if let value = try? container.decode(Double.self) { | ||
| self = .double(value) |
There was a problem hiding this comment.
Preserve out-of-range JSON numbers losslessly
In JSONValue.init(from:), values that fail Int decoding are immediately decoded as Double, which silently rounds many valid JSON numbers (for example, integers larger than Int.max such as 9223372036854775809). This means decoding and then re-encoding can change the numeric value, so arbitrary JSON payloads are no longer lossless. Because this type is intended for schema-less JSON transport, numeric corruption here is user-visible and should be avoided by using a lossless number representation (e.g., Decimal/string-backed number case) before falling back to binary floating point.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pull request overview
This PR adds a dynamic JSONValue type to CodableKit to support schema-less JSON decoding/encoding, runtime inspection, and path-based traversal, along with README usage examples and a dedicated test suite.
Changes:
- Introduces
JSONValue,JSONValueError, andJSONPathComponentwith Codable conformance, literal initializers, accessors, and traversal helpers. - Adds runtime tests covering decoding/encoding, accessors, path traversal, and convenience JSON string/data APIs.
- Updates the README with a “Dynamic JSON values” usage example.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
Sources/CodableKit/JSONValue.swift |
Implements the JSONValue dynamic JSON representation and helper APIs. |
Tests/CodableKitTests/JSONValueTests.swift |
Adds Swift Testing-based coverage for JSONValue behavior and conveniences. |
README.md |
Documents basic usage patterns for decoding and traversing JSONValue. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This pull request introduces a new dynamic JSON value type,
JSONValue, to the codebase.JSONValueallows for flexible, schema-less representation and traversal of arbitrary JSON data, similar to Rust'sserde_json::Value. The implementation includes decoding/encoding support, runtime accessors, path-based traversal, and literal conveniences. Comprehensive tests and documentation are also added.New dynamic JSON value support:
JSONValueenum to represent any JSON value (null, bool, string, int, double, array, object) with full Codable conformance, runtime accessors, and literal initializers. Includes path-based traversal via the newJSONPathComponenttype, and convenience methods for encoding/decoding from JSON data and strings. (Sources/CodableKit/JSONValue.swift)JSONValuein the project README, demonstrating decoding, traversal, and construction from JSON strings and literals. (README.md)Testing:
JSONValue, covering decoding, encoding, round-tripping, runtime accessors, path traversal, literal construction, and integration with Codable models. (Tests/CodableKitTests/JSONValueTests.swift)