BitzArt.Json.TypedValues 0.2.2

Prefix Reserved
dotnet add package BitzArt.Json.TypedValues --version 0.2.2
                    
NuGet\Install-Package BitzArt.Json.TypedValues -Version 0.2.2
                    
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="BitzArt.Json.TypedValues" Version="0.2.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BitzArt.Json.TypedValues" Version="0.2.2" />
                    
Directory.Packages.props
<PackageReference Include="BitzArt.Json.TypedValues" />
                    
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 BitzArt.Json.TypedValues --version 0.2.2
                    
#r "nuget: BitzArt.Json.TypedValues, 0.2.2"
                    
#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 BitzArt.Json.TypedValues@0.2.2
                    
#: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=BitzArt.Json.TypedValues&version=0.2.2
                    
Install as a Cake Addin
#tool nuget:?package=BitzArt.Json.TypedValues&version=0.2.2
                    
Install as a Cake Tool

NuGet version NuGet downloads

Overview

BitzArt.Json.TypedValues is a nuget package that allows retaining actual type information when serializing and deserializing values to and from JSON using System.Text.Json.

The Problem

Let's consider the following class hierarchy:

public class Fruit { }

public class Apple : Fruit
{
    public string AppleVariety { get; set; }
}

public class Banana : Fruit
{
    public string BananaVariety { get; set; }
}

The following code will result in the loss of objects' actual types when serialized/deserialized:

var fruits = new List<Fruit>
{
    new Apple { AppleVariety = "Granny Smith" },
    new Banana { BananaVariety = "Cavendish" }
};

var serialized = JsonSerializer.Serialize(fruits);

// Resulting deserialized objects are losing their
// actual types since the type information is not retained in JSON.
var deserialized = JsonSerializer.Deserialize<IEnumerable<Fruit>>(serialized);

The Solution

BitzArt.Json.TypedValues provides a way to retain type information during serialization and deserialization. By using the TypedValue<T> class, you can ensure that the type information is preserved.

var fruits = new List<TypedValue<Fruit>>
{
    new Apple { AppleVariety = "Granny Smith" },
    new Banana { BananaVariety = "Cavendish" }
};

var serialized = JsonSerializer.Serialize(fruits);

// Resulting deserialized objects will be deserialized back to their original types.
var deserialized = JsonSerializer.Deserialize<IEnumerable<TypedValue<Fruit>>>(serialized);
// or
var deserialized = JsonSerializer.Deserialize<IEnumerable<TypedValue>>(serialized).Select(x => (Fruit)x.Value);
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.  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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on BitzArt.Json.TypedValues:

Package Downloads
BitzArt.Flux.MudBlazor

BitzArt.Flux and MudBlazor integration package

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.2 985 6/1/2025
0.2.1 188 5/26/2025
0.2.0 174 5/25/2025