KeyEncoding
public enum KeyEncoding
Encoding to use for keys.
This type is derived from JSONEncoder‘s KeyEncodingStrategy
and XMLEncoders KeyEncodingStrategy.
-
Use the keys specified by each type. This is the default encoding.
Declaration
Swift
case useDefaultKeys -
Convert from “camelCaseKeys” to “snake_case_keys” before writing a key.
Capital characters are determined by testing membership in
CharacterSet.uppercaseLettersandCharacterSet.lowercaseLetters(Unicode General Categories Lu and Lt). The conversion to lower case usesLocale.system, also known as the ICU “root” locale. This means the result is consistent regardless of the current user’s locale and language preferences.Converting from camel case to snake case:
- Splits words at the boundary of lower-case to upper-case
- Inserts
_between words - Lowercases the entire string
- Preserves starting and ending
_.
For example,
oneTwoThreebecomesone_two_three._oneTwoThree_becomes_one_two_three_.Note
Using a key encoding strategy has a nominal performance cost, as each string key has to be converted.Declaration
Swift
case convertToSnakeCase -
Same as convertToSnakeCase, but using
-instead of_. For exampleoneTwoThreebecomesone-two-three.Declaration
Swift
case convertToKebabCase -
Capitalize the first letter only. For example
oneTwoThreebecomesOneTwoThree.Declaration
Swift
case capitalized -
Uppercase all letters. For example
oneTwoThreebecomesONETWOTHREE.Declaration
Swift
case uppercased -
Lowercase all letters. For example
oneTwoThreebecomesonetwothree.Declaration
Swift
case lowercased -
A custom encoding using the provided closure.
Declaration
Swift
case custom((String) -> String)
View on GitHub
Install in Dash