@@ -138,18 +138,75 @@ class TestJSONEncoder : TestJSONEncoderSuper {
138
138
return Model ( first: " Johnny Appleseed " ,
139
139
140
140
}
141
+
142
+ enum TopLevelCodingKeys : String , CodingKey {
143
+ case top
144
+ }
145
+
146
+ enum FirstNestedCodingKeys : String , CodingKey {
147
+ case first
148
+ }
149
+ enum SecondNestedCodingKeys : String , CodingKey {
150
+ case second
151
+ }
141
152
}
142
153
143
- enum TopLevelCodingKeys : String , CodingKey {
144
- case top
145
- }
146
-
147
- enum FirstNestedCodingKeys : String , CodingKey {
148
- case first
154
+ let model = Model . testValue
155
+ if #available ( OSX 10 . 13 , iOS 11 . 0 , watchOS 4 . 0 , tvOS 11 . 0 , * ) {
156
+ let expectedJSON = " { \" top \" :{ \" first \" : \" Johnny Appleseed \" , \" second \" : \" [email protected] \" }} " . data ( using : . utf8 ) !
157
+ _testRoundTrip ( of : model , expectedJSON : expectedJSON , outputFormatting : [ . sortedKeys ] )
158
+ } else {
159
+ _testRoundTrip ( of : model )
149
160
}
150
- enum SecondNestedCodingKeys : String , CodingKey {
151
- case second
161
+ }
162
+
163
+ func testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey( ) {
164
+ struct Model : Codable , Equatable {
165
+ let first : String
166
+ let second : String
167
+
168
+ init ( from coder: Decoder ) throws {
169
+ let container = try coder. container ( keyedBy: TopLevelCodingKeys . self)
170
+
171
+ let firstNestedContainer = try container. nestedContainer ( keyedBy: FirstNestedCodingKeys . self, forKey: . top)
172
+ self . first = try firstNestedContainer. decode ( String . self, forKey: . first)
173
+
174
+ let secondNestedContainer = try container. nestedContainer ( keyedBy: SecondNestedCodingKeys . self, forKey: . top)
175
+ self . second = try secondNestedContainer. decode ( String . self, forKey: . second)
176
+ }
177
+
178
+ func encode( to encoder: Encoder ) throws {
179
+ var container = encoder. container ( keyedBy: TopLevelCodingKeys . self)
180
+
181
+ var firstNestedContainer = container. nestedContainer ( keyedBy: FirstNestedCodingKeys . self, forKey: . top)
182
+ try firstNestedContainer. encode ( self . first, forKey: . first)
183
+
184
+ var secondNestedContainer = container. nestedUnkeyedContainer ( forKey: . top)
185
+ try secondNestedContainer. encode ( self . second)
186
+ }
187
+
188
+ init ( first: String , second: String ) {
189
+ self . first = first
190
+ self . second = second
191
+ }
192
+
193
+ static var testValue : Model {
194
+ return Model ( first: " Johnny Appleseed " ,
195
+
196
+ }
197
+
198
+ enum TopLevelCodingKeys : String , CodingKey {
199
+ case top
200
+ }
201
+
202
+ enum FirstNestedCodingKeys : String , CodingKey {
203
+ case first
204
+ }
205
+ enum SecondNestedCodingKeys : String , CodingKey {
206
+ case second
207
+ }
152
208
}
209
+
153
210
let model = Model . testValue
154
211
if #available( OSX 10 . 13 , iOS 11 . 0 , watchOS 4 . 0 , tvOS 11 . 0 , * ) {
155
212
let expectedJSON = " { \" top \" :{ \" first \" : \" Johnny Appleseed \" , \" second \" : \" [email protected] \" }} " . data ( using
: . utf8
) !
@@ -158,7 +215,7 @@ class TestJSONEncoder : TestJSONEncoderSuper {
158
215
_testRoundTrip ( of: model)
159
216
}
160
217
}
161
-
218
+
162
219
// MARK: - Output Formatting Tests
163
220
func testEncodingOutputFormattingDefault( ) {
164
221
let expectedJSON = " { \" name \" : \" Johnny Appleseed \" , \" email \" : \" [email protected] \" } " . data ( using
: . utf8
) !
@@ -1672,6 +1729,11 @@ JSONEncoderTests.test("testEncodingTopLevelDeepStructuredType") { TestJSONEncode
1672
1729
JSONEncoderTests . test ( " testEncodingClassWhichSharesEncoderWithSuper " ) { TestJSONEncoder ( ) . testEncodingClassWhichSharesEncoderWithSuper ( ) }
1673
1730
JSONEncoderTests . test ( " testEncodingTopLevelNullableType " ) { TestJSONEncoder ( ) . testEncodingTopLevelNullableType ( ) }
1674
1731
JSONEncoderTests . test ( " testEncodingMultipleNestedContainersWithTheSameTopLevelKey " ) { TestJSONEncoder ( ) . testEncodingMultipleNestedContainersWithTheSameTopLevelKey ( ) }
1732
+ JSONEncoderTests . test ( " testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey " )
1733
+ . xfail ( . always( " Attempt to re-encode into already encoded container is invalid. This will always fail " ) )
1734
+ . code {
1735
+ TestJSONEncoder ( ) . testEncodingConfilictedTypeNestedContainersWithTheSameTopLevelKey ( )
1736
+ }
1675
1737
JSONEncoderTests . test ( " testEncodingOutputFormattingDefault " ) { TestJSONEncoder ( ) . testEncodingOutputFormattingDefault ( ) }
1676
1738
JSONEncoderTests . test ( " testEncodingOutputFormattingPrettyPrinted " ) { TestJSONEncoder ( ) . testEncodingOutputFormattingPrettyPrinted ( ) }
1677
1739
JSONEncoderTests . test ( " testEncodingOutputFormattingSortedKeys " ) { TestJSONEncoder ( ) . testEncodingOutputFormattingSortedKeys ( ) }
0 commit comments