DynUtf8JsonWriter 1.2.2

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

// Install DynUtf8JsonWriter as a Cake Tool
#tool nuget:?package=DynUtf8JsonWriter&version=1.2.2

DynUtf8JsonWriter

Overview

Serializes dynamically typed values to JSON using Utf8JsonWriter.

Motivation

Explores use of source generation (hidden to library consumers), and dynamic method dispatch using the dynamic type.

Basic Usage

Instantiate SimpleDynamicJsonWriter (an implementation of DynamicJsonWriter built in to the library), providing a Utf8JsonWriter to the constructor. Use the DynamicJsonWriter alongside the original Utf8JsonWriter to produce JSON output as desired. To write a dynamically typed value, use DynamicJsonWriter.WriteDynamic. Calls to DynamicJsonWriter.WriteDynamic will be dispatched to the applicable DynamicJsonWriter.WriteValue overload, and then to the applicable Utf8JsonWriter write-value method.

Dynamically written value's type name

DynamicJsonWriter.WriteDynamic returns the name of the type that the value was interpreted as, or null if the value provided was null. The name of the type is provided as it would generally be written in C# source code (ex. decimal or string), not using reflection methods such as Type.Name or Type.FullName.

Supported Types

Native Utf8JsonWriter Types

The library provides support for the following types by calling the applicable "Write...Value" method on Utf8JsonWriter:

  • string
  • bool
  • DateTime
  • DateTimeOffset
  • decimal
  • double
  • Guid
  • int
  • long
  • float
  • uint
  • ulong

Auxiliary Types

DBNull is serialized to JSON null.

DateOnly is serialized to an ISO 8601 calendar date string, in yyyy-MM-dd format.

byte[] is serialized to a Base64 encoded string, using Utf8JsonWriter.WriteBase64StringValue.

Adding support for Additional Types

To add support for additional types, derive a subclass from DynamicJsonWriter, and use one of the following approaches.

Additional types using WriteValue methods

Provide additional WriteValue overloads in the subclass. For example:

public string WriteValue((string str, int num) pair)
{
    Writer.WriteStartArray();
    Writer.WriteStringValue(pair.str);
    Writer.WriteNumberValue(pair.num);
    Writer.WriteEndArray();

    return "(string str, int num)";
}

Also, override the WriteNonNullDynamic method as follows:

protected override string WriteNonNullDynamic(dynamic value) =>
    WriteValue(value);

This override is the same as the base implementation, but is requried for dynamic dispatch to work as expected.

Additional types using WriteFallback

Subclasses may implement WriteFallback to handle writing values whose type is otherwise unsupported. The default implementation always throws NotImplementedException.

WriteValue method attributes

DynamicJsonWriter's WriteValue methods are decorated with DynamicJsonWriteValueAttribute. For each overload, an attribute provides:

  • The Type that's handled.
  • The name of the type, as it would generally be written in C# source code (ex. bool).
  • The name of the Utf8JsonWriter write-value method used to write values (ex. WriteBooleanValue).
  • The name of the Utf8JsonReader get method that could be used to read values (ex. GetBoolean).

The library implementation does not look at DynamicJsonWriteValueAttribute decorations. These decorations are provided for optional use by the library consumer, when navigating library sources in an IDE, or via reflection on the DynamicJsonWriter type.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.
  • net6.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.

Version Downloads Last updated
1.2.2 178 5/24/2023
1.2.1 114 5/24/2023
1.1.0 118 5/22/2023
1.0.0 119 5/20/2023