JsonToObjectPropertyHandler
A property handler that maps the SQL Server json type into any property type via System.Text.Json.
A property handler that maps the SQL Server json type, which the database returns as a string, into a TObject property via JsonSerializer. Use it for the types that are not entities, such as collections and dictionaries.
This handler is compiled only for .NET 8 and later. It is not available on the
netstandard2.0target of the RepoDb package.
The conversion is the same as JsonToEntityPropertyHandler. The two classes differ only by the intent of the type argument.
Overview
| Name | Description |
|---|---|
| Class | JsonToObjectPropertyHandler<TObject> |
| Namespace | RepoDb.PropertyHandlers |
| Package | RepoDb |
| Implements | IPropertyHandler<string, TObject> |
| Column type | The SQL Server json type, returned by the database as a string |
| Property type | TObject |
| Availability | Not available on the netstandard2.0 target of the package; requires .NET 8 or later. |
Conversions
| Method | Direction | Description |
|---|---|---|
| Get | Database to property | Deserializes the JSON text into a TObject via JsonSerializer.Deserialize<TObject>(). A null or an empty value is returned as default. |
| Set | Property to database | Serializes the TObject into JSON text via JsonSerializer.Serialize(). A null value is written as null. |
Usability
Bind the handler, closed over the property type, to the property that maps to a json column.
Attribute
public class Setting
{
public int Id { get; set; }
[Map("values"), PropertyHandler(typeof(JsonToObjectPropertyHandler<Dictionary<string, string>>))]
public Dictionary<string, string> Values { get; set; }
}
Once bound, the library invokes the handler automatically when the property is read or written.
using (var connection = new SqlConnection(connectionString))
{
connection.Insert(new Setting { Id = 1, Values = new Dictionary<string, string> { ["theme"] = "dark" } });
var setting = connection.Query<Setting>(e => e.Id == 1).First();
// setting.Values["theme"] is "dark"
}
Fluent Mapping
To configure via FluentMapper:
FluentMapper
.Entity<Setting>()
.PropertyHandler<JsonToObjectPropertyHandler<Dictionary<string, string>>>(e => e.Values);
Property Level Mapping
To configure via PropertyHandlerMapper:
PropertyHandlerMapper.Add<Setting, JsonToObjectPropertyHandler<Dictionary<string, string>>>(e => e.Values, new JsonToObjectPropertyHandler<Dictionary<string, string>>(), true);
Type Level Mapping
To apply the handler to every property of the type Dictionary<string, string>:
PropertyHandlerMapper.Add<Dictionary<string, string>, JsonToObjectPropertyHandler<Dictionary<string, string>>>(new JsonToObjectPropertyHandler<Dictionary<string, string>>(), true);
A type level mapping is process-wide: it is applied to every
Dictionary<string, string>property of every entity and connection, not only to the intended column. Prefer the attribute, fluent or property level mapping unless everyDictionary<string, string>property maps to ajsoncolumn.