JsonHelper
public struct JsonHelper
Helps to convert dictionaries/arrays with custom objects to a valid JSON string. (and back)
-
Encodes the contents of a dictionary recursively.
Declaration
Swift
public static func encode(object: [String: Any]) -> EncodedDictionary
-
Encodes the contents of an array recursively.
Seealso
seealso
encode(object: [String: Any])
Declaration
Swift
public static func encode(object: [Any]) -> EncodedArray
-
Encodes dictionary convertibles like
User
.Declaration
Swift
public static func encode(object: DictionaryConvertible) -> EncodedDictionary
-
Encodes objects that conform to
StringRepresentable
.Declaration
Swift
public static func encode(object: StringRepresentable) -> String
-
Returns the UNIX timestamp from the
Date
Declaration
Swift
public static func encode(object: Date) -> Int
-
Returns the absolute string of the
URL
.Declaration
Swift
public static func encode(object: URL) -> String
-
Encodes a
[String: Any]
to a valid JSON-string.Throws
An error if the decoding failed
Declaration
Swift
public static func jsonString(from dictionary: [String: Any]) throws -> String?
Parameters
dictionary
The dictionary to encode
Return Value
The JSON-string
-
Encodes a
[Any]
to a valid JSON-string.Throws
An error if the decoding failed
Declaration
Swift
public static func jsonString(from array: [Any]) throws -> String?
Parameters
array
The array to encode
Return Value
The JSON-string
-
Encodes a
DictionaryConvertible
to a valid JSON-string.Throws
An error if the decoding failed
Declaration
Swift
public static func jsonString(from object: DictionaryConvertible) throws -> String?
Parameters
object
The object to encode
Return Value
The JSON-string
-
Decodes a given JSON-string
Note
Unlike the
jsonString(from:)
method, this will not convert the String to custom objects. You will have to use their initializers.Note
Try to avoid this method. It’s easier to use the JSON-initializer of
JsonConvertible
objects.Throws
An error if the decoding failed
Declaration
Swift
public static func decode(jsonString json: String) throws -> Any
Parameters
json
The JSON-string
Return Value
The decoded object