Skip to content

Serializer

MarcoDotIO edited this page Jul 6, 2023 · 3 revisions

Serializer

A BCS (Binary Canonical Serialization) Serializer meant for serializing data

public class Serializer 

Initializers

init()

public init() 

Methods

output()

Returns the _output object.

public func output() -> Data 

Returns

Data object.

bool(_:_:)

Serialize a boolean value or an array of boolean values using a custom Serializer.

public static func bool<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single boolean or an array of booleans. The serialized boolean value is represented as UInt8 where true is encoded as 1 and false is encoded as 0.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a Bool or an array of Bools.

Throws

An AptosError object that's an invalid data value with the supported type of either Bool or [Bool], if the provided value does not match either a Bool or an array of Bools.

toBytes(_:_:)

Convert a Data value or an array of Data values into bytes using a custom Serializer.

public static func toBytes<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to convert the value as a single Data object or an array of Data objects into bytes. The bytes are appended to the Serializer's output buffer.

Parameters

  • serializer: A custom Serializer instance to be used for converting data into bytes.
  • value: A generic value conforming to EncodingContainer, which is either a Data object or an array of Data objects.

Throws

An AptosError object that's an invalid data value with the supported type of either Data or [Data], if the provided value does not match either a Data object or an array of Data objects.

_struct(_:value:)

Serialize a value conforming to the EncodingProtocol using a custom Serializer, ensuring it conforms to the KeyProtocol.

public static func _struct(_ serializer: Serializer, value: EncodingProtocol) throws 

This function takes a custom Serializer and a value conforming to the EncodingProtocol, and attempts to serialize the value by calling its serialize method. The value must also conform to the KeyProtocol.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A value conforming to EncodingProtocol and expected to conform to KeyProtocol.

Throws

An AptosError object with a message that the value does not conform to the required KeyProtocol, if the provided value does not conform to KeyProtocol.

str(_:_:)

Serialize a String value or an array of String values using a custom Serializer.

public static func str<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single String or an array of Strings. The serialized String values are converted to Data using UTF-8 encoding.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a String or an array of Strings.

Throws

An AptosError object that's an invalid data value with the supported type of either String or [String], if the provided value does not match either a String object or an array of String objects.

u8(_:_:)

Serialize a UInt8 value or an array of UInt8 values using a custom Serializer.

public static func u8<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single UInt8 or an array of UInt8s.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a UInt8 or an array of UInt8s.

Throws

An AptosError object that's an invalid data value with the supported type of either UInt8 or [UInt8], if the provided value does not match either a UInt8 object or an array of UInt8 objects.

u16(_:_:)

Serialize a UInt16 value or an array of UInt16 values using a custom Serializer.

public static func u16<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single UInt16 or an array of UInt16s.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a UInt16 or an array of UInt16s.

Throws

An AptosError object that's an invalid data value with the supported type of either UInt16 or [UInt16], if the provided value does not match either a UInt16 or an array of UInt16s.

u32(_:_:)

Serialize a UInt32 value or an array of UInt32 values using a custom Serializer.

public static func u32<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single UInt32 or an array of UInt32s.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a UInt32 or an array of UInt32s.

Throws

An AptosError object that's an invalid data value with the supported type of either UInt32 or [UInt32], if the provided value does not match either a UInt32 or an array of UInt32s.

u64(_:_:)

Serialize a UInt64 value or an array of UInt64 values using a custom Serializer.

public static func u64<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single UInt64 or an array of UInt64s.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a UInt64 or an array of UInt64s.

Throws

An AptosError object that's an invalid data value with the supported type of either UInt64 or [UInt64], if the provided value does not match either a UInt64 or an array of UInt64s.

u128(_:_:)

Serialize a UInt128 value or an array of UInt128 values using a custom Serializer.

public static func u128<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single UInt128 or an array of UInt128s.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a UInt128 or an array of UInt128s.

Throws

An AptosError object that's an invalid data value with the supported type of either UInt128 or [UInt128], if the provided value does not match either a UInt128 or an array of UInt128s.

u256(_:_:)

Serialize a UInt256 value or an array of UInt256 values using a custom Serializer.

public static func u256<T: EncodingContainer>(_ serializer: Serializer, _ value: T) throws 

This function takes a custom Serializer and a generic value conforming to the EncodingContainer protocol, and attempts to serialize the value as a single UInt256 or an array of UInt256s.

Parameters

  • serializer: A custom Serializer instance to be used for serialization.
  • value: A generic value conforming to EncodingContainer, which is either a UInt256 or an array of UInt256s.

Throws

An AptosError object that's an invalid data value with the supported type of either UInt256 or [UInt256], if the provided value does not match either a UInt256 or an array of UInt256s.

Types
Protocols
Global Variables
Global Functions
Extensions
Clone this wiki locally