Tedd.ObjectFlattener
1.0.0-preview3
This is a prerelease version of Tedd.ObjectFlattener.
dotnet add package Tedd.ObjectFlattener --version 1.0.0-preview3
NuGet\Install-Package Tedd.ObjectFlattener -Version 1.0.0-preview3
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="Tedd.ObjectFlattener" Version="1.0.0-preview3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tedd.ObjectFlattener" Version="1.0.0-preview3" />
<PackageReference Include="Tedd.ObjectFlattener" />
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 Tedd.ObjectFlattener --version 1.0.0-preview3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Tedd.ObjectFlattener, 1.0.0-preview3"
#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 Tedd.ObjectFlattener@1.0.0-preview3
#: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=Tedd.ObjectFlattener&version=1.0.0-preview3&prerelease
#tool nuget:?package=Tedd.ObjectFlattener&version=1.0.0-preview3&prerelease
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Tedd.ObjectFlattener
Serialize complex objects into a key value format preserving hierarchy
How it works
ObjectFlattener uses Newtonsoft.Json to serialize an object structure. Then it flattens the hierarchy into a Key-Value format.
Example
Simplified
// Serialization
var flattened = ObjectFlattener.Flatten(root);
// Deserialization
var rootBack = ObjectFlattener.Unflatten<Root>(flattened);
Full sample
// Define some test objects
public enum TestStatus { Pending, InProgress, Completed, Failed }
public record Root
{
public TestStatus Status { get; set; }
public string @String { get; set; }
public List<string> List { get; set; } = new();
public SubLevel1 SubLevel1_1 { get; set; } = new();
public SubLevel1 SubLevel1_2 { get; set; } = new();
}
public record SubLevel1
{
public SubLevel2 SubLevel2_1 { get; set; } = new();
public SubLevel2 SubLevel2_2 { get; set; } = new();
}
public record SubLevel2
{
// For easy comparison we need to preserve order, so using SortedDictionary
public SortedDictionary<string, int> Dict { get; set; } = new();
public List<TestStatus> List { get; set; } = new();
}
// Set up and populate test object
var root = new Root()
{
Status = TestStatus.InProgress,
@String = "Hello world",
List = ["First", "Second"],
SubLevel1_1 = new()
{
SubLevel2_1 = new() {
Dict = new() { { "First", 1 }, { "Second", 2 }, { "Third", 3 } },
List = [TestStatus.Pending] },
SubLevel2_2 = new() {
Dict = new() { { "Four", 4 }, { "Five", 5 } },
List = [TestStatus.Pending, TestStatus.Completed] }
},
SubLevel1_2 = new()
{
SubLevel2_1 = new() { Dict = null, List = null },
SubLevel2_2 = new() {
Dict = new() { { "A", 10 }, { "B", 20 } },
List = [TestStatus.Failed] }
}
};
// Serialization
var flattened = ObjectFlattener.Flatten(root);
// Dump debug
foreach (var kvp in flattened)
Console.WriteLine(kvp.Key + " -> " + kvp.Value);
// Deserialization
var rootBack = ObjectFlattener.Unflatten<Root>(flattened);
// Compare
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonRoot = System.Text.Json.JsonSerializer.Serialize(root, options);
string jsonRootBack = System.Text.Json.JsonSerializer.Serialize(rootBack, options);
bool areEqual = jsonRoot == jsonRootBack;
Console.WriteLine();
Console.WriteLine("Are equal: " + areEqual);
Outputs
Status -> 1
String -> Hello world
List:0 -> First
List:1 -> Second
SubLevel1_1:SubLevel2_1:Dict:First -> 1
SubLevel1_1:SubLevel2_1:Dict:Second -> 2
SubLevel1_1:SubLevel2_1:Dict:Third -> 3
SubLevel1_1:SubLevel2_1:List:0 -> 0
SubLevel1_1:SubLevel2_2:Dict:Five -> 5
SubLevel1_1:SubLevel2_2:Dict:Four -> 4
SubLevel1_1:SubLevel2_2:List:0 -> 0
SubLevel1_1:SubLevel2_2:List:1 -> 2
SubLevel1_2:SubLevel2_1:Dict ->
SubLevel1_2:SubLevel2_1:List ->
SubLevel1_2:SubLevel2_2:Dict:A -> 10
SubLevel1_2:SubLevel2_2:Dict:B -> 20
SubLevel1_2:SubLevel2_2:List:0 -> 3
SubLevel1_3:SubLevel2_1:Dict:First -> 1
SubLevel1_3:SubLevel2_1:Dict:Second -> 2
SubLevel1_3:SubLevel2_1:Dict:Third -> 3
SubLevel1_3:SubLevel2_1:List:0 -> 0
SubLevel1_3:SubLevel2_2:Dict:Five -> 5
SubLevel1_3:SubLevel2_2:Dict:Four -> 4
SubLevel1_3:SubLevel2_2:List:0 -> 0
SubLevel1_3:SubLevel2_2:List:1 -> 2
Are equal: True
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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.
-
net9.0
- Newtonsoft.Json (>= 13.0.3)
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 |
---|---|---|
1.0.0-preview3 | 296 | 4/4/2025 |
1.0.0-preview2 | 120 | 4/4/2025 |