Skip to content

[SR-12588] JSONEncoder's sortedKeys doesn't work when Key is an enum that conforms to String  #3258

Open
@aciidgh

Description

@aciidgh
Previous ID SR-12588
Radar rdar://problem/62202368
Original Reporter @aciidb0mb3r
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler, Foundation
Labels Bug
Assignee None
Priority Medium

md5: f44fa76a09602e1ef66b339697c43ca2

Issue Description:

The following code produces different output on each invocation.

import Foundation

enum Key: String, Codable {
    case a, b, c
}

struct MyStruct: Encodable {
    var foo: [Key: Int]
}

let t = MyStruct(foo: [
    .a: 1,
    .b: 1,
    .c: 1,
])

let encoder = JSONEncoder()
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
let encoded = try encoder.encode(t)
print(String(data: encoded, encoding: .utf8)!)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions