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.uppercaseLetters and CharacterSet.lowercaseLetters (Unicode General Categories Lu and Lt). The conversion to lower case uses Locale.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:

    1. Splits words at the boundary of lower-case to upper-case
    2. Inserts _ between words
    3. Lowercases the entire string
    4. Preserves starting and ending _.

    For example, oneTwoThree becomes one_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 example oneTwoThree becomes one-two-three.

    Declaration

    Swift

    case convertToKebabCase
  • Capitalize the first letter only. For example oneTwoThree becomes OneTwoThree.

    Declaration

    Swift

    case capitalized
  • Uppercase all letters. For example oneTwoThree becomes ONETWOTHREE.

    Declaration

    Swift

    case uppercased
  • Lowercase all letters. For example oneTwoThree becomes onetwothree.

    Declaration

    Swift

    case lowercased
  • A custom encoding using the provided closure.

    Declaration

    Swift

    case custom((String) -> String)