Skip to content

Case

JSONDecoder.KeyDecodingStrategy.convertFromSnakeCase

iOS 8.0+ iPadOS 8.0+ Mac Catalyst 8.0+ macOS 10.10+ tvOS 9.0+ watchOS 2.0+
Convert from “snake_case_keys” to “camelCaseKeys” before attempting to match a key with the one specified by each type.
case convertFromSnakeCase

Discussion

The conversion to upper 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 snake case to camel case:

  1. Capitalizes the word starting after each _

  2. Removes all _

  3. Preserves starting and ending _ (as these are often used to indicate private variables or other metadata). For example, one_two_three becomes oneTwoThree. _one_two_three_ becomes _oneTwoThree_.

Note

Using a key decoding strategy has a nominal performance cost, as each string key has to be inspected for the _ character.