Enumerations

The following enumerations are available globally.

  • A representation of ambigious JSON.

    Heterogeneous Arrays.

    Take for exmaple the JSON below:

    {
        "users": [
            {
                "age": 18,
                "name": {
                    "first": "Caleb",
                    "last": "Kleveter"
                }
            }
        ],
        "metadata": [42, 3.14, true, "fizz buzz"]
    }
    

    The users data is pretty normal, but the metadata array can’t be represented by standard arrays.

    The metadata array, when decoded to a JSON instance, will result in this case:

    JSON.array([.number(.int(42)), .number(.double(3.14)), .bool(true), .string("fizz buzz")])
    

    Value Unwrapping

    Getting the associated value from an enum case can be a real pain, so there are properties for each case to unwrap the value.

    If you have the example JSON above, it is an .object case. You can use the .object property to get the [String: JSON] value it wraps:

    json.object // [String: JSON]?
    

    There are also properties for .null, .string, .bool, .int, .float, .double, and .array. Be sure to read the docs for the .null property.

    These properties also have setters. They let you set the value of the current JSON case. They will set the case regardless the current type, so if the case is a .bool, you can use .string and the case will be changed to a .string case.

    var json = JSON.bool(true)
    json.string = "Fizz Buzz"
    print(json) // "\"Fizz Buzz\""
    

    Dynamic Access

    JSON supports @dynamicMemberLookup, so it is really easy to access values your JSON objects/arrays:

    let firstname = json.users.0.name.first.string
    

    This also works for setting JSON values:

    json.users.0.name.first = "Tanner"
    
    See more

    Declaration

    Swift

    @dynamicMemberLookup
    public enum JSON : Hashable, CustomStringConvertible
  • A wrapper for standard numeric types.

    See more

    Declaration

    Swift

    public enum Number : Codable, Hashable, CustomStringConvertible