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 implementationDeserializes
jsonObjectinto a value of typeDeserialized.This method is provided so overriding all deserialization doesn’t require overriding all the different
deserializemethods.Default Implementation
Calls the
deserializedmethod corresponding tojsonObject‘s variant.Override this method if your deserializer doesn’t depend on the
jsonObject’s type. Otherwise, you probably want to override the specializeddeserializemethod(s).For an example of the first, see
JSONObject.Declaration
Swift
static func deserialize(jsonObject: JSONParsedObject) throws -> Deserialized -
deserialize(array:)Default implementationDeserializes
arrayinto a value of typeDeserialized.Default Implementation
Throws
JSONError.unexpectedType.Declaration
Swift
static func deserialize(array: NSArray) throws -> Deserialized -
deserialize(dictionary:)Default implementationDeserializes
dictionaryinto a value of typeDeserialized.Default Implementation
Throws
JSONError.unexpectedType.Declaration
Swift
static func deserialize(dictionary: NSDictionary) throws -> Deserialized -
deserialize(null:)Default implementationDeserializes
nullinto a value of typeDeserialized.Default Implementation
Throws
JSONError.unexpectedType.Declaration
Swift
static func deserialize(null: NSNull) throws -> Deserialized -
deserialize(number:)Default implementationDeserializes
numberinto a value of typeDeserialized.Default Implementation
Throws
JSONError.unexpectedType.Declaration
Swift
static func deserialize(number: NSNumber) throws -> Deserialized -
deserialize(string:)Default implementationDeserializes
stringinto a value of typeDeserialized.Default Implementation
Throws
JSONError.unexpectedType.Declaration
Swift
static func deserialize(string: NSString) throws -> Deserialized
-
parseJSON(_:)Extension methodParse
datausingJSONSerialization.jsonObject(with:options:)and then callsdeserialize(_:)on the resulting object.JSONSerialization.jsonObject(with:options:)is called with theallowFragmentsoption. This should never be a problem thanks to the type checking that occurs, and should you need it, you have it.Throws
JSONError.invalidJSONwhenJSONSerialization.jsonObject(with:options:)fails. Letsdeserialize(_:)‘s errors go through.Declaration
Swift
static func parseJSON(_ data: Data) throws -> Deserialized -
parseJSON(_:)Extension methodEncodes the string as UTF-8 and calls
parseJSONwith that data.Throws
JSONError.invalidJSONwhenJSONSerialization.jsonObject(with:options:)fails. LetsparseJSON(_:)‘s errors go through.Declaration
Swift
static func parseJSON(_ string: String) throws -> Deserialized -
deserialize(_:)Extension methodChecks
objectis a type returned byJSONSerialization.jsonObject(with:options:), and callsdeserialize(jsonObject:).The object casted and wrapped in
JSONParsedObject, this prevents from polluting the interface withAny.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 -
deserialize(key:of:orDefault:)Extension methodConvenience 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.missingValueif the requested key was not found and no default was provided. Also letsT.deserialize‘s errors go through.Declaration
Swift
static func deserialize(key: String, of dictionary: NSDictionary, orDefault def: Deserialized? = nil) throws -> Deserialized -
deserialize(keyAt:of:orDefault:)Extension methodConvenience 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.missingValueif the requested key was not found and no default was provided. Also letsT.deserialize‘s errors go through.Declaration
Swift
static func deserialize(keyAt path: [String], of dictionary: NSDictionary, orDefault def: Deserialized? = nil) throws -> Deserialized
View on GitHub
Install in Dash
JSONDeserializer Protocol Reference