@@ -383,30 +383,44 @@ fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCon
383
383
self . container = container
384
384
}
385
385
386
+ // MARK: - Coding Path Operations
387
+
388
+ private func _converted( _ key: CodingKey ) -> CodingKey {
389
+ switch encoder. options. keyEncodingStrategy {
390
+ case . useDefaultKeys:
391
+ return key
392
+ case . convertToSnakeCase:
393
+ let newKeyString = JSONEncoder . KeyEncodingStrategy. _convertToSnakeCase ( key. stringValue)
394
+ return _JSONKey ( stringValue: newKeyString, intValue: key. intValue)
395
+ case . custom( let converter) :
396
+ return converter ( codingPath + [ key] )
397
+ }
398
+ }
399
+
386
400
// MARK: - KeyedEncodingContainerProtocol Methods
387
401
388
- public mutating func encodeNil( forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = NSNull ( ) }
389
- public mutating func encode( _ value: Bool , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
390
- public mutating func encode( _ value: Int , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
391
- public mutating func encode( _ value: Int8 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
392
- public mutating func encode( _ value: Int16 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
393
- public mutating func encode( _ value: Int32 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
394
- public mutating func encode( _ value: Int64 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
395
- public mutating func encode( _ value: UInt , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
396
- public mutating func encode( _ value: UInt8 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
397
- public mutating func encode( _ value: UInt16 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
398
- public mutating func encode( _ value: UInt32 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
399
- public mutating func encode( _ value: UInt64 , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
400
- public mutating func encode( _ value: String , forKey key: Key ) throws { self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
402
+ public mutating func encodeNil( forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = NSNull ( ) }
403
+ public mutating func encode( _ value: Bool , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
404
+ public mutating func encode( _ value: Int , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
405
+ public mutating func encode( _ value: Int8 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
406
+ public mutating func encode( _ value: Int16 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
407
+ public mutating func encode( _ value: Int32 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
408
+ public mutating func encode( _ value: Int64 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
409
+ public mutating func encode( _ value: UInt , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
410
+ public mutating func encode( _ value: UInt8 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
411
+ public mutating func encode( _ value: UInt16 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
412
+ public mutating func encode( _ value: UInt32 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
413
+ public mutating func encode( _ value: UInt64 , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
414
+ public mutating func encode( _ value: String , forKey key: Key ) throws { self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = self . encoder. box ( value) }
401
415
402
416
public mutating func encode( _ value: Float , forKey key: Key ) throws {
403
417
// Since the float may be invalid and throw, the coding path needs to contain this key.
404
418
self . encoder. codingPath. append ( key)
405
419
defer { self . encoder. codingPath. removeLast ( ) }
406
420
#if DEPLOYMENT_RUNTIME_SWIFT
407
- self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = try self . encoder. box ( value)
421
+ self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = try self . encoder. box ( value)
408
422
#else
409
- self . container [ key. stringValue] = try self . encoder. box ( value)
423
+ self . container [ _converted ( key) . stringValue] = try self . encoder. box ( value)
410
424
#endif
411
425
}
412
426
@@ -415,28 +429,28 @@ fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCon
415
429
self . encoder. codingPath. append ( key)
416
430
defer { self . encoder. codingPath. removeLast ( ) }
417
431
#if DEPLOYMENT_RUNTIME_SWIFT
418
- self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = try self . encoder. box ( value)
432
+ self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = try self . encoder. box ( value)
419
433
#else
420
- self . container [ key. stringValue] = try self . encoder. box ( value)
434
+ self . container [ _converted ( key) . stringValue] = try self . encoder. box ( value)
421
435
#endif
422
436
}
423
437
424
438
public mutating func encode< T : Encodable > ( _ value: T , forKey key: Key ) throws {
425
439
self . encoder. codingPath. append ( key)
426
440
defer { self . encoder. codingPath. removeLast ( ) }
427
441
#if DEPLOYMENT_RUNTIME_SWIFT
428
- self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = try self . encoder. box ( value)
442
+ self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = try self . encoder. box ( value)
429
443
#else
430
- self . container [ key. stringValue] = try self . encoder. box ( value)
444
+ self . container [ _converted ( key) . stringValue] = try self . encoder. box ( value)
431
445
#endif
432
446
}
433
447
434
448
public mutating func nestedContainer< NestedKey> ( keyedBy keyType: NestedKey . Type , forKey key: Key ) -> KeyedEncodingContainer < NestedKey > {
435
449
let dictionary = NSMutableDictionary ( )
436
450
#if DEPLOYMENT_RUNTIME_SWIFT
437
- self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = dictionary
451
+ self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = dictionary
438
452
#else
439
- self . container [ key. stringValue] = dictionary
453
+ self . container [ _converted ( key) . stringValue] = dictionary
440
454
#endif
441
455
442
456
self . codingPath. append ( key)
@@ -449,9 +463,9 @@ fileprivate struct _JSONKeyedEncodingContainer<K : CodingKey> : KeyedEncodingCon
449
463
public mutating func nestedUnkeyedContainer( forKey key: Key ) -> UnkeyedEncodingContainer {
450
464
let array = NSMutableArray ( )
451
465
#if DEPLOYMENT_RUNTIME_SWIFT
452
- self . container [ key. stringValue. _bridgeToObjectiveC ( ) ] = array
466
+ self . container [ _converted ( key) . stringValue. _bridgeToObjectiveC ( ) ] = array
453
467
#else
454
- self . container [ key. stringValue] = array
468
+ self . container [ _converted ( key) . stringValue] = array
455
469
#endif
456
470
457
471
self . codingPath. append ( key)
0 commit comments