DataObject
public struct DataObject : ExpressibleByDictionaryLiteral
extension DataObject: Codable
extension DataObject: DataItemExtractor
extension DataObject: DataInputConvertible
extension DataObject: CustomStringConvertible
extension DataObject: Equatable
A custom wrapper around a common Dictionary used to limit the DataInput types into the Dictionary.
Only valid DataInput types will be stored in the inner Dictionary and
only DataInputConvertible types can be used to initialize this wrapper object.
-
Declaration
Swift
public var count: Int { get }Return Value
The number of
DataItemstored in thisDataObject. -
Creates a
DataObjectfrom a list of(String, DataInputConvertible)tuple.The first element in the tuple is the key that can be used to access the item back, the second element is a convertible that will be converted immediately before being stored. In case of duplicate keys the second occurrence of the same key will replace the first one.
Declaration
Swift
public init(pairs elements: [(String, DataInputConvertible)]) -
Creates a
DataObjectfrom a[String: DataInputConvertible]dictionary literal.The Convertible elements will be converted immediately before being stored. In case of duplicate keys the second occurrence of the same key will replace the first one.
Declaration
Swift
public init(dictionaryLiteral elements: (String, DataInputConvertible)...) -
Creates a
DataObjectfrom a[String: DataInputConvertible]dictionary.The Convertible elements will be converted immediately before being stored.
Declaration
Swift
public init(dictionary: [String : DataInputConvertible] = [:]) -
Creates a
DataObjectfrom a[String: DataInputConvertible?]dictionary, omitting any keys whose value isnil.Use this initializer whenever one or more root values may be optional. The dictionary literal syntax requires non-optional
DataInputConvertiblevalues and will not compile when optional values are present; this initializer accepts optionals and stripsnilentries so only explicitly set values appear in the result.// ✅ Compiles and omits keys whose value is nil DataObject(compacting: ["key": optionalValue]) // ❌ Does not compile when optionalValue is optional ["key": optionalValue] as DataObjectThe remaining (non-nil) convertible elements are converted immediately before being stored.
Declaration
Swift
public init(compacting dictionary: [String : DataInputConvertible?] = [:]) -
Creates a DataObject from a dictionary of DataInput values.
Declaration
Swift
public init(dictionaryInput: [String : DataInput])Parameters
dictionaryInputThe dictionary containing DataInput values.
-
Creates a DataObject from a JSON serializable Dictionary.
This method performs a JSON encoding round-trip to validate and convert the input.
Warning
Non conforming floats like
Double.nanorFloat.infinitywill be silently converted to strings “NaN” and “Infinity” (or “-Infinity” for negative “Infinity”) immediately by this function. Dates will be converted to Strings in the following format: yyyy-MM-dd’T'HH:mm:ss'Z’.Throws
A
JSONParsingErrorin case of non serializable objects.Declaration
Swift
public init(jsonObject: [String : Any]) throws(JSONParsingError)Parameters
jsonObjectThe dictionary representing a JSON serializable object
-
Sets the value at the given key
Declaration
Swift
public mutating func set(_ value: DataInput, key: String) -
Sets the convertible value at the given key after converting it.
Declaration
Swift
public mutating func set(converting convertible: DataInputConvertible, key: String) -
Removes the value stored at the given key.
Declaration
Swift
public mutating func removeValue(forKey key: String) -
Returns the underlying dictionary of
[String: DataInput].Declaration
Swift
public func asDictionary() -> [String : DataInput] -
Sets the item in the
DataObjectby following theJSONObjectPathand recursively creating the required containers.In case the
DataObjectcontains already the nested object or array as expressed in theJSONObjectPathit will insert the new item in those objects or arrays. The missing containers will, instead, be automatically be created by this method. In case an array is not big enough to insert an item at the given index, nil items will be put until we reach the required capacity.Declaration
Swift
public mutating func buildPath(_ path: JSONObjectPath, andSet item: DataItem)Parameters
pathThe
JSONObjectPaththat expresses the (eventually nested) location in which to put the itemitemThe item to insert at the provided location
-
Creates a
DataObjectby deserializing a JSONStringrepresentation of a dictionary.Throws
AJSONParsingError.jsonIsNotADictionaryif the top-level JSON value is not a dictionary, or aJSONParsingError.invalidJSONifjsonStringis not valid JSON.Declaration
Swift
init(jsonString: String) throws(JSONParsingError)Parameters
jsonStringA JSON-encoded string whose top-level value must be an object (e.g.
"{\"key\": \"value\"}"). -
Declaration
Swift
public init(from decoder: Decoder) throws -
Declaration
Swift
public func encode(to encoder: Encoder) throws -
Declaration
Swift
public func getDataItem(key: String) -> DataItem? -
Declaration
Swift
public func toDataInput() -> any DataInput -
Serializes the data object to a JSON string.
Declaration
Swift
func serialize() throws -> StringReturn Value
A UTF-8 JSON string representation of the data object.
-
Declaration
Swift
public var description: String { get } -
Declaration
Swift
public static func == (lhs: DataObject, rhs: DataObject) -> Bool
View on GitHub