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
<PackageReference Include="BitzArt.Json.TypedValues" Version="0.2.2" />
<PackageVersion Include="BitzArt.Json.TypedValues" Version="0.2.2" />
<PackageReference Include="BitzArt.Json.TypedValues" />
paket add BitzArt.Json.TypedValues --version 0.2.2
#r "nuget: BitzArt.Json.TypedValues, 0.2.2"
#:package BitzArt.Json.TypedValues@0.2.2
#addin nuget:?package=BitzArt.Json.TypedValues&version=0.2.2
#tool nuget:?package=BitzArt.Json.TypedValues&version=0.2.2
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 | Versions 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. |
-
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.