Interface IJsonSerializer
Defines a contract for JSON serialization and deserialization with Result-based error handling.
public interface IJsonSerializer
Remarks
Implementations use Result<T> for deserialization to handle failures gracefully without exceptions.
See JsonSerializer for the default implementation.
Methods
DeserializeObject<T>(string)
Deserializes a JSON string to the specified type.
Result<T> DeserializeObject<T>(string serializedValue)
Parameters
serializedValuestringThe JSON string to deserialize.
Returns
- Result<T>
A Result<T> containing the deserialized object on success, or a DeserializationFailure on failure.
Type Parameters
TThe target type to deserialize to.
Examples
var result = serializer.DeserializeObject<User>("{\"name\":\"John\"}");
result.Match(
user => Console.WriteLine(user.Name),
failure => Console.WriteLine("Deserialization failed")
);
SerializeObject<T>(T)
Serializes an object to a JSON string.
string SerializeObject<T>(T value)
Parameters
valueTThe object to serialize.
Returns
- string
The JSON string representation of the object.
Type Parameters
TThe type of the object to serialize.
Examples
var json = serializer.SerializeObject(new User { Name = "John" });