Skip to content

Commit

Permalink
Merge branch 'main' into DEV-77895
Browse files Browse the repository at this point in the history
  • Loading branch information
levtoji authored Nov 6, 2024
2 parents c01512e + dec6c7c commit 2da552e
Show file tree
Hide file tree
Showing 10 changed files with 419 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/formatter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
- name: Restore .NET tools
run: dotnet tool restore
- name: Run CSharpier
run: dotnet csharpier --check BO4E-dotnet.sln
run: dotnet csharpier . --check
21 changes: 12 additions & 9 deletions BO4E/BO/Leistungskurvendefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class Leistungskurvendefinition : BusinessObject
[ProtoMember(10)]
[JsonPropertyOrder(10)]
public bool? Ausgerollt { get; set; }



[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[ProtoMember(11, Name = nameof(Aenderungszeitpunkt))]
Expand All @@ -34,7 +33,7 @@ private DateTime _Aenderungszeitpunkt
get => Aenderungszeitpunkt.UtcDateTime;
set => Aenderungszeitpunkt = DateTime.SpecifyKind(value, DateTimeKind.Utc);
}

/// <summary>
/// Leistungskurvenänderungszeitpunkt
/// </summary>
Expand All @@ -44,17 +43,21 @@ private DateTime _Aenderungszeitpunkt
[JsonPropertyOrder(12)]
[ProtoIgnore]
public DateTimeOffset Aenderungszeitpunkt { get; set; }

/// <summary>
/// Code der Leistungskurvendefinition
/// </summary>
[BoKey]
[JsonProperty(Required = Required.Default, Order = 13, PropertyName = "leistungskurvendefinitionscode")]
[JsonProperty(
Required = Required.Default,
Order = 13,
PropertyName = "leistungskurvendefinitionscode"
)]
[JsonPropertyName("leistungskurvendefinitionscode")]
[ProtoMember(13)]
[JsonPropertyOrder(13)]
public string? LeistungskurvendefinitionsCode { get; set; }

/// <summary>
/// Häufigkeit der Übermittlung
/// </summary>
Expand All @@ -63,7 +66,7 @@ private DateTime _Aenderungszeitpunkt
[ProtoMember(14)]
[JsonPropertyOrder(14)]
public HaeufigkeitZaehlzeit? Haeufigkeit { get; set; }

/// <summary>
/// Übermittelbarkeit der ausgerollten Leistungskurvendefinition
/// </summary>
Expand All @@ -72,7 +75,7 @@ private DateTime _Aenderungszeitpunkt
[ProtoMember(15)]
[JsonPropertyOrder(15)]
public UebermittelbarkeitZaehlzeit? Uebermittelbarkeit { get; set; }

/// <summary>
/// oberer Schwellwert
/// </summary>
Expand All @@ -81,4 +84,4 @@ private DateTime _Aenderungszeitpunkt
[ProtoMember(16)]
[JsonPropertyOrder(16)]
public string? ObererSchwellwert { get; set; }
}
}
21 changes: 12 additions & 9 deletions BO4E/BO/Schaltzeitdefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public class Schaltzeitdefinition : BusinessObject
[ProtoMember(10)]
[JsonPropertyOrder(10)]
public bool? Ausgerollt { get; set; }



[Newtonsoft.Json.JsonIgnore]
[System.Text.Json.Serialization.JsonIgnore]
[ProtoMember(11, Name = nameof(Aenderungszeitpunkt))]
Expand All @@ -34,7 +33,7 @@ private DateTime _Aenderungszeitpunkt
get => Aenderungszeitpunkt.UtcDateTime;
set => Aenderungszeitpunkt = DateTime.SpecifyKind(value, DateTimeKind.Utc);
}

/// <summary>
/// Schaltzeitänderungszeitpunkt
/// </summary>
Expand All @@ -44,17 +43,21 @@ private DateTime _Aenderungszeitpunkt
[JsonPropertyOrder(12)]
[ProtoIgnore]
public DateTimeOffset Aenderungszeitpunkt { get; set; }

/// <summary>
/// Code der Schaltzeitdefinition
/// </summary>
[BoKey]
[JsonProperty(Required = Required.Default, Order = 13, PropertyName = "schaltzeitdefinitionscode")]
[JsonProperty(
Required = Required.Default,
Order = 13,
PropertyName = "schaltzeitdefinitionscode"
)]
[JsonPropertyName("schaltzeitdefinitionscode")]
[ProtoMember(13)]
[JsonPropertyOrder(13)]
public string? SchaltzeitdefinitionsCode { get; set; }

/// <summary>
/// Häufigkeit der Übermittlung
/// </summary>
Expand All @@ -63,7 +66,7 @@ private DateTime _Aenderungszeitpunkt
[ProtoMember(14)]
[JsonPropertyOrder(14)]
public HaeufigkeitZaehlzeit? Haeufigkeit { get; set; }

/// <summary>
/// Übermittelbarkeit der ausgerollten Schaltzeitdefinition
/// </summary>
Expand All @@ -72,7 +75,7 @@ private DateTime _Aenderungszeitpunkt
[ProtoMember(15)]
[JsonPropertyOrder(15)]
public UebermittelbarkeitZaehlzeit? Uebermittelbarkeit { get; set; }

/// <summary>
/// Schalthandlung an der Lokation
/// </summary>
Expand All @@ -81,4 +84,4 @@ private DateTime _Aenderungszeitpunkt
[ProtoMember(16)]
[JsonPropertyOrder(16)]
public Schalthandlung? Schalthandlung { get; set; }
}
}
6 changes: 5 additions & 1 deletion BO4E/ENUM/Verwendungszweck.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Runtime.Serialization;
using BO4E.meta.LenientConverters;

namespace BO4E.ENUM;

/// <summary>Verwendungungszweck der Werte Marktlokation</summary>
[System.Text.Json.Serialization.JsonConverter(
typeof(SystemTextVerwendungszweckStringEnumConverter)
)]
[Newtonsoft.Json.JsonConverter(typeof(NewtonsoftVerwendungszweckStringEnumConverter))]
public enum Verwendungszweck
{
/// <summary>Z84: Netznutzungsabrechnung</summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace BO4E.meta.LenientConverters;

/// <summary>
/// Converts a stringified single <see cref="BO4E.ENUM.Verwendungszweck"/>
/// to a <see cref="BO4E.COM.Verwendungszweck"/> which has the single enum value as member in <see cref="COM.Verwendungszweck.Zweck"/>
/// </summary>
/// /// <remarks><seealso cref="SystemTextVerwendungszweckEnumToComConverter"/></remarks>
public class NewtonsoftVerwendungszweckEnumToComConverter
: Newtonsoft.Json.JsonConverter<BO4E.COM.Verwendungszweck?>
{
/// <inheritdoc />
public override bool CanWrite => false;

/// <inheritdoc />
public override void WriteJson(
JsonWriter writer,
BO4E.COM.Verwendungszweck? value,
JsonSerializer serializer
)
{
throw new NotImplementedException(
"This converter is only intended to work with deserialization; Tests show that this alone is sufficient."
);
}

/// <inheritdoc />
public override BO4E.COM.Verwendungszweck? ReadJson(
JsonReader reader,
Type objectType,
BO4E.COM.Verwendungszweck? existingValue,
bool hasExistingValue,
JsonSerializer serializer
)
{
if (reader.TokenType == JsonToken.Null)
{
return null;
}

if (reader.TokenType == JsonToken.String)
{
var result = new BO4E.COM.Verwendungszweck
{
Marktrolle = ENUM.Marktrolle.LF,
Zweck = new List<ENUM.Verwendungszweck>(),
};
var stringValue = (string)reader.Value!;
// we don't want to interfere or re-add the famous and beloved NewtonsoftVerwendungszweckStringEnumConverter
stringValue = stringValue.Replace(
"MEHRMINDERMBENGENABRECHNUNG",
"MEHRMINDERMENGENABRECHNUNG"
);
result.Zweck.Add(
(BO4E.ENUM.Verwendungszweck)
Enum.Parse(typeof(BO4E.ENUM.Verwendungszweck), stringValue)
);
return result;
}

int? stringEnumConverterIndex = null;
foreach (var converter in serializer.Converters)
{
if (converter is NewtonsoftVerwendungszweckStringEnumConverter)
{
stringEnumConverterIndex = serializer.Converters.IndexOf(converter);
break;
}
}
int? thisConverterIndex =
serializer.Converters.IndexOf(this) == -1 ? null : serializer.Converters.IndexOf(this);

if (stringEnumConverterIndex == null)
{
serializer.Converters.Add(new NewtonsoftVerwendungszweckStringEnumConverter());
;
}
if (thisConverterIndex != null)
{
serializer.Converters.RemoveAt(thisConverterIndex.Value);
}
// Delegate to the default behavior for complex objects or other token types
var objectResult = JToken.ReadFrom(reader).ToObject<BO4E.COM.Verwendungszweck>(serializer);
if (thisConverterIndex != null)
{
serializer.Converters.Insert(thisConverterIndex.Value, this);
}
if (stringEnumConverterIndex == null)
{
serializer.Converters.RemoveAt(serializer.Converters.Count - 1);
}

return objectResult;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using BO4E.COM;
using JsonException = System.Text.Json.JsonException;
using JsonSerializer = System.Text.Json.JsonSerializer;

namespace BO4E.meta.LenientConverters;

/// <summary>
/// Converts a stringified single <see cref="BO4E.ENUM.Verwendungszweck"/>
/// to a <see cref="BO4E.COM.Verwendungszweck"/> which has the single enum value as member in <see cref="Verwendungszweck.Zweck"/>
/// </summary>
/// <remarks><seealso cref="NewtonsoftVerwendungszweckEnumToComConverter"/></remarks>
public class SystemTextVerwendungszweckEnumToComConverter
: System.Text.Json.Serialization.JsonConverter<Verwendungszweck?>
{
/// <inheritdoc />
public override Verwendungszweck? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}
if (reader.TokenType == JsonTokenType.String)
{
var result = new Verwendungszweck
{
Marktrolle = ENUM.Marktrolle.LF,
Zweck = new List<ENUM.Verwendungszweck>(),
};
string stringValue = reader.GetString()!;

// Adjust the string as per the Newtonsoft version
stringValue = stringValue.Replace(
"MEHRMINDERMBENGENABRECHNUNG",
"MEHRMINDERMENGENABRECHNUNG"
);

// Parse and add the enum value
if (Enum.TryParse<ENUM.Verwendungszweck>(stringValue, out var enumValue))
{
result.Zweck.Add(enumValue);
}
else
{
throw new JsonException($"Invalid Verwendungszweck value: {stringValue}");
}

return result;
}

// Delegate to the default deserialization behavior for Verwendungszweck
return JsonSerializer.Deserialize<Verwendungszweck>(
ref reader,
CloneJsonSerializerOptionsExceptThis(options)
);
}

private JsonSerializerOptions CloneJsonSerializerOptionsExceptThis(
JsonSerializerOptions options
)
{
var clonedOptions = new JsonSerializerOptions
{
AllowTrailingCommas = options.AllowTrailingCommas,
DefaultBufferSize = options.DefaultBufferSize,
DictionaryKeyPolicy = options.DictionaryKeyPolicy,
DefaultIgnoreCondition = options.DefaultIgnoreCondition,
IgnoreReadOnlyProperties = options.IgnoreReadOnlyProperties,
MaxDepth = options.MaxDepth,
PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive,
PropertyNamingPolicy = options.PropertyNamingPolicy,
ReadCommentHandling = options.ReadCommentHandling,
WriteIndented = options.WriteIndented,
};

foreach (var converter in options.Converters)
{
if (converter.GetType() == GetType())
{
// prevents stackoverflowexception
continue;
}
clonedOptions.Converters.Add(converter);
}
clonedOptions.Converters.Add(new SystemTextVerwendungszweckStringEnumConverter());
return clonedOptions;
}

/// <inheritdoc />
public override void Write(
Utf8JsonWriter writer,
Verwendungszweck? value,
JsonSerializerOptions options
)
{
JsonSerializer.Serialize(writer, value, CloneJsonSerializerOptionsExceptThis(options));
}
}
4 changes: 2 additions & 2 deletions BO4ETestProject/TestBO4E.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<PackageReference Include="FluentAssertions" Version="6.12.1" />
<PackageReference Include="JsonDiffPatch.Net" Version="2.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.6.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.6.2" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand Down
Loading

0 comments on commit 2da552e

Please sign in to comment.