Map
public class Map
A wrapper around a JSON object that conveniently returns parsed objects or adds complex objects into the JSON object.
Basic example of returning objects:
let jsonString = // Source of JSON string
let map = Map(jsonString: jsonString, encoding: .utf8)
let string: String = try map.value("some_key")
Basic example of adding objects:
let map = Map()
map.add("some string", for: "some_key")
let jsonString = map.jsonString(encoding: .utf8)
-
Initialize this map from a JSON dictionary. You should only pass
MapPrimitive(JSON) values in this dictionary.Declaration
Swift
public init(json: [String: Any?] = [:]) -
Initialize this
Mapfrom aMapEncodableobject using itsfillmethod.Declaration
Swift
public convenience init(_ mapEncodable: MapEncodable) throws -
Initialize this object from a JSON
String.Throws
Throws an error if the JSON string cannot be deserialized.Declaration
Swift
public convenience init(jsonString: String, encoding: String.Encoding = .utf8) throwsParameters
jsonStringThe JSON
Stringthat will be deserialized.encodingThe encoding used on the string.
-
Initialize this object from a JSON
String.Throws
Throws an error if the JSON string cannot be deserialized.Declaration
Swift
public convenience init(jsonData: Data, encoding: String.Encoding = .utf8) throwsParameters
jsonStringThe JSON
Stringthat will be deserialized.encodingThe encoding used on the string.
-
Create a
MapArrayfrom a JSONStringThrows
Throws an error if the JSON string cannot be deserialized.Declaration
Swift
public static func parseArray(jsonString: String, encoding: String.Encoding = .utf8) throws -> [Map]Parameters
jsonStringThe JSON
Stringthat will be deserializedencodingThe encoding used on the string
-
Create a
MapArrayfrom a JSONDataThrows
Throws an error if the JSON string cannot be deserialized.Declaration
Swift
public static func parseArray(jsonData: Data) throws -> [Map]Parameters
jsonDataThe JSON
Datathat will be deserializedencodingThe encoding used on the string
-
Returns the JSON dictionary representation of this map.
Declaration
Swift
public func makeDictionary() -> [String : Any?]Return Value
A dictionary object representing this map.
-
Serializes this object into a JSON
Dataobject.Throws
Throws an error if this object failed to serialize.Declaration
Swift
public func jsonData(options: JSONSerialization.WritingOptions = []) throws -> DataParameters
optionsThe writing options.
Return Value
The serialized object.
-
Serializes this object into a JSON
String.Throws
Throws an error if this object failed to serialize.Declaration
Swift
public func jsonString(options: JSONSerialization.WritingOptions = [], encoding: String.Encoding = .utf8) throws -> String?Parameters
optionsThe writing options.
encodingThe string encoding that should be used.
Return Value
The serialized object.
-
Add a value to the map. The object added should always be a
MapPrimitive(JSON value) otherwise parsing this object into JSON will result in a runtime exception.Declaration
Swift
public func add(_ value: Any?, for key: MapKey) throwsParameters
valueThe value that will be stored in the map.
keyThe key that will be used to store this value and that can be used to retrive this value.
-
Returns a value from the map. All values returned will always conform to
MapPrimitiveunless the add method was used incorrectly.Declaration
Swift
public func value(from key: MapKey) throws -> Any?Parameters
keyThe key that that is used to store this value in the map.
Return Value
The stored object.
-
Add a value to the map encoding it using the provided
MapEncoder.Declaration
Swift
public func add<T>(_ value: T.Object?, for key: MapKey, using encoder: T) throws where T : MapEncoderParameters
valueThe value that will be stored in this map.
keyThe JSON key that will be used for the JSON object.
encoderThe encoder that will be used to serialize the object.
-
Returns a value from the map. The value will be decoded it using the provided
MapDecoder.Throws
Throws an error if the value could not be deserialized or if it is nil.Declaration
Swift
public func value<T>(from key: MapKey, using decoder: T) throws -> T.Object where T : MapDecoderParameters
keyThe JSON key for the object that will be deserialized.
Return Value
The deserialized object.
-
Add a
MapPrimitivevalue to the map. AMapPrimitiveis any value supported by a JSON object.MapPrimitivevalues includes (but not limited to):StringIntBoolDouble[T]where T also conforms toMapPrimitive[String: T]where T also conforms toMapPrimitive
Declaration
Swift
public func add<T>(_ value: T?, for key: MapKey) throws where T : MapPrimitiveParameters
valueThe value that will be stored in the map.
keyThe JSON key that will be used for the JSON object.
-
Returns a value from the map if it conforms to the specified
MapPrimitivetype.Throws
Throws an error if the value could not be deserialized or it is nil.Declaration
Swift
public func value<T>(from key: MapKey) throws -> T where T : MapPrimitiveParameters
keyThe JSON key for the primitive that will be returned.
Return Value
The deserialized object.
-
Returns a set of values from the map if they all conform to the specified
MapPrimitivetype.Throws
Throws an error if the value could not be deserialized or it is nil.Declaration
Swift
public func value<T>(from key: MapKey) throws -> Set<T> where T : MapPrimitive, T : HashableParameters
keyThe JSON key for the array that will be deserialized.
Return Value
The deserialized object.
-
Add a value to the map. The object will be converted to its raw type.
Declaration
Swift
public func add<T>(_ value: T?, for key: MapKey) throws where T : RawRepresentableParameters
valueThe value that will be stored in the map.
keyThe JSON key that will be used for the JSON object.
-
Returns a value from the map. The object will be converted from its RawType into the .
Throws
Throws an error if the value could not be converted to the specified type or it is nil.Declaration
Swift
public func value<T>(from key: MapKey) throws -> T where T : RawRepresentableParameters
keyThe JSON key for the object that will be deserialized.
Return Value
The deserialized object.
-
Add a value to the map
Declaration
Swift
public func add<T>(_ value: [T]?, for key: MapKey) throws where T : RawRepresentableParameters
valueThe value that will be stored in the map. Will be converted to its RawType.
keyThe JSON key that will be used for the JSON object.
-
Returns a value from the map
Throws
Throws an error if the value is invalid or it is nil. Filters out any enums that don’t serialize properly instead of failing.Declaration
Swift
public func value<T>(from key: MapKey) throws -> [T] where T : RawRepresentableParameters
keyThe JSON key for the array that will be deserialized.
Return Value
The deserialized object.
-
Add a dictionary of the RawRepresentable types converted to JSON primatives.
Declaration
Swift
public func add<T>(_ value: [String : T]?, for key: MapKey) throws where T : RawRepresentableParameters
valueThe value that will be stored in the map. Will be converted to its RawType.
keyThe JSON key that will be used for the JSON object.
-
Returns a dictionary of the specified
RawRepresentabletypes.Throws
Throws an error if the value is invalid or it is nil. Filters out any enums that don’t serialize properly instead of failing.Declaration
Swift
public func value<T>(from key: MapKey) throws -> [String : T] where T : RawRepresentableParameters
keyThe JSON key for the dictionary that will be deserialized.
Return Value
The deserialized object.
-
Add a value to the map. The object will be converted to a JSON primative.
Declaration
Swift
public func add<T>(_ value: Set<T>?, for key: MapKey) throws where T : Hashable, T : RawRepresentableParameters
valueThe value that will be stored in the map. Will be converted to its RawType.
keyThe JSON key that will be used for the JSON object.
-
Returns a set of objects converted to the specified RawRepresentable type.
Throws
Throws an error if the value is invalid or it is nil. Filters out any enums that don’t serialize properly instead of failing.Declaration
Swift
public func value<T>(from key: MapKey) throws -> Set<T> where T : Hashable, T : RawRepresentableParameters
keyThe JSON key for the array that will be deserialized.
Return Value
The deserialized object.
-
Add a value to the map.
Declaration
Swift
public func add<T>(encodable: T?, for key: MapKey) throws where T : EncodableParameters
valuethe nested
Encodableobject that will be serialized to JSON be stored in the map.keyThe JSON key that will be used for the JSON object.
-
Returns a
Decodablevalue from the map.Throws
Throws an error if the value could not be decoded or if it is nil.Declaration
Swift
public func decodable<T>(from key: MapKey) throws -> T where T : DecodableParameters
keyThe JSON key for the object that will be deserialized.
Return Value
The decoded object.
-
Add a value to the map. The object will be serialized to JSON.
Declaration
Swift
public func add<T>(_ encodable: T?, for key: MapKey) throws where T : MapEncodableParameters
valuethe nested
MapEncodableobject that will be stored in the map.keyThe JSON key that will be used for the JSON object.
-
Returns a value from the map. The object will be converted from JSON..
Throws
Throws an error if the value could not be deserialized or if it is nil.Declaration
Swift
public func value<T>(from key: MapKey) throws -> T where T : MapDecodableParameters
keyThe JSON key for the object that will be deserialized.
Return Value
The deserialized object.
-
Add an array to the map. The objects will be converted by using their mapping function.
Declaration
Swift
public func add<T>(_ encodableArray: [T], for key: MapKey) throws where T : MapEncodableParameters
valueThe nested
MapEncodablearray that will be stored in the map.keyThe JSON key that will be used for the JSON object.
-
Returns a value from the map. Deserializes the object from
[[String: Any?]]`. The object will be converted by using its mapping function.Throws
Throws an error if the value could not be deserialized or it is nil and no default value was specified.Declaration
Swift
public func value<T: MapDecodable>(from key: MapKey, stopOnFailure: Bool = false) throws -> [T]Parameters
keyThe JSON key for the array of objects that will be deserialized.
Return Value
The deserialized object.
-
Add an array to the map. The objects will be converted by using their mapping function.
Declaration
Swift
public func add<T>(_ values: [String : T], for key: MapKey) throws where T : MapEncodableParameters
valueThe nested
MapEncodablearray that will be stored in the map.keyThe JSON key that will be used for the JSON object.
-
Returns a value from the map. Deserializes the object from
[String: [String: Any?]]. The object will be converted by using its mapping function.Throws
Throws an error if the value could not be deserialized or it is nil.Declaration
Swift
public func value<T: MapDecodable>(from key: MapKey, stopOnFailure: Bool = false) throws -> [String: T]Parameters
keyThe JSON key for the dictionary of objects that will be deserialized.
Return Value
The deserialized object.
View on GitHub
Map Class Reference