Skip to content

accordproject/concerto-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Join the Accord Project Discord

Introduction

This is a prototype implementation of the Concerto Schema Language for .NET written in C#. The current reference implementation is written in JavaScript.

This repository contains:

  • Serialization and Deserialization utilities for System.Text.Json and Newtonsoft.Json.

Basic Usage

using System.Text.Json;
using System.Text.Json.Serialization;
using Concerto.Serialization;
using Concerto.Models.org.test;
 
//...

    JsonSerializerOptions options = new()
    {
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        Converters =
        {
            new JsonStringEnumConverter(),
            new ConcertoConverter(),
        }
    };

    Employee employee = new Employee()
    {
        firstName = "Matt",
        lastName = "Roberts",
        email = "[email protected]",
        _identifier = "[email protected]",
        department = Department.ENGINEERING,
        employeeId = "123"
    };

    string jsonString = JsonSerializer.Serialize(employee, options);

    // jsonString contains
    //{
    //     "$class": "org.test.Employee",
    //     "department": "ENGINEERING",
    //     "employeeId": "123",
    //     "email": "[email protected]",
    //     "firstName": "Matt",
    //     "lastName": "Roberts",
    //     "$identifier": "[email protected]"
    // }
  

This repository uses statically built versions of the internal Concerto.Models.concerto and Concerto.Models.concerto.metamodel namespaces. In the future this should be build dynamically using the Concerto CLI. There is an open branch with the changes needed to concerto-tools package. You can build the C# classes from the Concerto source files (.cto) will the following command:

concerto compile --model ./models/concerto.metamodel.cto --model ./models/employee.cto --target csharp --output ./ --useSystemTextJson --useNewtonsoftJson

Running Tests

  $ cd ConcertoJsonConverter.Tests
  $ dotnet restore
  $ dotnet build --configuration Release --no-restore
  $ dotnet test --no-restore --verbosity normal

Limitations

Support for polymorphic deserialization & serialization is limited in the popular C# JSON libraries. We provide an exemplar Converter for both, each has different limitations.

System.Text.Json

System.Text.Json does not have any support for Polymorphic deserialization or serialization, so we provide our own implementation. The current implementation cannot inherit the default behaviour for JsonSerializerOptions. We attempt to mirror the basic features needed for Concerto (such as NullHandling and JsonProperty annotations), however other features are not supported.

Newtonsoft JSON.net

Newtonsoft does support basic polymorphic deserialization, however, it doesn't not support support deserialization to a supertype (see SimpleObject_DeserializeToSuperType_Succeeds in ./ConcertoJsonConverter.Tests/newtonsoft/Deserialize.cs).


GitHub license Join the Accord Project Discord

Accord Project is an open source, non-profit, initiative working to transform contract management and contract automation by digitizing contracts. Accord Project operates under the umbrella of the Linux Foundation. The technical charter for the Accord Project can be found here.

Learn More About Accord Project

Contributing

The Accord Project technology is being developed as open source. All the software packages are being actively maintained on GitHub and we encourage organizations and individuals to contribute requirements, documentation, issues, new templates, and code.

Find out what’s coming on our blog.

Join the Accord Project Technology Working Group Discord Community to get involved!

For code contributions, read our CONTRIBUTING guide and information for DEVELOPERS.

README Badge

Using Accord Project? Add a README badge to let everyone know: accord project

[![accord project](https://img.shields.io/badge/powered%20by-accord%20project-19C6C8.svg)](https://www.accordproject.org/)

License

Accord Project source code files are made available under the Apache License, Version 2.0. Accord Project documentation files are made available under the Creative Commons Attribution 4.0 International License (CC-BY-4.0).

Copyright 2018-2019 Clause, Inc. All trademarks are the property of their respective owners. See LF Projects Trademark Policy.