Avatar
norio_nomura 7/27/2018 2:53 PM
@swift-4.1.3 import Foundation struct S: Codable { var p1: String let p2 = 1 } let encoder = JSONEncoder() encoder.outputFormatting = .sortedKeys let json = String(data: try encoder.encode(S(p1: "p1")), encoding: .utf8) print(json == """ {"p1":"p1","p2":1} """) // true let s = try JSONDecoder().decode(S.self, from: """ {"p1":"p1","p2":2} """.data(using: .utf8)!) print(s.p2 == 2) // false
👍 1