- 
                Notifications
    You must be signed in to change notification settings 
- Fork 4
SysML2.NET.Serializer.Json
The SysML2.NET.Serializer.Json is used to serialize and deserialize DTOs from the SysML2.NET class library. The DTOs from both the SysML2.NET.PIM.DTO and SysML2.NET.Core.DTO namespaces are serialized and deserialized by the ISerializer and IDeSerializer interfaces. The SysML2.NET.Serializer.Json makes use of the System.Text.Json package.
For each of the DTOs a static serializer and deserializer is code-generated. The SysML2.NET.Serializer.Json.PIM namespace has been hand-coded, at the time of writing (2023-02-05), no computer readable formal specication (uml/ecore) is availble in the public domain
The library implements the serialization format as specified in Systems Modeling Application Programming Interface (API) andServices. A valid SysML2 JSON document is a JSON Array of IData JSON objects where each JSON object has at least the @id and @type property. The serializer supports both the JSON and JSON-LD format.
The ISerializer interface exposes 4 methods to serialize either an instance of IData or an IEnumerable<IData> to a Stream. Both synchronous and asynchronous serialization is supported.
void Serialize(IEnumerable<IData> dataItems, SerializationModeKind serializationModeKind, Stream stream, JsonWriterOptions jsonWriterOptions);
void Serialize(IData dataItem, SerializationModeKind serializationModeKind, Stream stream, JsonWriterOptions jsonWriterOptions);
Task SerializeAsync(IEnumerable<IData> dataItems, SerializationModeKind serializationModeKind, Stream stream, JsonWriterOptions jsonWriterOptions, CancellationToken cancellationToken);
Task SerializeAsync(IData dataItem, SerializationModeKind serializationModeKind, Stream stream, JsonWriterOptions jsonWriterOptions, CancellationToken cancellationToken);
The following example demonstrates how to serialize a list of IData objects to a MemoryStream:
var partDefinition = new PartDefinition
{
    Id = Guid.NewGuid(),
    IsIndividual = true,
    IsVariation = false,
    IsAbstract = false,
    IsSufficient = false,
    AliasIds = new List<string> { "PartDefinition:Alias_1", "PartDefinition:Alias_2" },
    DeclaredName = "PartDefinition:DeclaredName",
    OwnedRelationship = new List<Guid> { Guid.NewGuid() },
    OwningRelationship = Guid.NewGuid(),
    DeclaredShortName = "PartDefinition:DeclaredShortName"
};
var partDefinitionWithNullProperties = new PartDefinition
{
    Id = Guid.NewGuid(),
    IsIndividual = true,
    IsVariation = false,
    IsAbstract = false,
    IsSufficient = false,
    AliasIds = new List<string>(),
    DeclaredName = null,
    OwnedRelationship = new List<Guid>(),
    OwningRelationship = Guid.NewGuid(),
    DeclaredShortName = null
};
var elements = new List<IElement> { partDefinition, partDefinitionWithNullProperties };
var stream = new MemoryStream();
var jsonWriterOptions = new JsonWriterOptions { Indented = true };
ISerializer serializer = new Serializer();
serializer.Serialize(elements, SerializationModeKind.JSON, stream, jsonWriterOptions)
NOTE: The
JsonWriterOptionsclass is provided by the System.Text.Json library
The IDeSerializer interface exposes 2 methods to deserialize either an IEnumerable<IData> from a Stream. Both synchronous and asynchronous deserialization is supported.
IEnumerable<IData> DeSerialize(Stream stream, SerializationModeKind serializationModeKind, SerializationTargetKind serializationTargetKind);
Task<IEnumerable<IData>> DeSerializeAsync(Stream stream, SerializationModeKind serializationModeKind, SerializationTargetKind serializationTargetKind, CancellationToken cancellationToken);
The following example demonstrates how to deserialize a list of IData objects from a MemoryStream
IEnumerable<IData> data;
var jsonFile = Path.Combine(...some filepath);
using (var stream = new FileStream(jsonFile, FileMode.Open, FileAccess.Read))
{
    data = this.deSerializer.DeSerialize(stream, SerializationModeKind.JSON, SerializationTargetKind.CORE);
}
copyright @ Starion Group S.A.