Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Int enum as dictionary key, produce different json string than Int as dictionary key?

Tags:

json

ios

swift

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?

like image 225
Cheok Yan Cheng Avatar asked Mar 02 '23 18:03

Cheok Yan Cheng


1 Answers

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)
      }
    }
  }
like image 178
Juraj Blahunka Avatar answered Mar 04 '23 21:03

Juraj Blahunka