Skip to content
Open
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
18 changes: 10 additions & 8 deletions Sources/FoundationEssentials/JSON/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1184,14 +1184,16 @@ private extension __JSONEncoder {
}
}

func wrap(_ dict: [String : Encodable], for additionalKey: (some CodingKey)? = _CodingKey?.none) throws -> JSONEncoderValue? {
func wrap(_ dict: _JSONCodingKeyRepresentableDictionaryEncodableMarker, for additionalKey: (some CodingKey)? = _CodingKey?.none) throws -> JSONEncoderValue? {
let dict = dict as! [AnyHashable: Encodable]
var result = [String: JSONEncoderValue]()
result.reserveCapacity(dict.count)

let encoder = __JSONEncoder(options: self.options, ownerEncoder: self)
for (key, value) in dict {
encoder.codingKey = _CodingKey(stringValue: key)
result[key] = try encoder.wrap(value)
let stringKey = (key.base as! CodingKeyRepresentable).codingKey.stringValue
encoder.codingKey = _CodingKey(stringValue: stringKey)
result[stringKey] = try encoder.wrap(value)
}

return .object(result)
Expand All @@ -1214,8 +1216,8 @@ 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 {
return try self.wrap(encodable as! [String:Encodable], for: additionalKey)
} else if let encodable = value as? _JSONCodingKeyRepresentableDictionaryEncodableMarker {
return try self.wrap(encodable, for: additionalKey)
} else if let array = value as? _JSONDirectArrayEncodable {
if options.outputFormatting.contains(.prettyPrinted) {
let (bytes, lengths) = try array.individualElementRepresentation(encoder: self, additionalKey)
Expand Down Expand Up @@ -1362,11 +1364,11 @@ extension JSONEncoder : @unchecked Sendable {}
// Special-casing Support
//===----------------------------------------------------------------------===//

/// A marker protocol used to determine whether a value is a `String`-keyed `Dictionary`
/// A marker protocol used to determine whether a value is a `CodingKeyRepresentable`-keyed `Dictionary`
/// containing `Encodable` values (in which case it should be exempt from key conversion strategies).
private protocol _JSONStringDictionaryEncodableMarker { }
private protocol _JSONCodingKeyRepresentableDictionaryEncodableMarker { }

extension Dictionary : _JSONStringDictionaryEncodableMarker where Key == String, Value: Encodable { }
extension Dictionary : _JSONCodingKeyRepresentableDictionaryEncodableMarker where Key: CodingKeyRepresentable, Value: Encodable { }

/// A protocol used to determine whether a value is an `Array` containing values that allow
/// us to bypass UnkeyedEncodingContainer overhead by directly encoding the contents as
Expand Down
16 changes: 16 additions & 0 deletions Tests/FoundationEssentialsTests/JSONEncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2333,6 +2333,22 @@ extension JSONEncoderTests {

#expect(expected == resultString)
}

@Test func encodingDictionaryCodingKeyRepresentableKeyConversionUntouched() throws {
struct Key: RawRepresentable, CodingKeyRepresentable, Hashable, Codable {
let rawValue: String
}

let expected = "{\"leaveMeAlone\":\"test\"}"
let toEncode: [Key: String] = [Key(rawValue: "leaveMeAlone"): "test"]

let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
let resultData = try encoder.encode(toEncode)
let resultString = String(bytes: resultData, encoding: .utf8)

#expect(expected == resultString)
}

@Test func keyStrategySnakeGeneratedAndCustom() throws {
// Test that this works with a struct that has automatically generated keys
Expand Down