BitzArt.Json.TypedObjects 0.1.0

Prefix Reserved
dotnet add package BitzArt.Json.TypedObjects --version 0.1.0                
NuGet\Install-Package BitzArt.Json.TypedObjects -Version 0.1.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="BitzArt.Json.TypedObjects" Version="0.1.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add BitzArt.Json.TypedObjects --version 0.1.0                
#r "nuget: BitzArt.Json.TypedObjects, 0.1.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.
// Install BitzArt.Json.TypedObjects as a Cake Addin
#addin nuget:?package=BitzArt.Json.TypedObjects&version=0.1.0

// Install BitzArt.Json.TypedObjects as a Cake Tool
#tool nuget:?package=BitzArt.Json.TypedObjects&version=0.1.0                

NuGet version NuGet downloads

Overview

BitzArt.Json.TypedObjects is a nuget package that allows retaining actual value types during JSON serialization and deserialization.

Since JSON does not preserve information about specific value types, serialization of such values may lead to this information being lost in cases where the actual type is not known in compile-time.

⚠️ Currently, the library only supports standard value types.

The Problem

Consider the following example:

List<object> myObjects = [1, 1.234, "string", null, new Apple()];

What will happen if we serialize this list to JSON and then deserialize it back?

var serialized = JsonSerializer.Serialize(myObjects);
var deserialized = JsonSerializer.Deserialize<List<object>>(serialized);

The resulting list will contain objects of type JsonElement instead of their original types, and the information about the objects' actual types will be lost - since the JsonConverter does not know the actual types of the objects in the list, and cannot deserialize them correctly.

Polymorphic Types

Now, let's consider the following class hierarchy:

public class Fruit { }

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

The following code will result in the loss of the actual type of the object when deserialized:

var apple = new Apple
{
    Variety = "Granny Smith"
};

var serialized = JsonSerializer.Serialize(apple);

var deserialized = JsonSerializer.Deserialize<Fruit>(serialized);

Since the object was deserialized as Fruit, the resulting object will be of type Fruit, and all properties specific to the Apple class will be lost.

The Solution

This library implements TypedObjectJsonConverter - a custom JSON converter that handles serialization and deserialization of polymorphic types, retaining actual value types:

Now, let's see how the TypedObjectJsonConverter can be used to solve the problem of losing actual types during serialization and deserialization:

var apple = new Apple
{
    Variety = "Granny Smith"
};

var fruit = (Fruit)apple; 

var jsonSerializerOptions = new JsonSerializerOptions
{
    Converters = { new TypedObjectJsonConverter<Fruit>() }
};

var serialized = JsonSerializer.Serialize(fruit, jsonSerializerOptions);

var deserialized = JsonSerializer.Deserialize<Fruit>(serialized, jsonSerializerOptions);

How it Works

When a value is serialized, TypedObjectJsonConverter stores the value's type name along with the value itself in the resulting JSON:

{
    "type": "MyNamespace.MyClass",
    "value": {"The actual serialized value of the object goes here"}
}

When deserializing, the TypedObjectJsonConverter uses the stored type name to find the actual type of the object via reflection, and then uses it to deserialize the value, thus retaining the actual type of the object.

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.
  • net8.0

    • No dependencies.

NuGet packages (1)

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

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.1.0 336 10/20/2024
0.0.1-Prerelease 81 10/20/2024