Skip to content

DTO Serialization

samatrhea edited this page Aug 21, 2024 · 5 revisions

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 Json.NET library provides means to customize and control how objects are serialized. The JsonSerializer is used for serialization, using the JsonConverter for optimized DTO and Classless-DTO serialization.

ThingSerializer

The ThingSerializer derives from the JsonConverter. The WriteJson 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 ToJsonObject extension method that converts a DTO to a JObject using the IThingSerializer that corresponds to the DTO that is to be serialized.

ClasslessDtoSerializer

The ClasslessDtoSerializer derives from the JsonConverter. The WriteJson 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 ToJsonObject extension method that converts a Classless-DTO to a JObject using the IThingSerializer that corresponds to the ClassKind of the content of the Classless-DTO that is to be serialized.

Clone this wiki locally