A System.Text.Json serialization provider for the UUID library.
dotnet add package UUID.Serialization.System
using System.Text.Json;
// Register the converter globally
JsonSerializerOptions options = new()
{
Converters = { new UUIDConverter() }
};
// Serialize
var uuid = new UUID();
string json = JsonSerializer.Serialize(uuid, options);
// Deserialize
UUID deserializedUuid = JsonSerializer.Deserialize<UUID>(json, options);
public class UserModel
{
public UUID Id { get; set; }
public string Name { get; set; }
}
// Register converter
JsonSerializerOptions options = new()
{
Converters = { new UUIDConverter() }
};
// Serialize model
var user = new UserModel
{
Id = new UUID(),
Name = "John Doe"
};
string json = JsonSerializer.Serialize(user, options);
// Deserialize model
UserModel deserializedUser = JsonSerializer.Deserialize<UserModel>(json, options);
- Support for null handling
- Thread-safe implementation
- Efficient string conversion
- Comprehensive error handling
- Full documentation with XML comments
- Seamless integration with System.Text.Json
- Custom error messages for better debugging
The converter provides detailed error messages for common scenarios:
- Null values: "Cannot convert null value to UUID"
- Incorrect JSON types: Shows expected vs actual type
- Empty strings: "Cannot convert empty string to UUID"
- Invalid string formats: Includes the attempted value in error message
- UUID library
- System.Text.Json 9.0.0 or later
- Supports .NET 6.0+, .NET Standard 2.0+, and .NET Framework 4.8+
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the terms of the LICENSE file included in the root directory.