Skip to content

Commit d76908d

Browse files
JSON Deserialization ok, JSON Tests are passing
1 parent 9efe151 commit d76908d

182 files changed

Lines changed: 8628 additions & 8942 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="UmlCoreJsonDtoSerializerGeneratorTestFixture.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group 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.CodeGenerator.Tests.Generators.UmlHandleBarsGenerators
22+
{
23+
using System.IO;
24+
using System.Threading.Tasks;
25+
26+
using NUnit.Framework;
27+
28+
using SysML2.NET.CodeGenerator.Generators.UmlHandleBarsGenerators;
29+
30+
[TestFixture]
31+
public class UmlCoreJsonDtoDeSerializerGeneratorTestFixture
32+
{
33+
private DirectoryInfo umlPocoDirectoryInfo;
34+
private UmlCoreJsonDtoDeSerializerGenerator umlCoreDtoDeSerializerGenerator;
35+
36+
[OneTimeSetUp]
37+
public void OneTimeSetup()
38+
{
39+
var directoryInfo = new DirectoryInfo(TestContext.CurrentContext.TestDirectory);
40+
41+
var path = Path.Combine("UML", "_SysML2.NET.Serializer.Json.Core.AutoGenDeSerializer");
42+
43+
this.umlPocoDirectoryInfo = directoryInfo.CreateSubdirectory(path);
44+
this.umlCoreDtoDeSerializerGenerator = new UmlCoreJsonDtoDeSerializerGenerator();
45+
}
46+
47+
[Test]
48+
public async Task VerifyCanGenerateDtoJsonDeSerializer()
49+
{
50+
await Assert.ThatAsync(() => this.umlCoreDtoDeSerializerGenerator.GenerateAsync(GeneratorSetupFixture.XmiReaderResult, this.umlPocoDirectoryInfo), Throws.Nothing);
51+
}
52+
}
53+
}
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="UmlCoreJsonDtoDeSerializerGenerator.cs" company="Starion Group S.A.">
3+
//
4+
// Copyright 2022-2025 Starion Group 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.CodeGenerator.Generators.UmlHandleBarsGenerators
22+
{
23+
using System;
24+
using System.IO;
25+
using System.Linq;
26+
using System.Threading.Tasks;
27+
28+
using uml4net.Extensions;
29+
using uml4net.HandleBars;
30+
using uml4net.SimpleClassifiers;
31+
using uml4net.StructuredClassifiers;
32+
using uml4net.xmi.Readers;
33+
34+
using NamedElementHelper = SysML2.NET.CodeGenerator.HandleBarHelpers.NamedElementHelper;
35+
using PropertyHelper = SysML2.NET.CodeGenerator.HandleBarHelpers.PropertyHelper;
36+
37+
/// <summary>
38+
/// A UML Handlebars based DTO Json DeSerializer code generator
39+
/// </summary>
40+
public class UmlCoreJsonDtoDeSerializerGenerator : UmlHandleBarsGenerator
41+
{
42+
/// <summary>
43+
/// Gets the name of the template used to generate DTO Json DeSerializaer
44+
/// </summary>
45+
private const string DtoDeSerializerTemplateName = "core-json-dto-deserializer-uml-template";
46+
47+
/// <summary>
48+
/// Gets the name of the template used to generate Enum Json DeSerializaer
49+
/// </summary>
50+
private const string EnumDeSerializerTemplateName = "core-json-enum-deserializer-uml-template";
51+
52+
/// <summary>
53+
/// Gets the name of the template used to generate Json DeSerializer provider
54+
/// </summary>
55+
private const string DtoDeSerializerProviderTemplateName = "core-json-dto-deserialization-provider-uml-template";
56+
57+
/// <summary>
58+
/// Generates code specific to the concrete implementation
59+
/// </summary>
60+
/// <param name="xmiReaderResult">
61+
/// the <see cref="XmiReaderResult" /> that contains the UML model to generate from
62+
/// </param>
63+
/// <param name="outputDirectory">
64+
/// The target <see cref="DirectoryInfo" />
65+
/// </param>
66+
/// <returns>
67+
/// an awaitable <see cref="Task" />
68+
/// </returns>
69+
public override async Task GenerateAsync(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
70+
{
71+
await this.GenerateDtoJsonDeSerializer(xmiReaderResult, outputDirectory);
72+
await this.GenerateDeSerializationProvider(xmiReaderResult, outputDirectory);
73+
await this.GenerateEnumJsonDeSerializer(xmiReaderResult, outputDirectory);
74+
}
75+
76+
/// <summary>
77+
/// Generates Enum Json DeSerializer files
78+
/// </summary>
79+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
80+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
81+
/// <returns>
82+
/// an awaitable <see cref="Task" />
83+
/// </returns>
84+
/// <exception cref="ArgumentNullException">
85+
/// In case of null value for <paramref name="xmiReaderResult" /> or
86+
/// <paramref name="outputDirectory" />
87+
/// </exception>
88+
private Task GenerateEnumJsonDeSerializer(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
89+
{
90+
ArgumentNullException.ThrowIfNull(xmiReaderResult);
91+
ArgumentNullException.ThrowIfNull(outputDirectory);
92+
93+
return this.GenerateEnumJsonDeSerializerInternal(xmiReaderResult, outputDirectory);
94+
}
95+
96+
/// <summary>
97+
/// Generates Enum Json DeSerializer files
98+
/// </summary>
99+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
100+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
101+
/// <returns>
102+
/// an awaitable <see cref="Task" />
103+
/// </returns>
104+
private async Task GenerateEnumJsonDeSerializerInternal(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
105+
{
106+
var template = this.Templates[EnumDeSerializerTemplateName];
107+
108+
var enumerations = xmiReaderResult.Root.QueryPackages()
109+
.SelectMany(x => x.PackagedElement.OfType<IEnumeration>())
110+
.ToList();
111+
112+
foreach (var enumeration in enumerations)
113+
{
114+
var generatedInterface = template(enumeration);
115+
116+
generatedInterface = this.CodeCleanup(generatedInterface);
117+
118+
var fileName = $"{enumeration.Name.CapitalizeFirstLetter()}DeSerializer.cs";
119+
120+
await WriteAsync(generatedInterface, outputDirectory, fileName);
121+
}
122+
}
123+
124+
/// <summary>
125+
/// Register the custom helpers
126+
/// </summary>
127+
protected override void RegisterHelpers()
128+
{
129+
this.Handlebars.RegisterStringHelper();
130+
this.Handlebars.RegisterClassHelper();
131+
this.Handlebars.RegisterPropertyHelper();
132+
133+
NamedElementHelper.RegisterNamedElementHelper(this.Handlebars);
134+
PropertyHelper.RegisterPropertyHelper(this.Handlebars);
135+
}
136+
137+
/// <summary>
138+
/// Register the code templates
139+
/// </summary>
140+
protected override void RegisterTemplates()
141+
{
142+
this.RegisterTemplate(DtoDeSerializerTemplateName);
143+
this.RegisterTemplate(DtoDeSerializerProviderTemplateName);
144+
this.RegisterTemplate(EnumDeSerializerTemplateName);
145+
}
146+
147+
/// <summary>
148+
/// Generates the DeSerialization Provider class
149+
/// </summary>
150+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
151+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
152+
/// <returns>
153+
/// an awaitable <see cref="Task" />
154+
/// </returns>
155+
/// <exception cref="ArgumentNullException">
156+
/// In case of null value for <paramref name="xmiReaderResult" /> or
157+
/// <paramref name="outputDirectory" />
158+
/// </exception>
159+
private Task GenerateDeSerializationProvider(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
160+
{
161+
ArgumentNullException.ThrowIfNull(xmiReaderResult);
162+
ArgumentNullException.ThrowIfNull(outputDirectory);
163+
164+
return this.GenerateDeSerializationProviderInternal(xmiReaderResult, outputDirectory);
165+
}
166+
167+
/// <summary>
168+
/// Generates the DeSerialization Provider class
169+
/// </summary>
170+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
171+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
172+
/// <returns>
173+
/// an awaitable <see cref="Task" />
174+
/// </returns>
175+
private async Task GenerateDeSerializationProviderInternal(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
176+
{
177+
var template = this.Templates[DtoDeSerializerProviderTemplateName];
178+
179+
var classes = xmiReaderResult.Root.QueryPackages()
180+
.SelectMany(x => x.PackagedElement.OfType<IClass>())
181+
.Where(x => !x.IsAbstract)
182+
.OrderBy(x => x.Name)
183+
.ToList();
184+
185+
var generatedSerializationProvider = template(classes);
186+
187+
generatedSerializationProvider = this.CodeCleanup(generatedSerializationProvider);
188+
189+
const string fileName = "DeSerializationProvider.cs";
190+
191+
await Write(generatedSerializationProvider, outputDirectory, fileName);
192+
}
193+
194+
/// <summary>
195+
/// Generates DTO Json DeSerializer files
196+
/// </summary>
197+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
198+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
199+
/// <returns>
200+
/// an awaitable <see cref="Task" />
201+
/// </returns>
202+
/// <exception cref="ArgumentNullException">
203+
/// In case of null value for <paramref name="xmiReaderResult" /> or
204+
/// <paramref name="outputDirectory" />
205+
/// </exception>
206+
private Task GenerateDtoJsonDeSerializer(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
207+
{
208+
ArgumentNullException.ThrowIfNull(xmiReaderResult);
209+
ArgumentNullException.ThrowIfNull(outputDirectory);
210+
211+
return this.GenerateDtoJsonDeSerializerInternal(xmiReaderResult, outputDirectory);
212+
}
213+
214+
/// <summary>
215+
/// Generates DTO Json Serializer files
216+
/// </summary>
217+
/// <param name="xmiReaderResult">the <see cref="XmiReaderResult" /> that contains the UML model to generate from</param>
218+
/// <param name="outputDirectory">The target <see cref="DirectoryInfo" /></param>
219+
/// <returns>
220+
/// an awaitable <see cref="Task" />
221+
/// </returns>
222+
private async Task GenerateDtoJsonDeSerializerInternal(XmiReaderResult xmiReaderResult, DirectoryInfo outputDirectory)
223+
{
224+
var template = this.Templates[DtoDeSerializerTemplateName];
225+
226+
var classes = xmiReaderResult.Root.QueryPackages()
227+
.SelectMany(x => x.PackagedElement.OfType<IClass>())
228+
.Where(x => !x.IsAbstract)
229+
.ToList();
230+
231+
foreach (var @class in classes)
232+
{
233+
var generatedInterface = template(@class);
234+
235+
generatedInterface = this.CodeCleanup(generatedInterface);
236+
237+
var fileName = $"{@class.Name.CapitalizeFirstLetter()}DeSerializer.cs";
238+
239+
await WriteAsync(generatedInterface, outputDirectory, fileName);
240+
}
241+
}
242+
}
243+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@
215215
<None Update="Templates\core-json-dto-deserializer-uml-template.hbs">
216216
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
217217
</None>
218+
<None Update="Templates\core-json-dto-deserialization-provider-uml-template.hbs">
219+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
220+
</None>
221+
<None Update="Templates\core-json-enum-deserializer-uml-template.hbs">
222+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
223+
</None>
218224
</ItemGroup>
219225

220226
<ItemGroup>

0 commit comments

Comments
 (0)