Encodable

public extension Encodable
  • Converts the encodable object into a dictionary with String keys and CustomStringConvertible values.

    Discussion: The asDictionary property is an extension on Encodable that converts an encodable object into a dictionary representation. The resulting dictionary has string keys and values that conform to the CustomStringConvertible protocol.

    The property uses JSON encoding and serialization to convert the encodable object into a dictionary. It first encodes the object using the epiJSONEncoder from JSONEncoder. Then, it deserializes the encoded data into a dictionary using JSONSerialization.

    The resulting dictionary is then transformed to ensure that all values conform to CustomStringConvertible.

    • If a value is already CustomStringConvertible, it is directly added to the dictionary.
    • If a value is encodable, it recursively converts it to a dictionary using the asDictionary property.
    • If a value cannot be converted to a String, its description is used instead.

    Declaration

    Swift

    var asDictionary: [String : CustomStringConvertible]? { get }