|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="PrefixCommentDeSerializer.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 Microsoft.Extensions.Logging; |
| 31 | + using Microsoft.Extensions.Logging.Abstractions; |
| 32 | + |
| 33 | + using SysML2.NET.Common; |
| 34 | + using SysML2.NET.Core.DTO; |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// The purpose of the <see cref="PrefixCommentDeSerializer"/> is to provide deserialization capabilities |
| 38 | + /// for the <see cref="IPrefixComment"/> interface |
| 39 | + /// </summary> |
| 40 | + internal static class PrefixCommentDeSerializer |
| 41 | + { |
| 42 | + /// <summary> |
| 43 | + /// Deserializes an instance of <see cref="IPrefixComment"/> from the provided <see cref="JsonElement"/> |
| 44 | + /// </summary> |
| 45 | + /// <param name="jsonElement"> |
| 46 | + /// The <see cref="JsonElement"/> that contains the <see cref="IPrefixComment"/> json object |
| 47 | + /// </param> |
| 48 | + /// <param name="serializationModeKind"> |
| 49 | + /// enumeration specifying what kind of serialization shall be used |
| 50 | + /// </param> |
| 51 | + /// <param name="loggerFactory"> |
| 52 | + /// The <see cref="ILoggerFactory"/> used to setup logging |
| 53 | + /// </param> |
| 54 | + /// <returns> |
| 55 | + /// an instance of <see cref="IPrefixComment"/> |
| 56 | + /// </returns> |
| 57 | + internal static IPrefixComment DeSerialize(JsonElement jsonElement, SerializationModeKind serializationModeKind, ILoggerFactory loggerFactory = null) |
| 58 | + { |
| 59 | + var logger = loggerFactory == null ? NullLogger.Instance : loggerFactory.CreateLogger("PrefixCommentDeSerializer"); |
| 60 | + |
| 61 | + if (!jsonElement.TryGetProperty("@type", out JsonElement @type)) |
| 62 | + { |
| 63 | + throw new InvalidOperationException("The @type property is not available, the PrefixCommentDeSerializer cannot be used to deserialize this JsonElement"); |
| 64 | + } |
| 65 | + |
| 66 | + if (@type.GetString() != "PrefixComment") |
| 67 | + { |
| 68 | + throw new InvalidOperationException($"The PrefixCommentDeSerializer can only be used to deserialize objects of type IPrefixComment, a {@type.GetString()} was provided"); |
| 69 | + } |
| 70 | + |
| 71 | + var dtoInstance = new Core.DTO.PrefixComment(); |
| 72 | + |
| 73 | + if (jsonElement.TryGetProperty("@id", out JsonElement idProperty)) |
| 74 | + { |
| 75 | + var propertyValue = idProperty.GetString(); |
| 76 | + if (propertyValue == null) |
| 77 | + { |
| 78 | + throw new JsonException("The @id property is not present, the PrefixComment cannot be deserialized"); |
| 79 | + } |
| 80 | + else |
| 81 | + { |
| 82 | + dtoInstance.Id = Guid.Parse(propertyValue); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (jsonElement.TryGetProperty("aliasIds", out JsonElement aliasIdsProperty)) |
| 87 | + { |
| 88 | + foreach (var arrayItem in aliasIdsProperty.EnumerateArray()) |
| 89 | + { |
| 90 | + var propertyValue = arrayItem.GetString(); |
| 91 | + if (propertyValue != null) |
| 92 | + { |
| 93 | + dtoInstance.AliasIds.Add(propertyValue); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + logger.LogDebug($"the aliasIds Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 100 | + } |
| 101 | + |
| 102 | + if (jsonElement.TryGetProperty("annotation", out JsonElement annotationProperty)) |
| 103 | + { |
| 104 | + foreach (var arrayItem in annotationProperty.EnumerateArray()) |
| 105 | + { |
| 106 | + if (arrayItem.TryGetProperty("@id", out JsonElement annotationIdProperty)) |
| 107 | + { |
| 108 | + var propertyValue = annotationIdProperty.GetString(); |
| 109 | + if (propertyValue != null) |
| 110 | + { |
| 111 | + dtoInstance.Annotation.Add(Guid.Parse(propertyValue)); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + logger.LogDebug($"the annotation Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 119 | + } |
| 120 | + |
| 121 | + if (jsonElement.TryGetProperty("body", out JsonElement bodyProperty)) |
| 122 | + { |
| 123 | + var propertyValue = bodyProperty.GetString(); |
| 124 | + if (propertyValue != null) |
| 125 | + { |
| 126 | + dtoInstance.Body = propertyValue; |
| 127 | + } |
| 128 | + } |
| 129 | + else |
| 130 | + { |
| 131 | + logger.LogDebug($"the body Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 132 | + } |
| 133 | + |
| 134 | + if (jsonElement.TryGetProperty("elementId", out JsonElement elementIdProperty)) |
| 135 | + { |
| 136 | + var propertyValue = elementIdProperty.GetString(); |
| 137 | + if (propertyValue != null) |
| 138 | + { |
| 139 | + dtoInstance.ElementId = propertyValue; |
| 140 | + } |
| 141 | + } |
| 142 | + else |
| 143 | + { |
| 144 | + logger.LogDebug($"the elementId Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 145 | + } |
| 146 | + |
| 147 | + if (jsonElement.TryGetProperty("locale", out JsonElement localeProperty)) |
| 148 | + { |
| 149 | + var propertyValue = localeProperty.GetString(); |
| 150 | + if (propertyValue != null) |
| 151 | + { |
| 152 | + dtoInstance.Locale = propertyValue; |
| 153 | + } |
| 154 | + } |
| 155 | + else |
| 156 | + { |
| 157 | + logger.LogDebug($"the locale Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 158 | + } |
| 159 | + |
| 160 | + if (jsonElement.TryGetProperty("name", out JsonElement nameProperty)) |
| 161 | + { |
| 162 | + var propertyValue = nameProperty.GetString(); |
| 163 | + if (propertyValue != null) |
| 164 | + { |
| 165 | + dtoInstance.Name = propertyValue; |
| 166 | + } |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + logger.LogDebug($"the name Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 171 | + } |
| 172 | + |
| 173 | + if (jsonElement.TryGetProperty("ownedRelationship", out JsonElement ownedRelationshipProperty)) |
| 174 | + { |
| 175 | + foreach (var arrayItem in ownedRelationshipProperty.EnumerateArray()) |
| 176 | + { |
| 177 | + if (arrayItem.TryGetProperty("@id", out JsonElement ownedRelationshipIdProperty)) |
| 178 | + { |
| 179 | + var propertyValue = ownedRelationshipIdProperty.GetString(); |
| 180 | + if (propertyValue != null) |
| 181 | + { |
| 182 | + dtoInstance.OwnedRelationship.Add(Guid.Parse(propertyValue)); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + else |
| 188 | + { |
| 189 | + logger.LogDebug($"the ownedRelationship Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 190 | + } |
| 191 | + |
| 192 | + if (jsonElement.TryGetProperty("owningRelationship", out JsonElement owningRelationshipProperty)) |
| 193 | + { |
| 194 | + if (owningRelationshipProperty.ValueKind == JsonValueKind.Null) |
| 195 | + { |
| 196 | + dtoInstance.OwningRelationship = null; |
| 197 | + } |
| 198 | + else |
| 199 | + { |
| 200 | + if (owningRelationshipProperty.TryGetProperty("@id", out JsonElement owningRelationshipIdProperty)) |
| 201 | + { |
| 202 | + var propertyValue = owningRelationshipIdProperty.GetString(); |
| 203 | + if (propertyValue != null) |
| 204 | + { |
| 205 | + dtoInstance.OwningRelationship = Guid.Parse(propertyValue); |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + logger.LogDebug($"the owningRelationship Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 213 | + } |
| 214 | + |
| 215 | + if (jsonElement.TryGetProperty("shortName", out JsonElement shortNameProperty)) |
| 216 | + { |
| 217 | + var propertyValue = shortNameProperty.GetString(); |
| 218 | + if (propertyValue != null) |
| 219 | + { |
| 220 | + dtoInstance.ShortName = propertyValue; |
| 221 | + } |
| 222 | + } |
| 223 | + else |
| 224 | + { |
| 225 | + logger.LogDebug($"the shortName Json property was not found in the PrefixComment: {dtoInstance.Id}"); |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + return dtoInstance; |
| 230 | + } |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +// ------------------------------------------------------------------------------------------------ |
| 235 | +// --------THIS IS AN AUTOMATICALLY GENERATED FILE. ANY MANUAL CHANGES WILL BE OVERWRITTEN!-------- |
| 236 | +// ------------------------------------------------------------------------------------------------ |
0 commit comments