CSharpEssentials.Json 6.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package CSharpEssentials.Json --version 6.3.0
                    
NuGet\Install-Package CSharpEssentials.Json -Version 6.3.0
                    
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="CSharpEssentials.Json" Version="6.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CSharpEssentials.Json" Version="6.3.0" />
                    
Directory.Packages.props
<PackageReference Include="CSharpEssentials.Json" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CSharpEssentials.Json --version 6.3.0
                    
#r "nuget: CSharpEssentials.Json, 6.3.0"
                    
#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.
#:package CSharpEssentials.Json@6.3.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CSharpEssentials.Json&version=6.3.0
                    
Install as a Cake Addin
#tool nuget:?package=CSharpEssentials.Json&version=6.3.0
                    
Install as a Cake Tool

CSharpEssentials.Json

Enhanced JSON serialization utilities on top of System.Text.Json.

Features

  • Enum Conventions: [StringEnum] enums read and write their wire names (AddEnumConventions); other enums keep the host's behavior.
  • Multi-Format DateTime: Deserialize from many date formats automatically.
  • Polymorphic Serialization: Serialize abstract types and interfaces with a $type discriminator.
  • Pre-Configured Options: CamelCase, ignore cycles, ignore nulls, relaxed escaping.

Installation

dotnet add package CSharpEssentials.Json

Usage

Pre-Configured Options

using CSharpEssentials.Json;
using System.Text.Json;

var options = EnhancedJsonSerializerOptions.DefaultOptions;
string json = JsonSerializer.Serialize(myObject, options);

Quick Extensions

ConvertToJson, ConvertFromJson and ConvertToJsonDocument use EnhancedJsonSerializerOptions.DefaultOptions (or the options you pass) through reflection-based serialization. They are annotated [RequiresUnreferencedCode] and [RequiresDynamicCode]; in trimmed or NativeAOT apps call JsonSerializer with a JsonSerializerContext instead.

using System.Text.Json;
using CSharpEssentials.Json;

var user = new { Name = "Alice", Age = 30 };

string json = user.ConvertToJson();
var parsed = json.ConvertFromJson<User>();
JsonDocument doc = json.ConvertToJsonDocument();

JsonElement to CLR Values

Dictionary<string, object> and object deserialize to JsonElement. ToClrObject turns them into plain values: objects become Dictionary<string, object?>, arrays List<object?>, numbers int, long, decimal or double (first that fits).

using System.Text.Json;
using CSharpEssentials.Json;

using JsonDocument doc = JsonDocument.Parse("""{"id":1,"price":9.99,"tags":["a"]}""");

var data = (Dictionary<string, object?>)doc.RootElement.ToClrObject()!;
// data["id"] is int, data["price"] is decimal, data["tags"] is List<object?>

Enum Conventions

AddEnumConventions adds the enum converter for every enum with generated metadata ([StringEnum], see CSharpEssentials.Enums). Other enums are left to the converters already on the options, so a plain enum stays a number unless you add JsonStringEnumConverter yourself.

using System.Text.Json;
using CSharpEssentials.Enums;
using CSharpEssentials.Json;

[StringEnum]
public enum Status { Active, Inactive, [EnumFallback] Unknown }

public enum Level { Low, High }

public sealed record Account(Status Status, Level Level);

// Data mode (the default): tolerant, for values you stored or received from a trusted service
JsonSerializerOptions data = new JsonSerializerOptions(JsonSerializerDefaults.Web)
    .AddEnumConventions(EnumConventions.Default);

// Input mode: strict, for request bodies (AddEnumConventions() in CSharpEssentials.AspNetCore does this for you)
JsonSerializerOptions input = new JsonSerializerOptions(JsonSerializerDefaults.Web)
    .AddEnumConventions(EnumConventions.Default, EnumReadMode.Input);

JsonSerializer.Serialize(new Account(Status.Active, Level.High), data); // {"status":"active","level":1}
JsonSerializer.Deserialize<Account>("""{"status":"gone","level":0}""", data);  // Status.Unknown (fallback)
JsonSerializer.Deserialize<Account>("""{"status":"gone","level":0}""", input); // throws EnumValueJsonException
  • Reads accept the wire name, the member name in any casing, aliases and the number of a defined member. Input mode applies EnumConventions.AcceptNumbers, AcceptMemberNames and CaseInsensitive.
  • Undefined values ("gone", 999) are rejected with EnumValueJsonException (a JsonException). In Data mode only, they map to the [EnumFallback] member when the enum declares one (EnumConventions.UnknownValue = UnknownEnumValueHandling.UseFallback, the default).
  • Writes are the wire name, or the number with writeAs: EnumWireFormat.Number. [Flags] values are JSON arrays.
  • EnhancedJsonSerializerOptions.DefaultOptions already calls AddEnumConventions(EnumConventions.Default) (Data mode).
  • A [StringEnum] enum without generated metadata (CSE0015) throws when its converter is created instead of falling back to numbers. AddEnumConventionsWithReflection also converts enums without metadata through reflection; it is not trim or AOT safe.

ConditionalStringEnumConverter (4.x) is obsolete and forwards to the same converter in Input mode. It no longer has AllowUndefinedValues: undefined numbers are always rejected. See Migrating from 4.x to 5.0.

StringEnumNaming

StringEnumNaming is obsolete in 5.0 and stays as a working facade over EnumMetadata. In new code use EnumMetadata, EnumValueFormatter, EnumValueParser or the generated ToWireName() and TryParseWire helpers: they are the naming source that JSON, EF Core storage, Swagger schemas and query/route binding all use, so a member is spelled the same everywhere. Names are [JsonStringEnumMemberName] when present, otherwise snake_case_lower.

using CSharpEssentials.Json;

[StringEnum]
public enum Http { OK, HTTPStatus }

StringEnumNaming.GetName(Http.HTTPStatus);                    // "http_status"
StringEnumNaming.GetNames<Http>();                            // ["ok", "http_status"]
StringEnumNaming.TryParse<Http>("HTTPStatus", out var value); // true (policy name, member name or number)

Polymorphic Serialization

public abstract class Animal { public string Name { get; set; } = string.Empty; }
public class Dog : Animal { public string Breed { get; set; } = string.Empty; }

Animal dog = new Dog { Name = "Rex", Breed = "Labrador" };
string json = dog.ConvertToJson(); // {"$type":"Dog", "name":"Rex", "breed":"Labrador"}

Animal? restored = json.ConvertFromJson<Animal>();

Multi-Format DateTime

// All formats work automatically
string[] dates = ["2024-03-14T15:30:45Z", "14.03.2024", "March 14, 2024"];
foreach (var d in dates)
{
    DateTime dt = d.ConvertFromJson<DateTime>();
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed.  net11.0 is compatible. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (7)

Showing the top 5 NuGet packages that depend on CSharpEssentials.Json:

Package Downloads
CSharpEssentials.Results

Result pattern implementation for functional error handling in C#. Provides Result<T> and Result types for railway-oriented programming, eliminating exceptions for expected failures. Foundation for functional programming patterns and robust error handling.

CSharpEssentials

A comprehensive C# library enhancing functional programming capabilities with type-safe monads (Maybe, Result), discriminated unions (Any), and robust error handling. Features include: domain-driven design support, enhanced Entity Framework integration, testable time management, JSON utilities, and LINQ extensions. Built for modern C# development with focus on maintainability, testability, and functional programming principles.

CSharpEssentials.EntityFrameworkCore

Enhances Entity Framework Core with functional programming patterns and DDD-friendly features. Includes base entity classes, soft delete support, audit trails, query filters, optimistic concurrency, PostgreSQL integration, query performance monitoring, and domain event handling. Perfect for building maintainable and scalable data access layers in modern .NET applications.

CSharpEssentials.AspNetCore

A comprehensive ASP.NET Core library that enhances functional programming capabilities in web applications. Features include API versioning, global exception handling with enhanced problem details, enum binding and wire formats, model validation, optimized JSON handling, and seamless integration with CSharpEssentials core functional patterns (Result, Maybe, Rule Engine). Perfect for building robust, maintainable, and type-safe web APIs following functional programming principles. OpenAPI documents come from CSharpEssentials.AspNetCore.OpenApi or CSharpEssentials.AspNetCore.Swashbuckle.

CSharpEssentials.Any

Discriminated union types (Any<T0,T1> to Any<T0,...,T7>) for functional programming in C#. Provides type-safe alternatives to object/dynamic with pattern matching, implicit conversions, and JSON serialization support. Essential for functional programming patterns and type-safe handling of multiple possible types.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
6.5.2 374 10/10/2026
6.5.1 378 10/10/2026
6.5.0 378 10/9/2026
6.4.0 387 10/9/2026
6.3.0 407 10/8/2026
6.2.0 452 10/8/2026
6.1.0 445 10/7/2026
6.0.0 462 10/7/2026
5.2.1 470 10/7/2026
5.2.0 460 10/7/2026
5.1.0 484 10/7/2026
5.0.0 482 10/6/2026
4.1.0 456 10/6/2026
4.0.0 460 10/5/2026
3.2.3 622 5/31/2026
3.2.2 510 5/31/2026
3.2.1 486 5/31/2026
3.2.0 482 5/30/2026
3.1.0 475 5/27/2026
3.0.8 458 5/20/2026
Loading failed