Continuing the discussion from Idea: Exposing _JSONEncoder and _JSONDecoder functionality:
Back in July, 2017, @itaiferber mentioned the possibility of exposing a StructureEncoder and a StructureDecoder that would work similarly to JSONEncoder and JSONDecoder with the exception that they would not perform the final/initial steps of serializing to and from JSON.
I find this functionality tremendously helpful, so I hope to revive the discussion about this possibility by giving a shot at an implementation.
So, one question I have about swift development in general: My current implementation lives here: GitHub - mortenbekditlevsen/swift at structureencoder
Am I free to create a WIP PR against swift/master even though I haven't discussed this with anyone yet? Or do random PRs like this somehow 'pollute' anything for anyone?
More general questions:
- Do anyone else see a benefit in having this
Encoder/Decoder-pair?
My personal use case is the Firebase real-time database from Google. The APIs for storing and retrieving data accept and returnAnyvalues containing exactly such JSON-ish data structures. - With regards to the API for this. Does it seem alright to basically duplicate the API for
JSONEncoderandJSONDecoder?
In my oppinion I think that it does, and that it is nice to deal with a familiar API.
Questions that I have encountered in implementing this:
-
Should a possibly future
StructureEncoderandStructureDecoderlive in the same file as the correspondingJSONEncoderandJSONDecoder. They share the implementation details (_JSONEncoderand_JSONDecoderwhich are currentlyprivate, but these could of course be madeinternalinstead.
In the current implementation they do live in the same file. -
With regards to the currently
private_JSONEncoderand_JSONDecoder: Should they still be named in this way even though they are now the base of two types of encoding/decoding?
In the current implementation I have not done any renaming. -
With regards to the options for
StructureEncoderandStructureDecoder, then the options ofJSONEncoderandJSONDecodercan be used as they are - with exception ofJSONEncoder.OutputFormattingwhich do not make sense forStructureEncoding.
The 'cloned' options could either be typealiases for the correspondingJSONEncoder/JSONDecoderoptions, or they could be completely separate enumerations.
In the current implementation I have chosen to model them as new enumarations with the exact same structure, and then expose a property that can return theJSONEncoder/JSONDecoderversion of the options to be passed on to_JSONEncoderand_JSONDecoder.
I think that this nicely hides the fact that the implementation details are currently shared. -
Are there any assumptions in the
_JSONDecoderabout the possible values and types that can be present due to the knowledge that theJSONSerializationwas the only source of input for the_JSONDecoder.
I feel that there may be... -
For the
JSONEncoder, top level fragments are not allowed. Should 'top level values' be allowed inStructureEncoderor not?
In the current implementation top level values are allowed since I could find no good argument to disallow them. -
I have basically cloned all tests from
TestJSONEncoder.swiftto a newTestStructureEncoder.swiftfile. I have removed a few tests that relate to formatting, and change the ones that verify that top level fragments are disallowed. For the main workhorse of the test file, I have skipped the equality check of the intermediary value, since the intermediary format is a value of typeAnyand it's hard to test for equality when one value may be anNSDictionaryand the other may be aDictionary. I think that it is fair to skip this extra test step, since the complete round trip is still tested. Is this an acceptable approach to testing this?
I look forward to seeing any comments on this.
Sincerely,
/morten