I do not like the idea of such an abstract Unevaluated
type, although I think it is in general the right way to solve the problem discussed in this thread.
I think it would be better to add something like this specifically for JSON. I furthermore think this could be a great place to provide some more functionality for accessing "raw" JSON by making Unevaluated
more than just a marker type.
The problem I see, is this: You somehow find out, that the format you are currently decoding from is JSON. Then you decode such an Unevaluated
value from the decoder. Now you have to do something with either the NSDictionary, or NSArray, or whatever you get as .value
. Those types are as I see it an implementation detail of JSONDecoder (O and what you get with Unevalutuated
is actually a "raw" view into the decoders storage. This has in my opinion several disadvantages:
- It would be just annoying to refactor the custom decoding code, if JSONDecoder started to use Dictionary instead of NSDictionary, Array instead of NSArray, and so on.
- The resulting code won't be easily understandable I guess, because you won't be able to figure out right away, that this code is working with "raw" JSON / I would not immediately connect NSDictionary with JSON's "{ }". At least it won't be self documenting.
Im sticking a bit too much to the implementation example for above. Beyond this, an abstract implementation would leave us with those problems:
- There is no indication that there is just a limited set of options, but there is actually just a limited set of possible types for this
Any
value, because it is JSON, as we found out. - What is the decoders internal representation if it isn't JSON?
Also, I do dislike the concept of an abstract type for all formats, because I fell like it does not match the design of Decoder. I would consider it better to have some method like abstractRepresentation
alongside with keyedContainer
, unkeyedContainer
and singleValueContainer
. The problem with this is, that the return type is still open...
If we had a type such as RawJSON
(I'm really not good at naming), we could add general working-with-JSON capabilities and not show the NSDictionarys under our cloths to the world. One could ask this type whether it has a keyed container for us, or a number, and so on. We could also tell it that we want to have a number, also if there is a string (only if it is convertible of course). These examples are certainly not well designed right now, but I think it shows what I mean. This new type would could take a way lager role in the JSON decoding game. We could start with such a RawJSON
, do some lookup and extraction (like resolving the upper container levels that maybe contain information we don't really want to have as a type on it's own, I don't know if this is happens often, but I have seen something similar with another format) and then at some other point, tell JSONDecoder to decode the remaining JSON for us. If we need to handle something like the meta data from this issue, we could go on manually working with the JSON and if it contained other data, that would rather like to have as an instance of one of our custom types, we could just go on as we started: We would tell JSONDecoder to decode the remaining RawJSON
extracted before.
I don't think wo should miss this chance with adding an abstract type. Other implementations for other formats could follow the same way, but would not be required to do so.