swift-testing icon indicating copy to clipboard operation
swift-testing copied to clipboard

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

Open grynspan opened this issue 8 months ago • 2 comments

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.

grynspan avatar Mar 13 '25 17:03 grynspan

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 avatar Mar 14 '25 00:03 grynspan

@swift-ci test

grynspan avatar Mar 14 '25 04:03 grynspan