JsonHelper

public struct JsonHelper

Helps to convert dictionaries/arrays with custom objects to a valid JSON string. (and back)

Author

FelixSFD
  • Encodes a [String: Any] to a valid JSON-string.

    Throws

    An error if the decoding failed

    Author

    FelixSFD

    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

    Author

    FelixSFD

    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

    Author

    FelixSFD

    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

    Author

    FelixSFD

    Declaration

    Swift

    public static func decode(jsonString json: String) throws -> Any

    Parameters

    json

    The JSON-string

    Return Value

    The decoded object