JSONDeserializer

public protocol JSONDeserializer

A protocol for types that can deserialize JSON.

All methods have default implementations. See JSONDeserializer‘s extensions for details.

  • The output type for this deserializer.

    Declaration

    Swift

    associatedtype Deserialized
  • deserialize(jsonObject:) Default implementation

    Deserializes jsonObject into a value of type Deserialized.

    This method is provided so overriding all deserialization doesn’t require overriding all the different deserialize methods.

    Default Implementation

    Calls the deserialized method corresponding to jsonObject‘s variant.

    Override this method if your deserializer doesn’t depend on the jsonObject’s type. Otherwise, you probably want to override the specialized deserialize method(s).

    For an example of the first, see JSONObject.

    Declaration

    Swift

    static func deserialize(jsonObject: JSONParsedObject) throws -> Deserialized
  • deserialize(array:) Default implementation

    Deserializes array into a value of type Deserialized.

    Default Implementation

    Throws

    JSONError.unexpectedType.

    Declaration

    Swift

    static func deserialize(array: NSArray) throws -> Deserialized
  • deserialize(dictionary:) Default implementation

    Deserializes dictionary into a value of type Deserialized.

    Default Implementation

    Throws

    JSONError.unexpectedType.

    Declaration

    Swift

    static func deserialize(dictionary: NSDictionary) throws -> Deserialized
  • deserialize(null:) Default implementation

    Deserializes null into a value of type Deserialized.

    Default Implementation

    Throws

    JSONError.unexpectedType.

    Declaration

    Swift

    static func deserialize(null: NSNull) throws -> Deserialized
  • deserialize(number:) Default implementation

    Deserializes number into a value of type Deserialized.

    Default Implementation

    Throws

    JSONError.unexpectedType.

    Declaration

    Swift

    static func deserialize(number: NSNumber) throws -> Deserialized
  • deserialize(string:) Default implementation

    Deserializes string into a value of type Deserialized.

    Default Implementation

    Throws

    JSONError.unexpectedType.

    Declaration

    Swift

    static func deserialize(string: NSString) throws -> Deserialized
  • parseJSON(_:) Extension method

    Parse data using JSONSerialization.jsonObject(with:options:) and then calls deserialize(_:) on the resulting object.

    JSONSerialization.jsonObject(with:options:) is called with the allowFragments option. This should never be a problem thanks to the type checking that occurs, and should you need it, you have it.

    Throws

    JSONError.invalidJSON when JSONSerialization.jsonObject(with:options:) fails. Lets deserialize(_:)‘s errors go through.

    Declaration

    Swift

    static func parseJSON(_ data: Data) throws -> Deserialized
  • parseJSON(_:) Extension method

    Encodes the string as UTF-8 and calls parseJSON with that data.

    Throws

    JSONError.invalidJSON when JSONSerialization.jsonObject(with:options:) fails. Lets parseJSON(_:)‘s errors go through.

    Declaration

    Swift

    static func parseJSON(_ string: String) throws -> Deserialized
  • deserialize(_:) Extension method

    Checks object is a type returned by JSONSerialization.jsonObject(with:options:), and calls deserialize(jsonObject:).

    The object casted and wrapped in JSONParsedObject, this prevents from polluting the interface with Any.

    If you’re thinking about replacing this method, you should replace deserialize(jsonObject:) instead:

    • as with this method, all deserialization goes through it
    • you keep this method’s sanity checks
    • you get extra information about the object (if you want the raw object, use JSONParsedObject.inner)

    Declaration

    Swift

    static final func deserialize(_ object: Any) throws -> Deserialized
  • Convenience function to deserialize a key from a dictionary.

    Using this function has the advantage of producing a nicer error when the key is missing.

    Throws

    JSONError.missingValue if the requested key was not found and no default was provided. Also lets T.deserialize‘s errors go through.

    Declaration

    Swift

    static func deserialize(key: String,
                                of dictionary: NSDictionary,
                                orDefault def: Deserialized? = nil) throws -> Deserialized
  • Convenience function to deserialize a key from a nested dictionary.

    Using this function has the advantage of producing a nicer error when the key is missing.

    Throws

    JSONError.missingValue if the requested key was not found and no default was provided. Also lets T.deserialize‘s errors go through.

    Declaration

    Swift

    static func deserialize(keyAt path: [String],
                                of dictionary: NSDictionary,
                                orDefault def: Deserialized? = nil) throws -> Deserialized