KeyEncoding
public enum KeyEncoding
Encoding to use for keys.
This type is derived from JSONEncoder
‘s KeyEncodingStrategy
and XMLEncoder
s 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.uppercaseLetters
andCharacterSet.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,
oneTwoThree
becomesone_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 exampleoneTwoThree
becomesone-two-three
.Declaration
Swift
case convertToKebabCase
-
Capitalize the first letter only. For example
oneTwoThree
becomesOneTwoThree
.Declaration
Swift
case capitalized
-
Uppercase all letters. For example
oneTwoThree
becomesONETWOTHREE
.Declaration
Swift
case uppercased
-
Lowercase all letters. For example
oneTwoThree
becomesonetwothree
.Declaration
Swift
case lowercased
-
A custom encoding using the provided closure.
Declaration
Swift
case custom((String) -> String)