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 · +138 −0test/inputs/schema/date-time.schema
Aschema-swiftsupport-linux-false--4bd8c552e09a / quicktype.swift+138 −0
| @@ -0,0 +1,138 @@ | ||
| 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 time: String | |
| 14 | + let unionArray: [String] | |
| 15 | + | |
| 16 | + enum CodingKeys: String, CodingKey { | |
| 17 | + case complexUnionArray = "complex-union-array" | |
| 18 | + case date = "date" | |
| 19 | + case dateTime = "date-time" | |
| 20 | + case time = "time" | |
| 21 | + case unionArray = "union-array" | |
| 22 | + } | |
| 23 | +} | |
| 24 | + | |
| 25 | +// MARK: TopLevel convenience initializers and mutators | |
| 26 | + | |
| 27 | +extension TopLevel { | |
| 28 | + init(data: Data) throws { | |
| 29 | + self = try newJSONDecoder().decode(TopLevel.self, from: data) | |
| 30 | + } | |
| 31 | + | |
| 32 | + init(_ json: String, using encoding: String.Encoding = .utf8) throws { | |
| 33 | + guard let data = json.data(using: encoding) else { | |
| 34 | + throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil) | |
| 35 | + } | |
| 36 | + try self.init(data: data) | |
| 37 | + } | |
| 38 | + | |
| 39 | + init(fromURL url: URL) throws { | |
| 40 | + try self.init(data: try Data(contentsOf: url)) | |
| 41 | + } | |
| 42 | + | |
| 43 | + func with( | |
| 44 | + complexUnionArray: [ComplexUnionArrayElement]? = nil, | |
| 45 | + date: String? = nil, | |
| 46 | + dateTime: Date? = nil, | |
| 47 | + time: String? = nil, | |
| 48 | + unionArray: [String]? = nil | |
| 49 | + ) -> TopLevel { | |
| 50 | + return TopLevel( | |
| 51 | + complexUnionArray: complexUnionArray ?? self.complexUnionArray, | |
| 52 | + date: date ?? self.date, | |
| 53 | + dateTime: dateTime ?? self.dateTime, | |
| 54 | + time: time ?? self.time, | |
| 55 | + unionArray: unionArray ?? self.unionArray | |
| 56 | + ) | |
| 57 | + } | |
| 58 | + | |
| 59 | + func jsonData() throws -> Data { | |
| 60 | + return try newJSONEncoder().encode(self) | |
| 61 | + } | |
| 62 | + | |
| 63 | + func jsonString(encoding: String.Encoding = .utf8) throws -> String? { | |
| 64 | + return String(data: try self.jsonData(), encoding: encoding) | |
| 65 | + } | |
| 66 | +} | |
| 67 | + | |
| 68 | +enum ComplexUnionArrayElement: Codable { | |
| 69 | + case dateTime(Date) | |
| 70 | + case enumeration(ComplexUnionArrayEnum) | |
| 71 | + case integer(Int) | |
| 72 | + | |
| 73 | + init(from decoder: Decoder) throws { | |
| 74 | + let container = try decoder.singleValueContainer() | |
| 75 | + if let x = try? container.decode(Int.self) { | |
| 76 | + self = .integer(x) | |
| 77 | + return | |
| 78 | + } | |
| 79 | + if let x = try? container.decode(Date.self) { | |
| 80 | + self = .dateTime(x) | |
| 81 | + return | |
| 82 | + } | |
| 83 | + if let x = try? container.decode(ComplexUnionArrayEnum.self) { | |
| 84 | + self = .enumeration(x) | |
| 85 | + return | |
| 86 | + } | |
| 87 | + throw DecodingError.typeMismatch(ComplexUnionArrayElement.self, DecodingError.Context(codingPath: decoder.codingPath, debugDescription: "Wrong type for ComplexUnionArrayElement")) | |
| 88 | + } | |
| 89 | + | |
| 90 | + func encode(to encoder: Encoder) throws { | |
| 91 | + var container = encoder.singleValueContainer() | |
| 92 | + switch self { | |
| 93 | + case .dateTime(let x): | |
| 94 | + try container.encode(x) | |
| 95 | + case .enumeration(let x): | |
| 96 | + try container.encode(x) | |
| 97 | + case .integer(let x): | |
| 98 | + try container.encode(x) | |
| 99 | + } | |
| 100 | + } | |
| 101 | +} | |
| 102 | + | |
| 103 | +enum ComplexUnionArrayEnum: String, Codable { | |
| 104 | + case foo = "foo" | |
| 105 | + case bar = "bar" | |
| 106 | +} | |
| 107 | + | |
| 108 | +// MARK: - Helper functions for creating encoders and decoders | |
| 109 | + | |
| 110 | +func newJSONDecoder() -> JSONDecoder { | |
| 111 | + let decoder = JSONDecoder() | |
| 112 | + if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { | |
| 113 | + decoder.dateDecodingStrategy = .custom { decoder in | |
| 114 | + let dateStr = try decoder.singleValueContainer().decode(String.self).uppercased() | |
| 115 | + let dateData = try JSONEncoder().encode(dateStr) | |
| 116 | + let dateDecoder = JSONDecoder() | |
| 117 | + dateDecoder.dateDecodingStrategy = .iso8601 | |
| 118 | + return try dateDecoder.decode(Date.self, from: dateData) | |
| 119 | + } | |
| 120 | + } | |
| 121 | + return decoder | |
| 122 | +} | |
| 123 | + | |
| 124 | +func newJSONEncoder() -> JSONEncoder { | |
| 125 | + let encoder = JSONEncoder() | |
| 126 | + if #available(iOS 10.0, OSX 10.12, tvOS 10.0, watchOS 3.0, *) { | |
| 127 | + encoder.dateEncodingStrategy = .custom { date, encoder in | |
| 128 | + let formatter = ISO8601DateFormatter() | |
| 129 | + if #available(iOS 11.0, OSX 10.13, tvOS 11.0, watchOS 4.0, *), | |
| 130 | + date.timeIntervalSince1970.truncatingRemainder(dividingBy: 1) != 0 { | |
| 131 | + formatter.formatOptions.insert(.withFractionalSeconds) | |
| 132 | + } | |
| 133 | + var container = encoder.singleValueContainer() | |
| 134 | + try container.encode(formatter.string(from: date)) | |
| 135 | + } | |
| 136 | + } | |
| 137 | + return encoder | |
| 138 | +} |
No generated files match these filters.