Skip to content

Commit

Permalink
- Refactor creation of default JsonSerializerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander van Delft committed Nov 27, 2024
1 parent e4da0af commit 2022268
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 93 deletions.
4 changes: 3 additions & 1 deletion CDP4JsonSerializer/Cdp4JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace CDP4JsonSerializer

using CDP4Common.MetaInfo;

using CDP4JsonSerializer.Helper;
using CDP4JsonSerializer.JsonConverter;

using NLog;
Expand Down Expand Up @@ -320,7 +321,8 @@ public T Deserialize<T>(string contentString)
/// </summary>
public virtual void InitializeJsonSerializerOptions()
{
this.JsonSerializerOptions = SerializerOptions.Copy();
this.JsonSerializerOptions = JsonSerializerOptionsCreator.CreateNew();

this.JsonSerializerOptions.Converters.Add(new ThingSerializer(this.MetaInfoProvider, this.RequestDataModelVersion));
this.JsonSerializerOptions.Converters.Add(new ClasslessDtoSerializer(this.MetaInfoProvider, this.RequestDataModelVersion));
this.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter(null, false));
Expand Down
49 changes: 49 additions & 0 deletions CDP4JsonSerializer/Helper/JsonSerializerOptionsCreator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="JsonSerializerOptionsCreator.cs" company="Starion Group S.A.">
// Copyright (c) 2015-2024 Starion Group S.A.
//
// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Nathanael Smiechowski, Antoine Théate, Omar Elebiary, Jaime Bernar
//
// This file is part of CDP4-COMET SDK Community Edition
//
// The CDP4-COMET SDK Community Edition is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// The CDP4-COMET SDK Community Edition is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
// </copyright>
// -------------------------------------------------------------------------------------------------------------------------------

namespace CDP4JsonSerializer.Helper
{
using System.Text.Json;

/// <summary>
/// Static class that returns correct <see cref="JsonSerializerOptions"/> for serialization purposes
/// </summary>
public static class JsonSerializerOptionsCreator
{
/// <summary>
/// Creates the <see cref="JsonSerializerOptions"/> and returns the new instance
/// </summary>
/// <returns>the new instance</returns>
public static JsonSerializerOptions CreateNew()
{
return new JsonSerializerOptions
{
WriteIndented = false,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
PropertyNameCaseInsensitive = true,
NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString,
};
}
}
}
25 changes: 21 additions & 4 deletions CDP4JsonSerializer/Helper/SerializerHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SerializerHelper.cs" company="Starion Group S.A.">
// Copyright (c) 2015-2021 Starion Group S.A.
// Copyright (c) 2015-2024 Starion Group S.A.
//
// Author: Sam Gerené, Merlin Bieze, Alex Vorobiev, Naron Phou, Alexander van Delft
//
Expand Down Expand Up @@ -32,6 +32,8 @@ namespace CDP4JsonSerializer

using CDP4Common.Types;

using CDP4JsonSerializer.Helper;

/// <summary>
/// Utility method to convert a JSON token to a CDP4 type
/// </summary>
Expand All @@ -42,15 +44,30 @@ public static class SerializerHelper
/// </summary>
public const string DateTimeFormat = "yyyy-MM-ddTHH:mm:ss.fffZ";

/// <summary>
/// The default <see cref="System.Text.Json.JsonSerializerOptions"/> instance for this class
/// </summary>
private static readonly JsonSerializerOptions JsonSerializerOptions;

/// <summary>
/// Regex used for conversion of Json value to string
/// </summary>
private static readonly Regex JsonToValueArrayRegex = new(@"^\[(.*)\]$", RegexOptions.Singleline);
private static readonly Regex JsonToValueArrayRegex ;

/// <summary>
/// Regex used for conversion of HStore value to string
/// </summary>
private static readonly Regex HstoreToValueArrayRegex = new(@"^\{(.*)\}$", RegexOptions.Singleline);
private static readonly Regex HstoreToValueArrayRegex;

/// <summary>
/// Creates the new instance of the <see cref="SerializerHelper"/> static class
/// </summary>
static SerializerHelper()
{
JsonSerializerOptions = JsonSerializerOptionsCreator.CreateNew();
JsonToValueArrayRegex = new(@"^\[(.*)\]$", RegexOptions.Singleline);
HstoreToValueArrayRegex = new(@"^\{(.*)\}$", RegexOptions.Singleline);
}

/// <summary>
/// Convert a string to a <see cref="ValueArray{T}"/>
Expand Down Expand Up @@ -227,7 +244,7 @@ private static IEnumerable<string> ValueArrayToStringList(ValueArray<string> val

for (var i = 0; i < items.Count; i++)
{
items[i] = $"{JsonSerializer.Serialize(items[i], SerializerOptions.Options)}";
items[i] = $"{JsonSerializer.Serialize(items[i], JsonSerializerOptions)}";
}

return items;
Expand Down
88 changes: 0 additions & 88 deletions CDP4JsonSerializer/SerializerOptions.cs

This file was deleted.

0 comments on commit 2022268

Please sign in to comment.