SmartEncodable

public protocol SmartEncodable : Encodable

Undocumented

SmartDecodable

  • toDictionary(options:) Extension method

    Serializes into a dictionary

    • Example:

      struct Model: SmartCodable {
        var data: String
        static func mappingForKey() -> [SmartKeyTransformer]? {
            [CodingKeys.data <--- ["json_data", "alt_data"]]
        }
      }
      

    let model = Model(data: “value”) let dict1 = model.toDictionary() // [“data”: “value”] let dict2 = model.toDictionary(useMappedKeys: true) // [“json_data”: “value”]

    Declaration

    Swift

    public func toDictionary(options: Set<SmartEncodingOption>? = nil) -> [String : Any]?

    Parameters

    useMappedKeys

    Determines whether to use source field names defined in SmartKeyTransformer during encoding.

    options

    Optional set of encoding configuration options that control serialization behavior

    Return Value

    A dictionary representation of the object, or nil if encoding fails

  • Serializes into a JSON string

    Declaration

    Swift

    public func toJSONString(options: Set<SmartEncodingOption>? = nil, prettyPrint: Bool = false) -> String?

    Parameters

    useMappedKeys

    Whether to use the mapped key during encoding. The default value is false.

    options

    encoding options

    prettyPrint

    Whether to format print (adds line breaks in the JSON)

    Return Value

    JSON string