|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="PartDefinitionDeSerializer.cs" company="RHEA System S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022 RHEA System S.A. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// </copyright> |
| 19 | +// ------------------------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +// ------------------------------------------------------------------------------------------------ |
| 22 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 23 | +// ------------------------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +namespace SysML2.NET.Serializer.Json |
| 26 | +{ |
| 27 | + using System; |
| 28 | + using System.Text.Json; |
| 29 | + |
| 30 | + using SysML2.NET.DTO; |
| 31 | + |
| 32 | + using Microsoft.Extensions.Logging; |
| 33 | + using Microsoft.Extensions.Logging.Abstractions; |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// The purpose of the <see cref="PartDefinitionDeSerializer"/> is to provide deserialization capabilities |
| 37 | + /// for the <see cref="IPartDefinition"/> interface |
| 38 | + /// </summary> |
| 39 | + internal static class PartDefinitionDeSerializer |
| 40 | + { |
| 41 | + /// <summary> |
| 42 | + /// Deserializes an instance of <see cref="IPartDefinition"/> from the provided <see cref="JsonElement"/> |
| 43 | + /// </summary> |
| 44 | + /// <param name="jsonElement"> |
| 45 | + /// The <see cref="JsonElement"/> that contains the <see cref="IPartDefinition"/> json object |
| 46 | + /// </param> |
| 47 | + /// <param name="serializationModeKind"> |
| 48 | + /// enumeration specifying what kind of serialization shall be used |
| 49 | + /// </param> |
| 50 | + /// <param name="loggerFactory"> |
| 51 | + /// The <see cref="ILoggerFactory"/> used to setup logging |
| 52 | + /// </param> |
| 53 | + /// <returns> |
| 54 | + /// an instance of <see cref="IPartDefinition"/> |
| 55 | + /// </returns> |
| 56 | + internal static IPartDefinition DeSerialize(JsonElement jsonElement, SerializationModeKind serializationModeKind, ILoggerFactory loggerFactory = null) |
| 57 | + { |
| 58 | + var logger = loggerFactory == null ? NullLogger.Instance : loggerFactory.CreateLogger("IPartDefinition.DeSerialize"); |
| 59 | + |
| 60 | + if (!jsonElement.TryGetProperty("@type", out JsonElement typeProperty)) |
| 61 | + { |
| 62 | + throw new InvalidOperationException("The @type property is not available, the PartDefinitionDeSerializer cannot be used to deserialize this JsonElement"); |
| 63 | + } |
| 64 | + |
| 65 | + if (typeProperty.GetString() != "PartDefinition") |
| 66 | + { |
| 67 | + throw new InvalidOperationException($"The PartDefinitionDeSerializer can only be used to deserialize objects of type PartDefinition, a {typeProperty.GetString()} was provided"); |
| 68 | + } |
| 69 | + |
| 70 | + var partDefinition = new PartDefinition(); |
| 71 | + |
| 72 | + if (jsonElement.TryGetProperty("@id", out JsonElement idProperty)) |
| 73 | + { |
| 74 | + var propertyValue = idProperty.GetString(); |
| 75 | + if (propertyValue == null) |
| 76 | + { |
| 77 | + throw new JsonException("The @id property is not present, the PartDefinition cannot be deserialized"); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + partDefinition.Id = Guid.Parse(propertyValue); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + if (jsonElement.TryGetProperty("aliasIds", out JsonElement aliasIdsProperty)) |
| 86 | + { |
| 87 | + foreach (var item in aliasIdsProperty.EnumerateArray()) |
| 88 | + { |
| 89 | + partDefinition.AliasIds.Add(item.GetString()); |
| 90 | + } |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + logger.LogDebug($"the aliasIds Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 95 | + } |
| 96 | + |
| 97 | + if (jsonElement.TryGetProperty("elementId", out JsonElement elementIdProperty)) |
| 98 | + { |
| 99 | + var propertyValue = elementIdProperty.GetString(); |
| 100 | + if (propertyValue != null) |
| 101 | + { |
| 102 | + partDefinition.ElementId = propertyValue; |
| 103 | + } |
| 104 | + } |
| 105 | + else |
| 106 | + { |
| 107 | + logger.LogDebug($"the elementId Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 108 | + } |
| 109 | + |
| 110 | + if (jsonElement.TryGetProperty("isAbstract", out JsonElement isAbstractProperty)) |
| 111 | + { |
| 112 | + partDefinition.IsAbstract = isAbstractProperty.GetBoolean(); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + logger.LogDebug($"the isAbstract Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 117 | + } |
| 118 | + |
| 119 | + if (jsonElement.TryGetProperty("isIndividual", out JsonElement isIndividualProperty)) |
| 120 | + { |
| 121 | + partDefinition.IsIndividual = isIndividualProperty.GetBoolean(); |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + logger.LogDebug($"the isIndividual Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 126 | + } |
| 127 | + |
| 128 | + if (jsonElement.TryGetProperty("isSufficient", out JsonElement isSufficientProperty)) |
| 129 | + { |
| 130 | + partDefinition.IsSufficient = isSufficientProperty.GetBoolean(); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + logger.LogDebug($"the isSufficient Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 135 | + } |
| 136 | + |
| 137 | + if (jsonElement.TryGetProperty("isVariation", out JsonElement isVariationProperty)) |
| 138 | + { |
| 139 | + partDefinition.IsVariation = isVariationProperty.GetBoolean(); |
| 140 | + } |
| 141 | + else |
| 142 | + { |
| 143 | + logger.LogDebug($"the isVariation Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 144 | + } |
| 145 | + |
| 146 | + if (jsonElement.TryGetProperty("name", out JsonElement nameProperty)) |
| 147 | + { |
| 148 | + var propertyValue = nameProperty.GetString(); |
| 149 | + if (propertyValue != null) |
| 150 | + { |
| 151 | + partDefinition.Name = propertyValue; |
| 152 | + } |
| 153 | + } |
| 154 | + else |
| 155 | + { |
| 156 | + logger.LogDebug($"the name Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 157 | + } |
| 158 | + |
| 159 | + if (jsonElement.TryGetProperty("ownedRelationship", out JsonElement ownedRelationshipProperty)) |
| 160 | + { |
| 161 | + foreach (var item in ownedRelationshipProperty.EnumerateArray()) |
| 162 | + { |
| 163 | + if (item.TryGetProperty("@id", out JsonElement ownedRelationshipIdProperty)) |
| 164 | + { |
| 165 | + var propertyValue = ownedRelationshipIdProperty.GetString(); |
| 166 | + if (propertyValue != null) |
| 167 | + { |
| 168 | + |
| 169 | + partDefinition.OwnedRelationship.Add(Guid.Parse(propertyValue)); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + logger.LogDebug($"the ownedRelationship Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 177 | + } |
| 178 | + |
| 179 | + if (jsonElement.TryGetProperty("owningRelationship", out JsonElement owningRelationshipProperty)) |
| 180 | + { |
| 181 | + if (owningRelationshipProperty.TryGetProperty("@id", out JsonElement owningRelationshipIdProperty)) |
| 182 | + { |
| 183 | + var propertyValue = owningRelationshipIdProperty.GetString(); |
| 184 | + if (propertyValue != null) |
| 185 | + { |
| 186 | + partDefinition.OwningRelationship = Guid.Parse(propertyValue); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + logger.LogDebug($"the owningRelationship Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 193 | + } |
| 194 | + |
| 195 | + if (jsonElement.TryGetProperty("shortName", out JsonElement shortNameProperty)) |
| 196 | + { |
| 197 | + var propertyValue = shortNameProperty.GetString(); |
| 198 | + if (propertyValue != null) |
| 199 | + { |
| 200 | + partDefinition.ShortName = propertyValue; |
| 201 | + } |
| 202 | + } |
| 203 | + else |
| 204 | + { |
| 205 | + logger.LogDebug($"the shortName Json property was not found in the PartDefinition: {partDefinition.Id}"); |
| 206 | + } |
| 207 | + |
| 208 | + return partDefinition; |
| 209 | + } |
| 210 | + } |
| 211 | +} |
| 212 | + |
| 213 | +// ------------------------------------------------------------------------------------------------ |
| 214 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 215 | +// ------------------------------------------------------------------------------------------------ |
0 commit comments