Skip to content

Commit 1152aa4

Browse files
committed
Improve performance of JSONDecoder and JSONEncoder for large apps
1 parent 4e01366 commit 1152aa4

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Sources/FoundationEssentials/JSON/JSONDecoder.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,13 @@ extension JSONDecoderImpl: Decoder {
610610
if type == Decimal.self {
611611
return try self.unwrapDecimal(from: mapValue, for: codingPathNode, additionalKey) as! T
612612
}
613-
if T.self is _JSONStringDictionaryDecodableMarker.Type {
614-
return try self.unwrapDictionary(from: mapValue, as: type, for: codingPathNode, additionalKey)
613+
switch options.keyDecodingStrategy {
614+
case .useDefaultKeys:
615+
break
616+
case .convertFromSnakeCase, .custom:
617+
if T.self is _JSONStringDictionaryDecodableMarker.Type {
618+
return try self.unwrapDictionary(from: mapValue, as: type, for: codingPathNode, additionalKey)
619+
}
615620
}
616621

617622
return try self.with(value: mapValue, path: codingPathNode.appending(additionalKey)) {

Sources/FoundationEssentials/JSON/JSONEncoder.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1214,7 +1214,7 @@ private extension __JSONEncoder {
12141214
return self.wrap(url.absoluteString)
12151215
} else if let decimal = value as? Decimal {
12161216
return .number(decimal.description)
1217-
} else if let encodable = value as? _JSONStringDictionaryEncodableMarker {
1217+
} else if !options.keyEncodingStrategy.isDefault, let encodable = value as? _JSONStringDictionaryEncodableMarker {
12181218
return try self.wrap(encodable as! [String:Encodable], for: additionalKey)
12191219
} else if let array = value as? _JSONDirectArrayEncodable {
12201220
if options.outputFormatting.contains(.prettyPrinted) {
@@ -1466,3 +1466,14 @@ extension Array : _JSONDirectArrayEncodable where Element: _JSONSimpleValueArray
14661466
return (writer.bytes, lengths: byteLengths)
14671467
}
14681468
}
1469+
1470+
private extension JSONEncoder.KeyEncodingStrategy {
1471+
var isDefault: Bool {
1472+
switch self {
1473+
case .useDefaultKeys:
1474+
return true
1475+
case .custom, .convertToSnakeCase:
1476+
return false
1477+
}
1478+
}
1479+
}

0 commit comments

Comments
 (0)