Test case
1 generated file · +94 −0test/inputs/json/samples/simple-object.json
Aswiftcoding-keys-protocol-CaseIterable--169323764eaa / quicktype.swift+94 −0
| @@ -0,0 +1,94 @@ | ||
| 1 | +// This file was generated from JSON Schema using quicktype, do not modify it directly. | |
| 2 | +// To parse the JSON, add this file to your project and do: | |
| 3 | +// | |
| 4 | +// let topLevel = try TopLevel(json) | |
| 5 | + | |
| 6 | +import Foundation | |
| 7 | + | |
| 8 | +// MARK: - TopLevel | |
| 9 | +struct TopLevel: Codable { | |
| 10 | + let date: Int | |
| 11 | + let title: String | |
| 12 | + let validity: Bool | |
| 13 | + | |
| 14 | + enum CodingKeys: String, CodingKey, CaseIterable { | |
| 15 | + case date = "date" | |
| 16 | + case title = "title" | |
| 17 | + case validity = "validity" | |
| 18 | + } | |
| 19 | +} | |
| 20 | + | |
| 21 | +// MARK: TopLevel convenience initializers and mutators | |
| 22 | + | |
| 23 | +extension TopLevel { | |
| 24 | + init(data: Data) throws { | |
| 25 | + self = try newJSONDecoder().decode(TopLevel.self, from: data) | |
| 26 | + } | |
| 27 | + | |
| 28 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 29 | + guard let data = json.data(using: encoding) else { | |
| 30 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 31 | + } | |
| 32 | + try self.init(data: data) | |
| 33 | + } | |
| 34 | + | |
| 35 | + init(fromURL url: URL) throws { | |
| 36 | + try self.init(data: try Data(contentsOf: url)) | |
| 37 | + } | |
| 38 | + | |
| 39 | + func with( | |
| 40 | + date: Int? = nil, | |
| 41 | + title: String? = nil, | |
| 42 | + validity: Bool? = nil | |
| 43 | + ) -> TopLevel { | |
| 44 | + return TopLevel( | |
| 45 | + date: date ?? self.date, | |
| 46 | + title: title ?? self.title, | |
| 47 | + validity: validity ?? self.validity | |
| 48 | + ) | |
| 49 | + } | |
| 50 | + | |
| 51 | + func jsonData() throws -> Data { | |
| 52 | + return try newJSONEncoder().encode(self) | |
| 53 | + } | |
| 54 | + | |
| 55 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 56 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 57 | + } | |
| 58 | +} | |
| 59 | + | |
| 60 | +// MARK: - Helper functions for creating encoders and decoders | |
| 61 | + | |
| 62 | +func newJSONDecoder() -> JSONDecoder { | |
| 63 | + let decoder = JSONDecoder() | |
| 64 | + decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in | |
| 65 | + let container = try decoder.singleValueContainer() | |
| 66 | + let dateStr = try container.decode(String.self) | |
| 67 | + | |
| 68 | + let formatter = DateFormatter() | |
| 69 | + formatter.calendar = Calendar(identifier: .iso8601) | |
| 70 | + formatter.locale = Locale(identifier: "en_US_POSIX") | |
| 71 | + formatter.timeZone = TimeZone(secondsFromGMT: 0) | |
| 72 | + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX" | |
| 73 | + if let date = formatter.date(from: dateStr) { | |
| 74 | + return date | |
| 75 | + } | |
| 76 | + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXXXX" | |
| 77 | + if let date = formatter.date(from: dateStr) { | |
| 78 | + return date | |
| 79 | + } | |
| 80 | + throw DecodingError.typeMismatch(Date.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Could not decode date")) | |
| 81 | + }) | |
| 82 | + return decoder | |
| 83 | +} | |
| 84 | + | |
| 85 | +func newJSONEncoder() -> JSONEncoder { | |
| 86 | + let encoder = JSONEncoder() | |
| 87 | + let formatter = DateFormatter() | |
| 88 | + formatter.calendar = Calendar(identifier: .iso8601) | |
| 89 | + formatter.locale = Locale(identifier: "en_US_POSIX") | |
| 90 | + formatter.timeZone = TimeZone(secondsFromGMT: 0) | |
| 91 | + formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXXXX" | |
| 92 | + encoder.dateEncodingStrategy = .formatted(formatter) | |
| 93 | + return encoder | |
| 94 | +} |
No generated files match these filters.