Open Xezun opened 1 year ago
cc @parkera
We don’t have enough info when encoding the ”bad” example to know that Name will be encoded as a single value container with just a String and therefore is suitable for use as a key in a JSON dictionary (which is of course required to be a String). Therefore Dictionary goes into the fallback path of encoding itself as an Array instead.
If a dictionary's key type is defined by OC like this:
typedef NSString *SomeKeyName NS_EXTENSIBLE_STRING_ENUM;
In swift, the key type was converted to struct, which lead to the JSONEncoder
doesn't work as expected.
The JSONSerialization
may work, but it needs all the value types implemented NSCoding
protocol at first.
Is there any way to resolve this problem without changing many codes?
Thank you!
@parkera
I had the same problem, my goal was to use a string enum as a key so that I could better process the results, but failed due to the lack of this feature. I believe When the Decoder thinks it's a Dictionary, it should be able to get the target type, and instead of decoding the key with String, it should decode it with Dictionary.Key, like the Dictionary.Value
@YOCKOW I just fixed this in swift-foundation(FoundationPreview), but it looks like I should move it to swift-corelibs-foundation?
Environment