DotNetToolbox.ObjectDumper
8.1.1
dotnet add package DotNetToolbox.ObjectDumper --version 8.1.1
NuGet\Install-Package DotNetToolbox.ObjectDumper -Version 8.1.1
<PackageReference Include="DotNetToolbox.ObjectDumper" Version="8.1.1" />
paket add DotNetToolbox.ObjectDumper --version 8.1.1
#r "nuget: DotNetToolbox.ObjectDumper, 8.1.1"
// Install DotNetToolbox.ObjectDumper as a Cake Addin #addin nuget:?package=DotNetToolbox.ObjectDumper&version=8.1.1 // Install DotNetToolbox.ObjectDumper as a Cake Tool #tool nuget:?package=DotNetToolbox.ObjectDumper&version=8.1.1
Object Dumper (DotNetToolbox.ObjectDumper)
Introduction
DotNetToolbox.ObjectDumper is a powerful .NET 8 library for converting objects into a human-readable string representations. It provides customizable formatting options, including indentation, maximum depth control, and the ability to specify custom formatters for specific types. This library is particularly useful for logging, debugging, and serialization scenarios where a visual representation of an object is needed.
Table of Contents
Installation
The DotNet Toolbox Object Dumper is available as a NuGet package. To install it, run the following command in the Package Manager Console:
PM> Install-Package DotNetToolbox.ObjectDumper
Dependencies
- .NET 8
Extension Methods
Dump and DumpAsJson The library extends any object with Dump
and DumpAsJson
methods, allowing any object to be dumped easily without requiring manual instantiation of dumper objects.
Examples:
Dumping an object using default options:
var person = new Person { Name = "John Doe", Age = 30 }; Console.WriteLine(person.Dump());
Customizing the dump output:
Console.WriteLine(person.Dump(options => { options.IndentSize = 2; options.UseTabs = true; options.UseFullNames = true; });
Dumping an object as JSON:
Console.WriteLine(person.DumpAsJson());
Customizing JSON dump options:
Console.WriteLine(person.DumpAsJson(options => { options.Indented = false; options.MaxDepth = 5; }));
Object Dumping
The core feature of DotNetToolbox.ObjectDumper is to create a human-readable string representation of an object. It traverses the object graph and outputs a formatted string that shows the types, properties, and values.
JSON Object Dumping
In addition to the custom object dumping, the library also provides support for dumping objects as JSON strings using the System.Text.Json
serializer, offering a familiar and standardized output.
Customization
DotNetToolbox.ObjectDumper allows extensive customization of the output format:
- DumpBuilderOptions: Determines the behavior of the main object dumper. Users can configure indentation style, indent size, and whether to use full type names.
- JsonDumpBuilderOptions: Configures the JSON object dumper, with options for indentation and maximum object graph traversal depth.
One of the powerful features of DumpBuilderOptions
is the ability to define custom formatters for types. This allows you to control exactly how objects of certain types are represented in the output.
Custom Formatter Example:
- Defining a custom formatter for a type:
string customDump = person.Dump(options => { options.CustomFormatters[typeof(DateTime)] = value => ((DateTime)value).ToString("yyyy-MM-dd"); }); Console.WriteLine(customDump);
In this example, all DateTime
objects within the person
object will be formatted using the "yyyy-MM-dd"
format.
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. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
DotNetToolbox.ObjectDumper Version 8.1.1
Stable release of DotNetToolbox.ObjectDumper, a .NET 8 library with a powerful extension for converting objects into a human-readable string representations.