-
Notifications
You must be signed in to change notification settings - Fork 3
DTO Serialization
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);
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.
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.
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.
copyright @ Starion Group S.A.