XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions 8.0.13

dotnet add package XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions --version 8.0.13                
NuGet\Install-Package XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions -Version 8.0.13                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions" Version="8.0.13" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions --version 8.0.13                
#r "nuget: XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions, 8.0.13"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions as a Cake Addin
#addin nuget:?package=XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions&version=8.0.13

// Install XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions as a Cake Tool
#tool nuget:?package=XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions&version=8.0.13                

XO.EntityFrameworkCore.NpgsqlJsonSerializerOptions

NuGet GitHub Actions Status codecov

An Entity Framework Core plugin that adds support for JsonSerializerOptions to the Npgsql provider's built-in JSON POCO mapping. You could use it to influence the property naming policy, or to use JSON source generation.

NOTE: Version 8.0 of the Npgsql EF provider introduced full support for Entity Framework Core's JSON columns feature. This plugin is for Npgsql's native POCO mapping, not the EF Core feature. You probably want to use JSON columns in new projects!

Usage

  1. Opt in to Npgsql's dynamic JSON POCO mapping, which is disabled by default in Npgsql 8.0. An example from this project's tests:

    private static readonly DbDataSource _dataSource = new NpgsqlDataSourceBuilder(
        "Host=localhost;Username=postgres;Password=password;Database=XO.EntityFrameworkCore.Tests")
        .EnableDynamicJson()
        .Build();
    
  2. Call the extension method to add the plugin to your DbContext.

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder
            .UseNpgsql(_dataSource)
            .UseNpgsqlJsonSerializerOptions();
    }
    
  3. Call UseJsonSerializerOptions to configure JsonSerializerOptions for a specific property.

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyEntity>(entity =>
        {
            entity.Property(x => x.MyJsonProperty)
                .HasColumnType("jsonb")
                .UseJsonSerializerOptions(new JsonSerializerOptions
                {
                    DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
                    PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
                });
        });
    }
    
    

Options

Default Serializer Options

To apply default JsonSerializerOptions to all json and jsonb columns, pass the default instance to the options builder:

Note: When configuring the default serializer options, pass the same instance every time your configuration callback is invoked. Otherwise, Entity Framework will detect the options have changed and construct a new IServiceProvider for every DbContext!

private static readonly JsonSerializerOptions defaultOptions
    = new JsonSerializerOptions
    {
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
    };

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseNpgsql("Host=localhost")
        .UseNpgsqlJsonSerializerOptions(defaultOptions);
}

JsonSerializerValueComparer<T>

By default, the plugin configures affected entity properties to use JsonSerializerValueComparer<T> for value equality comparison, which serializes each value to JSON and compares the resulting strings. If you prefer the default value comparer's semantics for your value type(s), or if you just don't want the additional serialization overhead, you can disable it:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseNpgsql("Host=localhost")
        .UseNpgsqlJsonSerializerOptions(useJsonSerializerValueComparer: false);
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.13 16,004 5/13/2024
2.0.4 59,749 8/17/2023
1.1.21 49,895 4/28/2023
1.0.6 494 4/27/2023