@itaiferber no problem, I'm sure you guys and girls have plenty of other more important things to keep you occupied.
Myself & @valeriomazzeo created something very similar to Unevaluated
yesterday called AnyCodable
with an equivalent strategy of fuzzy decoding to primitive types. It was originally an enum
with all of the primitive types as cases, but we refactored that down to using Any
like in your original suggestion. It's far from a perfect solution but it will do what we need for now, you can find the source here: GitHub - asensei/AnyCodable: Generic Any? data encapsulation meant to facilitate the transformation of loosely typed objects using Codable.
I do think that one problem with the current JSONDecoder is that once you tell it to decode(data)
there isn't an easy way to get the data back out without knowing about the type you are expecting beforehand. Internally it's using JSONSerialization
which decodes to NSNull, NSNumber, NSString, NSArray, NSDictionary
. Instead of the fuzzy decoding that we are having to do to get any values out with AnyCodable
, it would be useful to get to these raw values. Hence why I suggested my previous solution.
If we had access to the raw value provided by JSONSerialization
(the example of JSONSerialization is simply an implementation detail, but somewhere a decoder will have some sort of raw value it can pull from) the user of JSONDecoder
could implement whatever mapping they wanted and would give back all of the control from JSONDecoder
.