Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SySML2.NET.REST/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ private async Task<IEnumerable<IData>> RequestData(Uri requestUri, CancellationT

using var stream = await response.Content.ReadAsStreamAsync();

var result = await this.deserializer.DeSerializeAsync(stream, SerializationModeKind.JSON, SerializationTargetKind.PSM, cancellationToken);
var result = await this.deserializer.DeSerializeAsync(stream, SerializationModeKind.JSON, SerializationTargetKind.PSM, false, cancellationToken);

return result;
}
Expand Down
2 changes: 1 addition & 1 deletion SysML2.NET.API/Modules/ProjectModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ await WriteResultsToResponse(dataItems, SerializationModeKind.JSON, res, jsonWri
var stream = new MemoryStream();
await req.Body.CopyToAsync(stream, cancellationToken);

var dataItems = await this.deserializer.DeSerializeAsync(stream, SerializationModeKind.JSON, SerializationTargetKind.PSM, CancellationToken.None);
var dataItems = await this.deserializer.DeSerializeAsync(stream, SerializationModeKind.JSON, SerializationTargetKind.PSM, false, CancellationToken.None);

var project = (Project)dataItems.Single();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ internal static class AnnotatingElementDeSerializer
/// <param name="serializationModeKind">
/// enumeration specifying what kind of serialization shall be used
/// </param>
/// <param name="deserializeDerivedProperties">
/// Asserts that the deserializer should deserialize derived properties if present or if they are ignored
/// </param>
/// <param name="loggerFactory">
/// The <see cref="ILoggerFactory"/> used to setup logging
/// </param>
/// <returns>
/// an instance of <see cref="IAnnotatingElement"/>
/// </returns>
internal static IAnnotatingElement DeSerialize(JsonElement jsonElement, SerializationModeKind serializationModeKind, ILoggerFactory loggerFactory = null)
internal static IAnnotatingElement DeSerialize(JsonElement jsonElement, SerializationModeKind serializationModeKind, bool deserializeDerivedProperties, ILoggerFactory loggerFactory = null)
{
var logger = loggerFactory == null ? NullLogger.Instance : loggerFactory.CreateLogger("AnnotatingElementDeSerializer");

Expand Down Expand Up @@ -85,6 +88,33 @@ internal static IAnnotatingElement DeSerialize(JsonElement jsonElement, Serializ
}
}

if (deserializeDerivedProperties)
{
DeserializeDtoIncludingDerivedProperties(dtoInstance, jsonElement, logger);
}
else
{
DeserializeDtoExcludingDerivedProperties(dtoInstance, jsonElement, logger);
}

return dtoInstance;
}

/// <summary>
/// Deserializes properties of a <see cref="AnnotatingElement" />
/// from a <see cref="JsonElement" />, including derived properties
/// </summary>
/// <param name="dtoInstance">
/// The <see cref="AnnotatingElement"/> instance holding deserialized values
/// </param>
/// <param name="jsonElement">
/// The <see cref="JsonElement"/> that contains the <see cref="IAnnotatingElement"/> json object
/// </param>
/// <param name="logger">
/// The <see cref="ILogger"/> to produce logging statement
/// </param>
private static void DeserializeDtoIncludingDerivedProperties(SysML2.NET.Core.DTO.Root.Annotations.AnnotatingElement dtoInstance, JsonElement jsonElement, ILogger logger)
{
if (jsonElement.TryGetProperty("aliasIds"u8, out var aliasIdsProperty))
{
foreach (var arrayItem in aliasIdsProperty.EnumerateArray())
Expand Down Expand Up @@ -465,8 +495,128 @@ internal static IAnnotatingElement DeSerialize(JsonElement jsonElement, Serializ
logger.LogDebug("the textualRepresentation Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

}

/// <summary>
/// Deserializes properties of a <see cref="AnnotatingElement" />
/// from a <see cref="JsonElement" />, excluding derived properties
/// </summary>
/// <param name="dtoInstance">
/// The <see cref="AnnotatingElement"/> instance holding deserialized values
/// </param>
/// <param name="jsonElement">
/// The <see cref="JsonElement"/> that contains the <see cref="IAnnotatingElement"/> json object
/// </param>
/// <param name="logger">
/// The <see cref="ILogger"/> to produce logging statement
/// </param>
private static void DeserializeDtoExcludingDerivedProperties(SysML2.NET.Core.DTO.Root.Annotations.AnnotatingElement dtoInstance, JsonElement jsonElement, ILogger logger)
{
if (jsonElement.TryGetProperty("aliasIds"u8, out var aliasIdsProperty))
{
foreach (var arrayItem in aliasIdsProperty.EnumerateArray())
{
var propertyValue = arrayItem.GetString();

if (propertyValue != null)
{
dtoInstance.AliasIds.Add(propertyValue);
}
}
}
else
{
logger.LogDebug("the aliasIds Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

if (jsonElement.TryGetProperty("declaredName"u8, out var declaredNameProperty))
{
dtoInstance.DeclaredName = declaredNameProperty.GetString();
}
else
{
logger.LogDebug("the declaredName Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

if (jsonElement.TryGetProperty("declaredShortName"u8, out var declaredShortNameProperty))
{
dtoInstance.DeclaredShortName = declaredShortNameProperty.GetString();
}
else
{
logger.LogDebug("the declaredShortName Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

if (jsonElement.TryGetProperty("elementId"u8, out var elementIdProperty))
{
var propertyValue = elementIdProperty.GetString();

if (propertyValue != null)
{
dtoInstance.ElementId = propertyValue;
}
}
else
{
logger.LogDebug("the elementId Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

if (jsonElement.TryGetProperty("isImpliedIncluded"u8, out var isImpliedIncludedProperty))
{
if (isImpliedIncludedProperty.ValueKind != JsonValueKind.Null)
{
dtoInstance.IsImpliedIncluded = isImpliedIncludedProperty.GetBoolean();
}
}
else
{
logger.LogDebug("the isImpliedIncluded Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

if (jsonElement.TryGetProperty("ownedRelationship"u8, out var ownedRelationshipProperty))
{
foreach (var arrayItem in ownedRelationshipProperty.EnumerateArray())
{
if (arrayItem.TryGetProperty("@id"u8, out var ownedRelationshipExternalIdProperty))
{
var propertyValue = ownedRelationshipExternalIdProperty.GetString();

if (propertyValue != null)
{
dtoInstance.OwnedRelationship.Add(Guid.Parse(propertyValue));
}
}
}
}
else
{
logger.LogDebug("the ownedRelationship Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

if (jsonElement.TryGetProperty("owningRelationship"u8, out var owningRelationshipProperty))
{
if (owningRelationshipProperty.ValueKind == JsonValueKind.Null)
{
dtoInstance.OwningRelationship = null;
}
else
{
if (owningRelationshipProperty.TryGetProperty("@id"u8, out var owningRelationshipExternalIdProperty))
{
var propertyValue = owningRelationshipExternalIdProperty.GetString();

if (propertyValue != null)
{
dtoInstance.OwningRelationship = Guid.Parse(propertyValue);
}
}
}
}
else
{
logger.LogDebug("the owningRelationship Json property was not found in the AnnotatingElement: { Id }", dtoInstance.Id);
}

return dtoInstance;
}
}
}
Expand Down
Loading
Loading