Interface IXpingSerializer
- Namespace
- Xping.Sdk.Core.Serialization
- Assembly
- Xping.Sdk.Core.dll
Defines the contract for serialization and deserialization operations in the Xping SDK. This interface provides a centralized way to serialize test execution data to JSON format and deserialize it back to strongly-typed objects.
public interface IXpingSerializer
Methods
DeserializeAsync<T>(Stream, CancellationToken)
Asynchronously deserializes a stream containing JSON to an object of the specified type. This method is useful for deserializing large objects or when working with streams.
Task<T?> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream containing the JSON to deserialize.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task<T>
A task representing the asynchronous operation, containing the deserialized object or null.
Type Parameters
TThe type to deserialize to.
Remarks
The stream is NOT disposed by this method. The caller retains ownership of the stream and is responsible for disposing it. The stream position is advanced as data is read. The stream remains open after deserialization completes.
Exceptions
- ArgumentNullException
Thrown when
streamis null.- JsonException
Thrown when the JSON is invalid or cannot be converted to the target type.
Deserialize<T>(ReadOnlySpan<byte>)
Deserializes a UTF-8 encoded JSON byte array to an object of the specified type.
T? Deserialize<T>(ReadOnlySpan<byte> utf8Json)
Parameters
utf8JsonReadOnlySpan<byte>The UTF-8 encoded JSON byte array to deserialize.
Returns
- T
The deserialized object, or null if the JSON represents a null value.
Type Parameters
TThe type to deserialize to.
Exceptions
- JsonException
Thrown when the JSON is invalid or cannot be converted to the target type.
Deserialize<T>(string)
Deserializes a JSON string to an object of the specified type.
T? Deserialize<T>(string json)
Parameters
jsonstringThe JSON string to deserialize.
Returns
- T
The deserialized object, or null if the JSON represents a null value.
Type Parameters
TThe type to deserialize to.
Exceptions
- ArgumentNullException
Thrown when
jsonis null.- JsonException
Thrown when the JSON is invalid or cannot be converted to the target type.
SerializeAsync<T>(Stream, T, CancellationToken)
Asynchronously serializes an object to a stream in JSON format. This method is useful for serializing large objects or when working with streams.
Task SerializeAsync<T>(Stream stream, T value, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write the JSON to.
valueTThe object to serialize.
cancellationTokenCancellationTokenA cancellation token to observe while waiting for the task to complete.
Returns
- Task
A task representing the asynchronous operation.
Type Parameters
TThe type of the object to serialize.
Remarks
The stream is NOT disposed by this method. The caller retains ownership of the stream and is responsible for disposing it. The stream position is advanced to the end of the written content, and the stream is flushed to ensure all data is written.
Exceptions
- ArgumentNullException
Thrown when
streamorvalueis null.
SerializeToUtf8Bytes<T>(T)
Serializes an object to a UTF-8 encoded byte array.
byte[] SerializeToUtf8Bytes<T>(T value)
Parameters
valueTThe object to serialize.
Returns
- byte[]
A UTF-8 encoded byte array containing the JSON representation.
Type Parameters
TThe type of the object to serialize.
Exceptions
- ArgumentNullException
Thrown when
valueis null.
Serialize<T>(T)
Serializes an object to a JSON string.
string Serialize<T>(T value)
Parameters
valueTThe object to serialize.
Returns
- string
A JSON string representation of the object.
Type Parameters
TThe type of the object to serialize.
Exceptions
- ArgumentNullException
Thrown when
valueis null.