Test case
1 generated file · +9 −1test/inputs/json/priority/combinations1.json
Mswiftsupport-linux-false--4bd8c552e09a / quicktype.swift+9 −1
| @@ -2793,7 +2793,15 @@ func newJSONDecoder() -> JSONDecoder { | ||
| 2793 | 2793 | func newJSONEncoder() -> JSONEncoder { |
| 2794 | 2794 | let encoder = JSONEncoder() |
| 2795 | 2795 | if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { |
| 2796 | - encoder.dateEncodingStrategy = .iso8601 | |
| 2796 | + encoder.dateEncodingStrategy = .custom { date, encoder in | |
| 2797 | + let formatter = ISO8601DateFormatter() | |
| 2798 | + if #available(iOS 11.0, OSX 10.13, tvOS 11.0, watchOS 4.0, *), | |
| 2799 | + date.timeIntervalSince1970.truncatingRemainder(dividingBy: 1) != 0 { | |
| 2800 | + formatter.formatOptions.insert(.withFractionalSeconds) | |
| 2801 | + } | |
| 2802 | + var container = encoder.singleValueContainer() | |
| 2803 | + try container.encode(formatter.string(from: date)) | |
| 2804 | + } | |
| 2797 | 2805 | } |
| 2798 | 2806 | return encoder |
| 2799 | 2807 | } |
Test case
1 generated file · +9 −1test/inputs/json/priority/combinations2.json
Mswiftsupport-linux-false--4bd8c552e09a / quicktype.swift+9 −1
| @@ -2557,7 +2557,15 @@ func newJSONDecoder() -> JSONDecoder { | ||
| 2557 | 2557 | func newJSONEncoder() -> JSONEncoder { |
| 2558 | 2558 | let encoder = JSONEncoder() |
| 2559 | 2559 | if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { |
| 2560 | - encoder.dateEncodingStrategy = .iso8601 | |
| 2560 | + encoder.dateEncodingStrategy = .custom { date, encoder in | |
| 2561 | + let formatter = ISO8601DateFormatter() | |
| 2562 | + if #available(iOS 11.0, OSX 10.13, tvOS 11.0, watchOS 4.0, *), | |
| 2563 | + date.timeIntervalSince1970.truncatingRemainder(dividingBy: 1) != 0 { | |
| 2564 | + formatter.formatOptions.insert(.withFractionalSeconds) | |
| 2565 | + } | |
| 2566 | + var container = encoder.singleValueContainer() | |
| 2567 | + try container.encode(formatter.string(from: date)) | |
| 2568 | + } | |
| 2561 | 2569 | } |
| 2562 | 2570 | return encoder |
| 2563 | 2571 | } |
Test case
1 generated file · +9 −1test/inputs/json/priority/combinations3.json
Mswiftsupport-linux-false--4bd8c552e09a / quicktype.swift+9 −1
| @@ -3092,7 +3092,15 @@ func newJSONDecoder() -> JSONDecoder { | ||
| 3092 | 3092 | func newJSONEncoder() -> JSONEncoder { |
| 3093 | 3093 | let encoder = JSONEncoder() |
| 3094 | 3094 | if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { |
| 3095 | - encoder.dateEncodingStrategy = .iso8601 | |
| 3095 | + encoder.dateEncodingStrategy = .custom { date, encoder in | |
| 3096 | + let formatter = ISO8601DateFormatter() | |
| 3097 | + if #available(iOS 11.0, OSX 10.13, tvOS 11.0, watchOS 4.0, *), | |
| 3098 | + date.timeIntervalSince1970.truncatingRemainder(dividingBy: 1) != 0 { | |
| 3099 | + formatter.formatOptions.insert(.withFractionalSeconds) | |
| 3100 | + } | |
| 3101 | + var container = encoder.singleValueContainer() | |
| 3102 | + try container.encode(formatter.string(from: date)) | |
| 3103 | + } | |
| 3096 | 3104 | } |
| 3097 | 3105 | return encoder |
| 3098 | 3106 | } |
Test case
1 generated file · +9 −1test/inputs/json/priority/combinations4.json
Mswiftsupport-linux-false--4bd8c552e09a / quicktype.swift+9 −1
| @@ -3596,7 +3596,15 @@ func newJSONDecoder() -> JSONDecoder { | ||
| 3596 | 3596 | func newJSONEncoder() -> JSONEncoder { |
| 3597 | 3597 | let encoder = JSONEncoder() |
| 3598 | 3598 | if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { |
| 3599 | - encoder.dateEncodingStrategy = .iso8601 | |
| 3599 | + encoder.dateEncodingStrategy = .custom { date, encoder in | |
| 3600 | + let formatter = ISO8601DateFormatter() | |
| 3601 | + if #available(iOS 11.0, OSX 10.13, tvOS 11.0, watchOS 4.0, *), | |
| 3602 | + date.timeIntervalSince1970.truncatingRemainder(dividingBy: 1) != 0 { | |
| 3603 | + formatter.formatOptions.insert(.withFractionalSeconds) | |
| 3604 | + } | |
| 3605 | + var container = encoder.singleValueContainer() | |
| 3606 | + try container.encode(formatter.string(from: date)) | |
| 3607 | + } | |
| 3600 | 3608 | } |
| 3601 | 3609 | return encoder |
| 3602 | 3610 | } |
Test case
1 generated file · +291 −0test/inputs/schema/date-time.schema
Aschema-swiftsupport-linux-false--4bd8c552e09a / quicktype.swift+291 −0
| @@ -0,0 +1,291 @@ | ||
| 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 complexUnionArray: [ComplexUnionArrayElement] | |
| 11 | + let date: String | |
| 12 | + let dateTime: Date | |
| 13 | + let localDateClass: LocalDate? | |
| 14 | + let offsetDateTimeClass: OffsetDateTime? | |
| 15 | + let offsetTimeClass: OffsetTime? | |
| 16 | + let time: String | |
| 17 | + let unionArray: [String] | |
| 18 | + | |
| 19 | + enum CodingKeys: String, CodingKey { | |
| 20 | + case complexUnionArray = "complex-union-array" | |
| 21 | + case date = "date" | |
| 22 | + case dateTime = "date-time" | |
| 23 | + case localDateClass = "local-date-class" | |
| 24 | + case offsetDateTimeClass = "offset-date-time-class" | |
| 25 | + case offsetTimeClass = "offset-time-class" | |
| 26 | + case time = "time" | |
| 27 | + case unionArray = "union-array" | |
| 28 | + } | |
| 29 | +} | |
| 30 | + | |
| 31 | +// MARK: TopLevel convenience initializers and mutators | |
| 32 | + | |
| 33 | +extension TopLevel { | |
| 34 | + init(data: Data) throws { | |
| 35 | + self = try newJSONDecoder().decode(TopLevel.self, from: data) | |
| 36 | + } | |
| 37 | + | |
| 38 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 39 | + guard let data = json.data(using: encoding) else { | |
| 40 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 41 | + } | |
| 42 | + try self.init(data: data) | |
| 43 | + } | |
| 44 | + | |
| 45 | + init(fromURL url: URL) throws { | |
| 46 | + try self.init(data: try Data(contentsOf: url)) | |
| 47 | + } | |
| 48 | + | |
| 49 | + func with( | |
| 50 | + complexUnionArray: [ComplexUnionArrayElement]? = nil, | |
| 51 | + date: String? = nil, | |
| 52 | + dateTime: Date? = nil, | |
| 53 | + localDateClass: LocalDate?? = nil, | |
| 54 | + offsetDateTimeClass: OffsetDateTime?? = nil, | |
| 55 | + offsetTimeClass: OffsetTime?? = nil, | |
| 56 | + time: String? = nil, | |
| 57 | + unionArray: [String]? = nil | |
| 58 | + ) -> TopLevel { | |
| 59 | + return TopLevel( | |
| 60 | + complexUnionArray: complexUnionArray ?? self.complexUnionArray, | |
| 61 | + date: date ?? self.date, | |
| 62 | + dateTime: dateTime ?? self.dateTime, | |
| 63 | + localDateClass: localDateClass ?? self.localDateClass, | |
| 64 | + offsetDateTimeClass: offsetDateTimeClass ?? self.offsetDateTimeClass, | |
| 65 | + offsetTimeClass: offsetTimeClass ?? self.offsetTimeClass, | |
| 66 | + time: time ?? self.time, | |
| 67 | + unionArray: unionArray ?? self.unionArray | |
| 68 | + ) | |
| 69 | + } | |
| 70 | + | |
| 71 | + func jsonData() throws -> Data { | |
| 72 | + return try newJSONEncoder().encode(self) | |
| 73 | + } | |
| 74 | + | |
| 75 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 76 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 77 | + } | |
| 78 | +} | |
| 79 | + | |
| 80 | +enum ComplexUnionArrayElement: Codable { | |
| 81 | + case dateTime(Date) | |
| 82 | + case enumeration(ComplexUnionArrayEnum) | |
| 83 | + case integer(Int) | |
| 84 | + | |
| 85 | + init(from decoder: Decoder) throws { | |
| 86 | + let container = try decoder.singleValueContainer() | |
| 87 | + if let x = try? container.decode(Int.self) { | |
| 88 | + self = .integer(x) | |
| 89 | + return | |
| 90 | + } | |
| 91 | + if let x = try? container.decode(Date.self) { | |
| 92 | + self = .dateTime(x) | |
| 93 | + return | |
| 94 | + } | |
| 95 | + if let x = try? container.decode(ComplexUnionArrayEnum.self) { | |
| 96 | + self = .enumeration(x) | |
| 97 | + return | |
| 98 | + } | |
| 99 | + throw DecodingError.typeMismatch(ComplexUnionArrayElement.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ComplexUnionArrayElement")) | |
| 100 | + } | |
| 101 | + | |
| 102 | + func encode(to encoder: Encoder) throws { | |
| 103 | + var container = encoder.singleValueContainer() | |
| 104 | + switch self { | |
| 105 | + case .dateTime(let x): | |
| 106 | + try container.encode(x) | |
| 107 | + case .enumeration(let x): | |
| 108 | + try container.encode(x) | |
| 109 | + case .integer(let x): | |
| 110 | + try container.encode(x) | |
| 111 | + } | |
| 112 | + } | |
| 113 | +} | |
| 114 | + | |
| 115 | +enum ComplexUnionArrayEnum: String, Codable { | |
| 116 | + case foo = "foo" | |
| 117 | + case bar = "bar" | |
| 118 | +} | |
| 119 | + | |
| 120 | +// MARK: - LocalDate | |
| 121 | +struct LocalDate: Codable { | |
| 122 | + private enum K: String, CodingKey { case unused } | |
| 123 | + init(from decoder: Decoder) throws { | |
| 124 | + _ = try decoder.container(keyedBy: K.self) | |
| 125 | + } | |
| 126 | + init() { | |
| 127 | + } | |
| 128 | +} | |
| 129 | + | |
| 130 | +// MARK: LocalDate convenience initializers and mutators | |
| 131 | + | |
| 132 | +extension LocalDate { | |
| 133 | + init(data: Data) throws { | |
| 134 | + self = try newJSONDecoder().decode(LocalDate.self, from: data) | |
| 135 | + } | |
| 136 | + | |
| 137 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 138 | + guard let data = json.data(using: encoding) else { | |
| 139 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 140 | + } | |
| 141 | + try self.init(data: data) | |
| 142 | + } | |
| 143 | + | |
| 144 | + init(fromURL url: URL) throws { | |
| 145 | + try self.init(data: try Data(contentsOf: url)) | |
| 146 | + } | |
| 147 | + | |
| 148 | + func with( | |
| 149 | + ) -> LocalDate { | |
| 150 | + return LocalDate( | |
| 151 | + ) | |
| 152 | + } | |
| 153 | + | |
| 154 | + func jsonData() throws -> Data { | |
| 155 | + return try newJSONEncoder().encode(self) | |
| 156 | + } | |
| 157 | + | |
| 158 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 159 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 160 | + } | |
| 161 | +} | |
| 162 | + | |
| 163 | +// MARK: - OffsetDateTime | |
| 164 | +struct OffsetDateTime: Codable { | |
| 165 | + private enum K: String, CodingKey { case unused } | |
| 166 | + init(from decoder: Decoder) throws { | |
| 167 | + _ = try decoder.container(keyedBy: K.self) | |
| 168 | + } | |
| 169 | + init() { | |
| 170 | + } | |
| 171 | +} | |
| 172 | + | |
| 173 | +// MARK: OffsetDateTime convenience initializers and mutators | |
| 174 | + | |
| 175 | +extension OffsetDateTime { | |
| 176 | + init(data: Data) throws { | |
| 177 | + self = try newJSONDecoder().decode(OffsetDateTime.self, from: data) | |
| 178 | + } | |
| 179 | + | |
| 180 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 181 | + guard let data = json.data(using: encoding) else { | |
| 182 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 183 | + } | |
| 184 | + try self.init(data: data) | |
| 185 | + } | |
| 186 | + | |
| 187 | + init(fromURL url: URL) throws { | |
| 188 | + try self.init(data: try Data(contentsOf: url)) | |
| 189 | + } | |
| 190 | + | |
| 191 | + func with( | |
| 192 | + ) -> OffsetDateTime { | |
| 193 | + return OffsetDateTime( | |
| 194 | + ) | |
| 195 | + } | |
| 196 | + | |
| 197 | + func jsonData() throws -> Data { | |
| 198 | + return try newJSONEncoder().encode(self) | |
| 199 | + } | |
| 200 | + | |
| 201 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 202 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 203 | + } | |
| 204 | +} | |
| 205 | + | |
| 206 | +// MARK: - OffsetTime | |
| 207 | +struct OffsetTime: Codable { | |
| 208 | + private enum K: String, CodingKey { case unused } | |
| 209 | + init(from decoder: Decoder) throws { | |
| 210 | + _ = try decoder.container(keyedBy: K.self) | |
| 211 | + } | |
| 212 | + init() { | |
| 213 | + } | |
| 214 | +} | |
| 215 | + | |
| 216 | +// MARK: OffsetTime convenience initializers and mutators | |
| 217 | + | |
| 218 | +extension OffsetTime { | |
| 219 | + init(data: Data) throws { | |
| 220 | + self = try newJSONDecoder().decode(OffsetTime.self, from: data) | |
| 221 | + } | |
| 222 | + | |
| 223 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 224 | + guard let data = json.data(using: encoding) else { | |
| 225 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 226 | + } | |
| 227 | + try self.init(data: data) | |
| 228 | + } | |
| 229 | + | |
| 230 | + init(fromURL url: URL) throws { | |
| 231 | + try self.init(data: try Data(contentsOf: url)) | |
| 232 | + } | |
| 233 | + | |
| 234 | + func with( | |
| 235 | + ) -> OffsetTime { | |
| 236 | + return OffsetTime( | |
| 237 | + ) | |
| 238 | + } | |
| 239 | + | |
| 240 | + func jsonData() throws -> Data { | |
| 241 | + return try newJSONEncoder().encode(self) | |
| 242 | + } | |
| 243 | + | |
| 244 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 245 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 246 | + } | |
| 247 | +} | |
| 248 | + | |
| 249 | +// MARK: - Helper functions for creating encoders and decoders | |
| 250 | + | |
| 251 | +private func isValidDate(_ value: String) -> Bool { | |
| 252 | + let formatter = DateFormatter() | |
| 253 | + formatter.locale = Locale(identifier: "en_US_POSIX") | |
| 254 | + formatter.timeZone = TimeZone(secondsFromGMT: 0) | |
| 255 | + formatter.dateFormat = "u-MM-dd" | |
| 256 | + formatter.isLenient = false | |
| 257 | + return formatter.date(from: String(value.prefix(10))) != nil | |
| 258 | +} | |
| 259 | + | |
| 260 | +func newJSONDecoder() -> JSONDecoder { | |
| 261 | + let decoder = JSONDecoder() | |
| 262 | + if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { | |
| 263 | + decoder.dateDecodingStrategy = .custom { decoder in | |
| 264 | + let dateStr = try decoder.singleValueContainer().decode(String.self).uppercased() | |
| 265 | + guard isValidDate(dateStr) else { | |
| 266 | + throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Invalid date")) | |
| 267 | + } | |
| 268 | + let dateData = try JSONEncoder().encode(dateStr) | |
| 269 | + let dateDecoder = JSONDecoder() | |
| 270 | + dateDecoder.dateDecodingStrategy = .iso8601 | |
| 271 | + return try dateDecoder.decode(Date.self, from: dateData) | |
| 272 | + } | |
| 273 | + } | |
| 274 | + return decoder | |
| 275 | +} | |
| 276 | + | |
| 277 | +func newJSONEncoder() -> JSONEncoder { | |
| 278 | + let encoder = JSONEncoder() | |
| 279 | + if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { | |
| 280 | + encoder.dateEncodingStrategy = .custom { date, encoder in | |
| 281 | + let formatter = ISO8601DateFormatter() | |
| 282 | + if #available(iOS 11.0, OSX 10.13, tvOS 11.0, watchOS 4.0, *), | |
| 283 | + date.timeIntervalSince1970.truncatingRemainder(dividingBy: 1) != 0 { | |
| 284 | + formatter.formatOptions.insert(.withFractionalSeconds) | |
| 285 | + } | |
| 286 | + var container = encoder.singleValueContainer() | |
| 287 | + try container.encode(formatter.string(from: date)) | |
| 288 | + } | |
| 289 | + } | |
| 290 | + return encoder | |
| 291 | +} |
No generated files match these filters.