JsonHookup 0.1.2
dotnet add package JsonHookup --version 0.1.2
NuGet\Install-Package JsonHookup -Version 0.1.2
<PackageReference Include="JsonHookup" Version="0.1.2" />
paket add JsonHookup --version 0.1.2
#r "nuget: JsonHookup, 0.1.2"
// Install JsonHookup as a Cake Addin #addin nuget:?package=JsonHookup&version=0.1.2 // Install JsonHookup as a Cake Tool #tool nuget:?package=JsonHookup&version=0.1.2
JsonHookup
For when you wish System.Text.Json would honor DataContract and DataMember attributes
Overview
JsonHookup provides a "modifier" to System.Text.Json
, so that its JsonSerializer
can (largely) honor [DataContract]
/ [DataMember]
attributes on classes being serialized and deserialized.
This is especially useful when a library abstracts their ISerializer
, but the DTO classes are all decorated using [DataMember]
attributes.
Background
System.Text.Json
infamously refuses to honor System.Runtime.Serialization
constructs such as [DataContract]
, [DataMember]
, and [IgnoreDataMember]
. And historically before .NET 6/7, that was more defensible: System.Text.Json couldn't do much more than honor custom property names without involving cumbersome extensibility.
But as of .NET7, a foothold has appeared: Modifiers for the Type Info Resolver
Usage
// Apply JsonHookup to an instance of JsonSerializerOptions cloned from the static Default
JsonSerializerOptions jsonOptionsFromDefault = JsonHookup.AddHookupToDefault(HookupOptions.DefaultExplicit);
JsonSerializer.Serialize(myObject, jsonOptionsFromDefault);
// or
// Apply JsonHookup to some existing JsonSerializerOptions
JsonSerializerOptions existingJsonOptions;
someExistingOptions.AddHookup(HookupOptions.DefaultExplicit);
JsonSerializer.Serialize(myObject, existingJsonOptions);
// or
JsonSerializerOptions existingJsonOptions;
HookupOptions hookupOptions = new() // For example...
{
Ignore = HookupParts.DataMemberOrder, // ignore data member order
DataContractMode = HookupMode.Explicit, // use the hookup only on classes decorated with [DataContract] or [UseDataContract]
DataMemberMode = HookupMode.Explicit, // only include properties decorated with [DataMember]
}
existingJsonOptions.AddHookup(hookupOptions);
JsonSerializer.Serialize(myObject, existingJsonOptions);
Serializer options lifetime
Note: general guidance in System.Text.Json is to re-use an instance of JsonSerializerOptions
as widely as possible. This guidance still applies with JsonHookup.
Target Framework
JsonHookup depends on functionality added in .NET 7.
Status
This project is completely experimental.
Known Issues as of v0.1.2
- Interface types are not supported. Only concrete
class
orstruct
types are supported. - Member ordering semantics are rudimentary. Data Contract calls for some additional behavior.
- Emit Default Value is ignored
- Testing is skeletal at best
- The only supported TypeInfoResolver is
DefaultJsonTypeInfoResolver
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. 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. |
-
net7.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.