I try to convert a dictionary, with Int enum
enum TypeE: Int, Codable
{
case note = 1
case tab
}
let encoder = JSONEncoder()
let dictionary0 = [TypeE.note:"VALUE0", TypeE.tab:"VALUE1"]
var data = try encoder.encode(dictionary0)
var string = String(data: data, encoding: .utf8)!
// [1,"VALUE0",2,"VALUE1"]
print(string)
The produced json string output is
[1,"VALUE0",2,"VALUE1"]
It looks strange to me. As, the produced json string represents an array.
If I tested with
let encoder = JSONEncoder()
let dictionary1 = [1:"VALUE0", 2:"VALUE1"]
var data = try encoder.encode(dictionary1)
var string = String(data: data, encoding: .utf8)!
// {"1":"VALUE0","2":"VALUE1"}
print(string)
The produced json string output is
{"1":"VALUE0","2":"VALUE1"}
It seems like if I use Int enum as dictionary key, the produced json string will become representation of array?
Is there any mistake in my code, or my expectation is incorrect?
The source code of Codable provides explanation for this behavior. If the key isn't a String
or an Int
, the resulting type is an Array
:
public func encode(to encoder: Encoder) throws {
if Key.self == String.self {
// Since the keys are already Strings, we can use them as keys directly.
...
} else if Key.self == Int.self {
// Since the keys are already Ints, we can use them as keys directly.
...
} else {
// Keys are Encodable but not Strings or Ints, so we cannot arbitrarily
// convert to keys. We can encode as an array of alternating key-value
// pairs, though.
var container = encoder.unkeyedContainer()
for (key, value) in self {
try container.encode(key)
try container.encode(value)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With