JSON support
Jerrycurl provides functionality related to storing JSON in your database and provides two extensions that allow you to use JSON object directly in your mapping code.
@result Blog
SELECT ...
WHERE JSON_VALUE(@R.Col(m => m.Settings), @R.JsonPath(m => m.Settings.IsPublic)) = 1
Which roughly translates to:
SELECT ...
WHERE JSON_VALUE(T1."Settings", '$.IsPublic') = 1
To add support for mapping JSON directly in your code, install one
If you are using .NET Core 3 or .NET 5 we recommend that you target the native System.Text.Json
library with the Jerrycurl.Extensions.Json
packages.
dotnet add package Jerrycurl.Extensions.Json
For lower .NET versions, you can instead use the JSON.NET package.
dotnet add package Jerrycurl.Extensions.Newtonsoft.Json
Setup
Having installed one of the package above, simply apply the UseJson
extension to add JSON behavior
to your domain.
class BlogDomain : IDomain
{
public void Configure(DomainOptions options)
{
options.UseSqlServer("...");
options.UseJson();
}
}
Types
Any type within your object model that is represented as JSON in you database can be used by simply
adding [Json]
attribute.
[Json]
public class BlogSettings
{
public bool IsPublic { get; set; }
}
Usage
JSON values can be
@model BlogSettings
@project Blog b
UPDATE @b.TblName()
SET @b.ColName(m => m.Settings) = @M.Par() -- auto-serialized
@result BlogSettings
@project Blog b
SELECT @b.Col(m => m.Settings) AS @R.Prop() -- auto-deserialized
FROM @b.Tbl()