All Projects → dahomey-technologies → Dahomey.json

dahomey-technologies / Dahomey.json

Licence: mit
The main purpose of this library is to bring missing features to the official .Net namespace System.Text.Json

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Dahomey.json

Jsonifier
Fast and simple JSON encoding toolkit
Stars: ✭ 79 (-5.95%)
Mutual labels:  json, serialization
Jackson Module Kotlin
Module that adds support for serialization/deserialization of Kotlin (http://kotlinlang.org) classes and data classes.
Stars: ✭ 830 (+888.1%)
Mutual labels:  json, serialization
Groot
From JSON to Core Data and back.
Stars: ✭ 533 (+534.52%)
Mutual labels:  json, serialization
Flatcc
FlatBuffers Compiler and Library in C for C
Stars: ✭ 434 (+416.67%)
Mutual labels:  json, serialization
Fhir.js
Node.JS library for serializing/deserializing FHIR resources between JS/JSON and XML using various node.js XML libraries
Stars: ✭ 61 (-27.38%)
Mutual labels:  json, serialization
Airframe
Essential Building Blocks for Scala
Stars: ✭ 442 (+426.19%)
Mutual labels:  json, serialization
Dartson
Dartson is a Dart library that can be used to convert Dart objects into a JSON string.
Stars: ✭ 78 (-7.14%)
Mutual labels:  json, serialization
Velocypack
A fast and compact format for serialization and storage
Stars: ✭ 347 (+313.1%)
Mutual labels:  json, serialization
Tech1 Benchmarks
Java JMH Benchmarks repository. No Longer Supported.
Stars: ✭ 50 (-40.48%)
Mutual labels:  json, serialization
Eminim
JSON serialization framework for Nim, works from a Stream directly to any type and back. Depends only on stdlib.
Stars: ✭ 32 (-61.9%)
Mutual labels:  json, serialization
Jsoniter Scala
Scala macros for compile-time generation of safe and ultra-fast JSON codecs
Stars: ✭ 410 (+388.1%)
Mutual labels:  json, serialization
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (-20.24%)
Mutual labels:  json, serialization
Handyjson
A handy swift json-object serialization/deserialization library
Stars: ✭ 3,913 (+4558.33%)
Mutual labels:  json, serialization
Iguana
universal serialization engine
Stars: ✭ 481 (+472.62%)
Mutual labels:  json, serialization
Leopotamgrouplibraryunity
Tools library for unity 3d game engine: animator graph helpers, serialization (json), localization, event routing (eventbus, ui actions), embedded scripting, uGui xml markup, threading, tweening, in-memory protection and other helpers (pure C#)
Stars: ✭ 373 (+344.05%)
Mutual labels:  json, serialization
Fastjson
A fast JSON parser/generator for Java.
Stars: ✭ 23,997 (+28467.86%)
Mutual labels:  json, serialization
Bebop
An extremely simple, fast, efficient, cross-platform serialization format
Stars: ✭ 305 (+263.1%)
Mutual labels:  json, serialization
Kim
Kim: A JSON Serialization and Marshaling framework
Stars: ✭ 326 (+288.1%)
Mutual labels:  json, serialization
Play Json Extra
playframework2 json extra module. provide convenience functions for define Format, Reads, Writes
Stars: ✭ 20 (-76.19%)
Mutual labels:  json, serialization
Simplify Core
Simplify 为简化重复的JAVA代码而生,基于JDK8,无其它jar包依赖,提供序列化,json parse/generator,日期处理,asm && jdkproxy 实现动态代理功能 等常见操作。
Stars: ✭ 65 (-22.62%)
Mutual labels:  json, serialization

Dahomey.Json

Extensions to System.Text.Json

Nuget (with prereleases) License

Introduction

The main purpose of this library is to bring missing features to the official .Net namespace System.Text.Json

Supported .NET versions

  • .NET Standard 2.0
  • .NET Core 3.1
  • .NET 5.0.1

Features

  • Extensible Polymorphism support based on discriminator conventions (example)
  • Conditional Property Serialization support based on the existence of a method ShouldSerialize[PropertyName]() (example)
  • Support for interfaces and abstract classes (example)
  • Support for numeric, enum and custom dictionary keys (example)
  • Support for non default constructors (example)
  • Can ignore default values (example)
  • Can require properties or fields with different policies (example)
  • Object mapping to programmatically configure features in a non invasive way (example)
  • Support for Writable JSON Document Object Model (cf. Spec) (example)
  • Support for serialization callbacks (before/after serialization/deserialization) (example)
  • Support for anonymous types (example)
  • Support for DataContractAttribute and DataMemberAttribute (example)
  • Extended support for structs (example)
  • Support for Nullables (example)
  • Support for collection interfaces: ISet<>, IList<>, ICollection<>, IEnumerable<>, IReadOnlyList<>, IReadOnlyCollection<>, IDictionary<>, IReadOnlyDictionary<> (example)
  • Support for immutable collections: ImmutableArray<>, ImmutableList<>, ImmutableSortedSet<>, ImmutableHashSet<>, ImmutableDictionary<>, ImmutableSortedDictionary<> (example)
  • Support for reference loop handling (cf. https://github.com/dotnet/corefx/issues/41002) (example)
  • Support for deserializing into read-only properties (cf. https://github.com/dotnet/corefx/issues/40602) (example)
  • Support for dynamics (example)
  • Support for C# 8 nullable reference types
  • Support for MissingMemberHandling (example)
  • Support for different property requirement policy (example)
  • Support for extended naming policies: Kebab case and Snake case (example1, example2)

Installation

NuGet

https://www.nuget.org/packages/Dahomey.Json/

Install-Package Dahomey.Json

Compilation from source

  1. dotnet restore
  2. dotnet pack -c Release

How to use Dahomey.Json

Common Setup

using Dahomey.Json;
using System.Text.Json;

JsonSerializerOptions options = new JsonSerializerOptions();
options.SetupExtensions();

You can also instantiate options and setup extensions in one line:

using Dahomey.Json;
using System.Text.Json;

JsonSerializerOptions options = new JsonSerializerOptions().SetupExtensions();

Polymorphism

In order to distinguish inherited classes from a reference to a base class or a collection of references to a base class, a special property called discriminator can be added to the serialized json.

To describe the type of the discriminator property as well as the association between a specific inherited Type and a specific discriminator value, a discriminator convention supporting the interface IDiscriminatorConvention should be written.

Discriminator conventions can be registered to the discriminator convention registry accessible from the JsonSerilizedOptions.

Several conventions can be registered. When registering an inherited Type, an attempt to register it to each convention, begining with the last convention register. The first convention to accept to register a specific type will stop the registration process. It means that different type inheritance hierarchy could serialize/deserialize their discriminator property in a different way.

The library offers 1 built-in discriminator convention:

  • DefaultDiscriminatorConvention: the discriminator value is defined by decorating classes with the attribute JsonDiscriminatorAttribute

This built-in convention setup the convention property name to $type

DefaultDiscriminatorConvention

This type of the discriminator value is configured via the generic parameter of the convention class. The value type passed to the JsonDiscriminatorAttribute must match this type.

public class WeatherForecast
{
   public DateTimeOffset Date { get; set; }
   public int TemperatureCelsius { get; set; }
   public string Summary { get; set; }
}
[JsonDiscriminator(1234)]
public class WeatherForecastDerived : WeatherForecast
{
    public int WindSpeed { get; set; }
}
JsonSerializerOptions options = new JsonSerializerOptions();
options.SetupExtensions();
DiscriminatorConventionRegistry registry = options.GetDiscriminatorConventionRegistry();
registry.ClearConventions();
registry.RegisterConvention(new DefaultDiscriminatorConvention<int>(options, "_t"));
registry.RegisterType<WeatherForecastDerived>();

string json = JsonSerializer.Serialize<WeatherForecast>(weatherForecastDerived, options);
{
  "_t": 1234,
  "Date": "2019-08-01T00:00:00-07:00",
  "TemperatureCelsius": 25,
  "Summary": "Hot",
  "WindSpeed": 35
}

Discriminator policies

  • Auto (default value): the discriminator property is written only the declared type and the actual type are different.
  • Always: the discriminator property is forced to always be written.
  • Never: he discriminator property is forced to never be written
JsonSerializerOptions options = new JsonSerializerOptions();
options.SetupExtensions();
DiscriminatorConventionRegistry registry = options.GetDiscriminatorConventionRegistry();
registry.DiscriminatorPolicy = DiscriminatorPolicy.Always;

Conditional Property Serialization

In the class to serialize, if a method exists which signature is bool ShouldSerialize[PropertyName](), it will be called to conditionally serialize the matching property.

public class WeatherForecast
{
   public DateTimeOffset Date { get; set; }
   public int TemperatureCelsius { get; set; }
   public string Summary { get; set; }
   
   public bool ShouldSerializeSummary()
   {
       return !string.IsNullOrEmpty(Summary);
   }
}
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].