Skip to content

[Experimental] Implement JSON coding without using Foundation or Codable. #1024

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

grynspan
Copy link
Contributor

This PR replaces most of our uses of JSONEncoder with a home-grown implementation. Types conform to the internal JSON.Serializable protocol and emit strings, numbers, dictionaries, arrays, etc. Unlike with Codable, there is no synthesized implementation here.

Pros:

  • Less reliance on Foundation (ideally we drop the dependency entirely at some point);
  • No need to use existentials to encode a value (allowing us to eventually support Embedded Swift); and
  • The potential for more inlining as all the encoding work is done in-module.

Cons:

  • Don't Repeat Yourself;
  • This implementation doesn't come with a decoder (which is harder to write);
  • The implementation of JSON.Serializable cannot be supplied by the compiler;
  • More code means more technical debt and a higher maintenance burden;
  • The implementation is less configurable/customizable than Foundation's; and
  • The implementation is not a full Encoder conformance, so it cannot be used to reimplement CustomTestArgumentEncodable.

Resolves rdar://146964016.

Checklist:

  • Code and documentation should follow the style of the Style Guide.
  • If public symbols are renamed or modified, DocC references should be updated.

This PR replaces most of our uses of `JSONEncoder` with a home-grown
implementation. Types conform to the internal `JSON.Serializable` protocol and
emit strings, numbers, dictionaries, arrays, etc. Unlike with `Codable`, there
is no synthesized implementation here.

Pros:

- Less reliance on Foundation (ideally we drop the dependency entirely at some
  point);
- No need to use existentials to encode a value (allowing us to eventually
  support Embedded Swift); and
- The potential for more inlining as all the encoding work is done in-module.

Cons:

- Don't Repeat Yourself;
- This implementation doesn't come with a decoder (which is harder to write);
- The implementation of `JSON.Serializable` cannot be supplied by the compiler;
- More code means more technical debt and a higher maintenance burden;
- The implementation is less configurable/customizable than Foundation's; and
- The implementation is not a full `Encoder` conformance, so it cannot be used
  to reimplement `CustomTestArgumentEncodable`.

Resolves rdar://146964016.
@grynspan grynspan added tools integration 🛠️ Integration of swift-testing into tools/IDEs embedded-swift 📟 Embedded Swift issues issue-handling Related to Issue handling within the testing library labels Mar 13, 2025
@grynspan grynspan added this to the Swift 6.2 milestone Mar 13, 2025
@grynspan grynspan self-assigned this Mar 13, 2025
@grynspan
Copy link
Contributor Author

I'm going to redo this as an enum now that I have a better idea of the constraints we need to solve for.

@grynspan
Copy link
Contributor Author

@swift-ci test

@grynspan grynspan modified the milestones: Swift 6.2, Swift 6.x Apr 6, 2025
@grynspan grynspan changed the title Implement JSON coding without using Foundation or Codable. [Experimental] Implement JSON coding without using Foundation or Codable. Apr 8, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
embedded-swift 📟 Embedded Swift issues issue-handling Related to Issue handling within the testing library tools integration 🛠️ Integration of swift-testing into tools/IDEs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant