Skip to content

Commit d1b0514

Browse files
author
sam.gerene
committed
[Add] sample DeSerializationProvider and PartDefinitionDeSerializer classes
1 parent 2168bf3 commit d1b0514

File tree

12 files changed

+654
-8
lines changed

12 files changed

+654
-8
lines changed

SysML2.NET.CodeGenerator.Tests/DataModelLoaderTestFixture.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@
1818
// </copyright>
1919
// ------------------------------------------------------------------------------------------------
2020

21-
using System;
22-
2321
namespace SysML2.NET.CodeGenerator.Tests
2422
{
23+
using System;
2524
using System.Linq;
2625

2726
using ECoreNetto;
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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+
// ------------------------------------------------------------------------------------------------

SysML2.NET.CodeGenerator.Tests/SysML2.NET.CodeGenerator.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20+
<Compile Remove="Expected\AutoGenDeSerializer\PartDefinitionDeSerializer.cs" />
2021
<Compile Remove="Expected\AutoGenEnum\VisibilityKind.cs" />
2122
<Compile Remove="Expected\AutoGenSerializer\ElementSerializer.cs" />
2223
<Compile Remove="Expected\AutoGenSerializer\TriggerInvocationExpressionSerializer.cs" />
@@ -27,6 +28,9 @@
2728
</ItemGroup>
2829

2930
<ItemGroup>
31+
<Content Include="Expected\AutoGenDeSerializer\PartDefinitionDeSerializer.cs">
32+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
33+
</Content>
3034
<Content Include="Expected\AutoGenEnum\VisibilityKind.cs">
3135
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
3236
</Content>

SysML2.NET.CodeGenerator/Generators/HandleBarsGenerators/DtoGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ protected override void RegisterHelpers()
9494
this.Handlebars.RegisteredDocumentationHelper();
9595
this.Handlebars.RegisterTypeNameHelper();
9696
this.Handlebars.RegisterGeneralizationHelper();
97+
this.Handlebars.RegisterStructuralFeatureHelper();
9798
}
9899

99100
/// <summary>

SysML2.NET.CodeGenerator/SysML2.NET.CodeGenerator.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
</PropertyGroup>
2020

2121
<ItemGroup>
22-
<PackageReference Include="ECoreNetto" Version="2.2.0" />
22+
<PackageReference Include="ECoreNetto" Version="2.3.0" />
2323
<PackageReference Include="Handlebars.Net" Version="2.1.2" />
2424
<PackageReference Include="Handlebars.Net.Helpers" Version="2.3.5" />
2525
<PackageReference Include="HtmlAgilityPack" Version="1.11.43" />

SysML2.NET.Serializer.Json.Tests/Data/projects.29845a29-b25b-4bab-b8cc-f46a021b7f5a.commitsec39c63a-fdaa-4a47-98a5-8e8f56b3a986.elements.json renamed to SysML2.NET.Serializer.Json.Tests/Data/projects.29845a29-b25b-4bab-b8cc-f46a021b7f5a.commits.ec39c63a-fdaa-4a47-98a5-8e8f56b3a986.elements.json

File renamed without changes.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="DeSerializerTestFixture.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+
namespace SysML2.NET.Serializer.Json.Tests
22+
{
23+
using System;
24+
using System.Collections.Generic;
25+
using System.IO;
26+
using System.Linq;
27+
28+
using NUnit.Framework;
29+
30+
using SysML2.NET.DTO;
31+
32+
/// <summary>
33+
/// Suite of tests for the <see cref="DeSerializer"/>
34+
/// </summary>
35+
[TestFixture]
36+
public class DeSerializerTestFixture
37+
{
38+
private DeSerializer deSerializer;
39+
40+
[SetUp]
41+
public void SetUp()
42+
{
43+
this.deSerializer = new DeSerializer();
44+
}
45+
46+
[Test]
47+
public void Verify_that_elements_json_can_be_deserialized()
48+
{
49+
var serializationModeKind = SerializationModeKind.JSON;
50+
51+
var fileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, "Data", "projects.29845a29-b25b-4bab-b8cc-f46a021b7f5a.commits.ec39c63a-fdaa-4a47-98a5-8e8f56b3a986.elements.json");
52+
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
53+
{
54+
var elements = this.deSerializer.DeSerialize(stream, serializationModeKind);
55+
56+
Assert.That(elements.OfType<IPartDefinition>().Count(), Is.EqualTo(4));
57+
58+
var partDefinition = elements.OfType<IPartDefinition>().Single(x => x.Id == Guid.Parse("07bd19e6-4587-4bdf-b274-1bbdb4b17707"));
59+
60+
Assert.That(partDefinition.ElementId, Is.EqualTo("07bd19e6-4587-4bdf-b274-1bbdb4b17707"));
61+
Assert.That(partDefinition.IsAbstract, Is.False);
62+
Assert.That(partDefinition.IsIndividual, Is.False);
63+
Assert.That(partDefinition.IsSufficient, Is.False);
64+
Assert.That(partDefinition.IsVariation, Is.False);
65+
Assert.That(partDefinition.Name, Is.EqualTo("AutomaticClutch"));
66+
67+
Assert.That(partDefinition.OwnedRelationship,
68+
Is.EquivalentTo(new List<Guid> { Guid.Parse("d53e1d54-913d-43c1-aa11-e40f87420a5c") }));
69+
70+
Assert.That(partDefinition.OwningRelationship, Is.EqualTo(Guid.Parse("11322389-ecab-42e0-8730-9802f2032d75")));
71+
72+
Assert.That(partDefinition.ShortName, Is.Null);
73+
}
74+
}
75+
}
76+
}

SysML2.NET.Serializer.Json.Tests/SysML2.NET.Serializer.Json.Tests.csproj

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,12 @@
3333
</PackageReference>
3434
</ItemGroup>
3535

36-
<ItemGroup>
37-
<Folder Include="Data\" />
38-
</ItemGroup>
39-
4036
<ItemGroup>
4137
<ProjectReference Include="..\SysML2.NET.Serializer.Json\SysML2.NET.Serializer.Json.csproj" />
4238
</ItemGroup>
4339

4440
<ItemGroup>
45-
<None Update="Data\projects.29845a29-b25b-4bab-b8cc-f46a021b7f5a.commitsec39c63a-fdaa-4a47-98a5-8e8f56b3a986.elements.json">
41+
<None Update="Data\projects.29845a29-b25b-4bab-b8cc-f46a021b7f5a.commits.ec39c63a-fdaa-4a47-98a5-8e8f56b3a986.elements.json">
4642
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
4743
</None>
4844
</ItemGroup>

0 commit comments

Comments
 (0)