Support for String transformation from camelCase to snake case and vice versa

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

6 Likes

It would be great to move these transforms onto String in the standard library, as they're useful for more than encoders and decoders. Even better if the feature could be generalized into a performant String transform API.

5 Likes
Terms of Service

Privacy Policy

Cookie Policy