Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/FoundationEssentials/JSON/JSONDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ extension JSONDecoderImpl: Decoder {
if type == Decimal.self {
return try self.unwrapDecimal(from: mapValue, for: codingPathNode, additionalKey) as! T
}
if T.self is _JSONStringDictionaryDecodableMarker.Type {
if !options.keyDecodingStrategy.isDefault, T.self is _JSONStringDictionaryDecodableMarker.Type {
return try self.unwrapDictionary(from: mapValue, as: type, for: codingPathNode, additionalKey)
}

Expand Down Expand Up @@ -1857,5 +1857,14 @@ extension EncodingError {
}
}

fileprivate extension JSONDecoder.KeyDecodingStrategy {
var isDefault: Bool {
switch self {
case .useDefaultKeys: true
default: false
}
}
}

@available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *)
extension JSONDecoder : @unchecked Sendable {}
51 changes: 49 additions & 2 deletions Sources/FoundationEssentials/JSON/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1214,9 +1214,9 @@ private extension __JSONEncoder {
return self.wrap(url.absoluteString)
} else if let decimal = value as? Decimal {
return .number(decimal.description)
} else if let encodable = value as? _JSONStringDictionaryEncodableMarker {
} else if !options.keyEncodingStrategy.isDefault, let encodable = value as? _JSONStringDictionaryEncodableMarker {
return try self.wrap(encodable as! [String:Encodable], for: additionalKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we able to use _specializingCast here as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, no, we can't use it here. _specializingCast internally performs type equality check, and here we use as? to check protocol conformance. so _specializingCast is inapplicable here

Copy link
Contributor Author

@ChrisBenua ChrisBenua Sep 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@inline(__always)
internal func _specializingCast<Input, Output>(_ value: Input, to type: Output.Type) -> Output? {
  guard Input.self == Output.self else { return nil } // this check will always fail when we check for protocol conformance
  return _identityCast(value, to: type)
}

} else if let array = value as? _JSONDirectArrayEncodable {
} else if let array = _asDirectArrayEncodable(value, for: additionalKey) {
if options.outputFormatting.contains(.prettyPrinted) {
let (bytes, lengths) = try array.individualElementRepresentation(encoder: self, additionalKey)
return .directArray(bytes, lengths: lengths)
Expand Down Expand Up @@ -1246,6 +1246,42 @@ private extension __JSONEncoder {
return encoder.takeValue()
}

func _asDirectArrayEncodable<T: Encodable>(_ value: T, for additionalKey: (some CodingKey)? = _CodingKey?.none) -> _JSONDirectArrayEncodable? {
return if let array = _specializingCast(array, to: [Int8].self) {
array
} else if let array = _specializingCast(array, to: [Int16].self) {
array
} else if let array = _specializingCast(array, to: [Int32].self) {
array
} else if let array = _specializingCast(array, to: [Int64].self) {
array
} else if let array = _specializingCast(array, to: [Int128].self) {
array
} else if let array = _specializingCast(array, to: [Int].self) {
array
} else if let array = _specializingCast(array, to: [UInt8].self) {
array
} else if let array = _specializingCast(array, to: [UInt16].self) {
array
} else if let array = _specializingCast(array, to: [UInt32].self) {
array
} else if let array = _specializingCast(array, to: [UInt64].self) {
array
} else if let array = _specializingCast(array, to: [UInt128].self) {
array
} else if let array = _specializingCast(array, to: [UInt].self) {
array
} else if let array = _specializingCast(array, to: [String].self) {
array
} else if let array = _specializingCast(array, to: [Float].self) {
array
} else if let array = _specializingCast(array, to: [Double].self) {
array
} else {
nil
}
}

@inline(__always)
func getEncoder(for additionalKey: CodingKey?) -> __JSONEncoder {
if let additionalKey {
Expand Down Expand Up @@ -1466,3 +1502,14 @@ extension Array : _JSONDirectArrayEncodable where Element: _JSONSimpleValueArray
return (writer.bytes, lengths: byteLengths)
}
}

fileprivate extension JSONEncoder.KeyEncodingStrategy {
var isDefault: Bool {
switch self {
case .useDefaultKeys:
return true
case .custom, .convertToSnakeCase:
return false
}
}
}