Skip to content

SysML2.NET.Serializer.Dictionary

samatrhea edited this page Feb 6, 2023 · 4 revisions

The SysML2.NET.Serializer.Dictionary is used to read and write DTOs from the SysML2.NET from and to a Dictionary<String, Object>. The DTOs from the SysML2.NET.Core.DTO namespace is written and read by the and IWriter and IReader. For each of the DTOs a static reader and writer is code-generated.

DictionaryKind

The DictionaryKind enum is used to specify how the Dictionary is to be read or written from and to a Dictionary<String, Object>. When the DictionaryKind is set to DictionaryKind.Complex then the values that are read from the Dictionary<String, Object> are read as is. When the DictionaryKind is set to DictionaryKind.Simplified then the following applies, the values that are of the following types are read as is:

  • Number, an abstract type, which has the subtypes Integer and Float
  • String
  • Boolean
  • The spatial type Point
  • Temporal types: Date, Time, LocalTime, DateTime, LocalDateTime and Duration
  • values of other types are converted from string, in case these are an IEnumerable<T> then the values are converted from an Array of String using JSON notation, i.e. [ value_1, ..., value_n ]

Reader

The IReader interface exposes 2 methods to read either an instance of IData or an IEnumerable<IData> from a Dictionary<String, Object> or a IEnumerable<Dictionary<string, object>>.

IData Read(Dictionary<string, object> dictionary, DictionaryKind dictionaryKind);

IEnumerable<IData> Read(IEnumerable<Dictionary<string, object>> dictionaries, DictionaryKind dictionaryKind);

The following example demonstrates how to read an instance of IData from a Dictionary<string, object>:

var dictionary = new Dictionary<string, object>
{
    { "@type", "AnnotatingElement" },
    { "@id", Guid.Parse("0b192c18-afa2-44b6-8de2-86e5e6ffa09e") },
    { "aliasIds", new List<string> { "alias_1", "alias_2" } },
    { "annotation", new List<Guid> { Guid.Parse("7f8c06c6-93cb-4193-9a0c-c6ca4dc1eec1") } },
    { "declaredName", "the name" },
    { "declaredShortName", "the shortName" },
    { "elementId", "element id" },
    { "isImpliedIncluded", true },
    { "ownedRelationship", new List<Guid> { Guid.Parse("9006ff06-43fe-4a4e-a4bc-402e82f84dde") } },
    { "owningRelationship", Guid.Parse("fe6d7f0c-6e7b-4ce9-acbe-25d2537f08d9") }
};

var annotatingElement = reader.Read(dictionary, DictionaryKind.Complex) as IAnnotatingElement;

Writer

The IWriter interface exposes 2 methods to write an instance of IData or an IEnumerable<IData> to a Dictionary<String, Object> or a IEnumerable<Dictionary<string, object>>.

Dictionary<string, object> Write(IData dataItem, DictionaryKind dictionaryKind);

IEnumerable<Dictionary<string, object>> Write(IEnumerable<IData> dataItems, DictionaryKind dictionaryKind);

The following example demonstrates how to write an instance of IData to a Dictionary<string, object>:

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",
    ElementId = "PartDefinition:ElementId",
    OwnedRelationship = new List<Guid> { Guid.NewGuid() },
    OwningRelationship = Guid.NewGuid(),
    DeclaredShortName = "PartDefinition:DeclaredShortName"
};

var simpleDictionary = this.writer.Write(partDefinition, DictionaryKind.Simplified);

var complexDictionary = this.writer.Write(this.partDefinition, DictionaryKind.Complex);