Struct SerializationResult<T>
Represents the outcome of a serialization or deserialization operation that produces
a value of type T. Carries either the successful result value
or an error message and optional exception.
Inherited Members
Namespace: Scylla.Core.Util.Serialization
Assembly: ScyllaCore.dll
Syntax
public readonly struct SerializationResult<T>
Type Parameters
| Name | Description |
|---|---|
| T | The type of the value produced on success. Commonly |
Remarks
This is the primary return type of ScyllaSerialization operations such as ToJSON<T>(T) and FromJSON<T>(string). The value-carrying variant has the same success/failure semantics as the non-generic SerializationResult, but additionally stores the deserialized or serialized value when the operation succeeds.
Always check IsSuccess before accessing Value; accessing Value on a failed result throws InvalidOperationException. Use GetValueOrDefault(T) or TryGetValue(out T) for safe access without an explicit check.
Properties
ErrorMessage
Gets the human-readable error message describing the failure, or null when
the operation succeeded. Falls back to "Unknown error" if a null
message was passed to Failure(string, Exception).
Declaration
public string ErrorMessage { get; }
Property Value
| Type | Description |
|---|---|
| string | A non-null, non-empty string when IsFailure is |
Exception
Gets the exception that caused the operation to fail, if one was captured.
May be null even on failure when the error is a structured serialization
error rather than an unexpected exception.
Declaration
public Exception Exception { get; }
Property Value
| Type | Description |
|---|---|
| Exception | The underlying Exception, or |
IsFailure
Gets a value indicating whether the operation failed. This is the logical complement of IsSuccess.
Declaration
public bool IsFailure { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
IsSuccess
Gets a value indicating whether the operation completed without errors and a result value is available via Value.
Declaration
public bool IsSuccess { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
Value
Gets the value produced by the operation. Only valid when
IsSuccess is true.
Declaration
public T Value { get; }
Property Value
| Type | Description |
|---|---|
| T | The deserialized or serialized value of type |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when accessed on a failed result. The exception message includes ErrorMessage to aid diagnosis. Use GetValueOrDefault(T) or TryGetValue(out T) to access the value safely without this risk. |
Methods
Failure(string, Exception)
Creates a SerializationResult<T> that represents a failed operation. The value is not set; accessing Value on the returned result will throw InvalidOperationException.
Declaration
public static SerializationResult<T> Failure(string errorMessage, Exception exception = null)
Parameters
| Type | Name | Description |
|---|---|---|
| string | errorMessage | A message describing the failure. If |
| Exception | exception | The exception that caused the failure, or |
Returns
| Type | Description |
|---|---|
| SerializationResult<T> | A result with IsSuccess set to |
GetValueOrDefault(T)
Returns Value if the operation succeeded, or
defaultValue if it failed. Unlike accessing
Value directly, this method never throws.
Declaration
public T GetValueOrDefault(T defaultValue = default)
Parameters
| Type | Name | Description |
|---|---|---|
| T | defaultValue | The value to return when the operation failed. Defaults to
|
Returns
| Type | Description |
|---|---|
| T | The operation result value on success, or |
Success(T)
Creates a SerializationResult<T> that represents a successful operation with the specified value.
Declaration
public static SerializationResult<T> Success(T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | The value produced by the operation (e.g., the serialized JSON string or the
deserialized object). May be |
Returns
| Type | Description |
|---|---|
| SerializationResult<T> | A result with IsSuccess set to |
ToResult()
Converts this value-bearing result to the non-generic SerializationResult, discarding the payload value. Useful when downstream code only cares about success or failure and not the specific output value.
Declaration
public SerializationResult ToResult()
Returns
| Type | Description |
|---|---|
| SerializationResult | A Success() result if this result is successful, or a Failure(string, Exception) result carrying the same ErrorMessage and Exception if this result failed. |
TryGetValue(out T)
Attempts to retrieve the result value without throwing on failure.
Declaration
public bool TryGetValue(out T value)
Parameters
| Type | Name | Description |
|---|---|---|
| T | value | When this method returns |
Returns
| Type | Description |
|---|---|
| bool |
|