DTO Serialization - STARIONGROUP/COMET-SDK-Community-Edition GitHub Wiki

Overview

The CDP4JsonSerializer supports the serialization of a C# object and is performance optimized for DTO objects and the Classless-DTO. The CDP4JsonSerializer supports both serialization to a stream and a to string.

The following snippet demonstrates how to serialize a POCO to a Stream

var metadataprovider = new MetaDataProvider();
var serializer = new Cdp4JsonSerializer(this.metadataprovider, new Version(1, 1, 0));

var siteDirectory = new CDP4Common.SiteDirectoryData.SiteDirectory();
var person = new CDP4Common.SiteDirectoryData.Person();
siteDirectory.person.Add(person);

using (var stream = new MemoryStream())
{
    serializer.SerializeToStream(siteDirectory, stream, true);
}

The following code snippet demonstrates how to serialize a DTO to a Stream

var metadataprovider = new MetaDataProvider();
var serializer = new Cdp4JsonSerializer(this.metadataprovider, new Version(1, 1, 0));

var siteDirectory = new CDP4Common.DTO.SiteDirectory();
using (var stream = new MemoryStream())
{
    serializer.SerializeToStream(siteDirectory, stream);
}

The following snippet demonstrates how to serialize a POCO to a string

var metadataprovider = new MetaDataProvider();
var serializer = new Cdp4JsonSerializer(this.metadataprovider, new Version(1, 1, 0));

var siteDirectory = new CDP4Common.SiteDirectoryData.SiteDirectory();
var person = new CDP4Common.SiteDirectoryData.Person();
siteDirectory.person.Add(person);

var json = serializer.SerializeToStream(siteDirectory, true);

CDP4-COMET Optimized Serialization

The system.text.json library provides means to customize and control how objects are serialized. The JsonSerializer is used for serialization, where dedicated JsonConverter<T> implementations are used for serializing DTO, Enumeration and the Classless-DTO instances.

ThingSerializer

The ThingSerializer derives from the JsonConverter<T>. The Write method is overriden to only serialize those classes and properties that are to be included according to the specified CDP Version. The static SerializerProvider implements the SerializeThing extension method that writes a DTO to the Utf8JsonWriter using the IThingSerializer that corresponds to the DTO that is to be serialized.

ClasslessDtoSerializer

The ClasslessDtoSerializer derives from the JsonConverter<T>. The Write method is overriden to only serialize those classes and properties that are to be included according to the specified CDP Version. The static SerializerProvider implements the SerializeThing extension method writes a Classless-DTO to the Utf8JsonWriter using the IThingSerializer that corresponds to the ClassKind of the content of the Classless-DTO that is to be serialized.