Hi,
When a value "apple_id"
needs to be transformed into "appleId"
or vice versa.
When you want to do the manual transformation of Dictionary string keys or string values from camelCase to snake case or vice versa, you need to copy existing base code in your app or reinventing the weel.
In the Swift Standard Library, these functions exist and the tests are written for both implementations with specific JSON focus in mind.
The Encoder and Decoder have encoding and decoding strategies for JSON where keys can be transformed during the encoding process or decoding process.
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let encoder = JSONEncoder()
encoder.keyEncodingStrategy = .convertToSnakeCase
test/stdlib/TestJSONEncoder.swift
stdlib/public/Darwin/Foundation/JSONEncoder.swift
fileprivate static func _convertToSnakeCase(_ stringKey: String) -> String
fileprivate static func _convertFromSnakeCase(_ stringKey: String) -> String
This functionality is private.
Can we make it public?
Some projects just copy-pasting the implementation from Swift Standard Library ex:
Sources/SQLKit/SQLRowDecoder.swift