Skip to content

Commit

Permalink
Fix Lots of Model Classes; Introduce Json Converters to 100% match th…
Browse files Browse the repository at this point in the history
…e example files (#2)

* Stricter Serialization Tests (WIP)

* wip

* Use System.Text.Json 9.0.0-rc2

so that we get the JsonStringEnumMemberNameAttribute
https://stackoverflow.com/a/78976451/10009545

* wip

* wip

* don't include sub-seconds

* don't include sub-seconds 2

* wip

* wip

* fix everything

* remove BOM

* remove settings

* remove settings 2

* Update README.md

* Fix code style issues with Prettier

* empty commit

---------

Co-authored-by: Konstantin <[email protected]>
Co-authored-by: Lint Action <[email protected]>
  • Loading branch information
3 people authored Oct 10, 2024
1 parent a628280 commit 5f458ca
Show file tree
Hide file tree
Showing 41 changed files with 764 additions and 402 deletions.
6 changes: 2 additions & 4 deletions MaLoIdentModels/MaLoIdentModels.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=827b5381_002Db4e6_002D437e_002D91f5_002D76b43760d70c/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" Name="Test_Request" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="C:\github\malo-ident-net-models\MaLoIdentModels\MaLoIdentModelsTests" Presentation="&amp;lt;MaLoIdentModelsTests&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=fd8f61c1_002D7fe3_002D4070_002D8bfa_002Da09488ff5b82/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Test_Request #2" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AJsonConverterOfT_002EReadCore_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002E_002E_003FUsers_003FKleinKonstantin_003FAppData_003FRoaming_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003Fa7e99f3da3fc9d80c1949ce87d548d992a745092d1c720f44952dbbd144437f_003FJsonConverterOfT_002EReadCore_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=90afe6a5_002D332c_002D4da1_002Da8d5_002D747550c45ccf/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &amp;lt;MaLoIdentModelsTests&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
&lt;Project Location="C:\github\malo-ident-net-models\MaLoIdentModels\MaLoIdentModelsTests" Presentation="&amp;lt;MaLoIdentModelsTests&amp;gt;" /&gt;&#xD;
&lt;/SessionState&gt;</s:String></wpf:ResourceDictionary>
27 changes: 27 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/Address.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
namespace MaLoIdentModels;

using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

public class Address
{
[JsonPropertyName("countryCode")]
[RegularExpression(@"[A-Z]{2}")]
public string CountryCode { get; set; }

[JsonPropertyName("zipCode")]
public string ZipCode { get; set; }

[JsonPropertyName("city")]
public string City { get; set; }

[JsonPropertyName("street")]
public string Street { get; set; }

[JsonPropertyName("houseNumber")]
public int HouseNumber { get; set; }

[JsonPropertyName("houseNumberAddition")]
public string HouseNumberAddition { get; set; }
}
13 changes: 13 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/DataControllableResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class DataControllableResource
{
[JsonPropertyName("srId")]
public string? SrId { get; set; }

[JsonPropertyName("dataControllableResourceMeasuringPointOperators")]
public List<SrMarketPartner>? DataControllableResourceMeasuringPointOperators { get; set; }
}
49 changes: 49 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/DataMarketLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class DataMarketLocation
{
[JsonPropertyName("maloId")]
public string? MaloId { get; set; }

[JsonPropertyName("energyDirection")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public EnergyDirection EnergyDirection { get; set; }

[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonPropertyName("measurementTechnologyClassification")]
public MeasurementTechnologyClassification MeasurementTechnologyClassification { get; set; }

[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonPropertyName("optionalChangeForecastBasis")]
public OptionalChangeForecastBasis OptionalChangeForecastBasis { get; set; }

[JsonPropertyName("dataMarketLocationProperties")]
public List<MarketLocationProperties>? DataMarketLocationProperties { get; set; }

[JsonPropertyName("dataMarketLocationNetworkOperators")]
public List<MarketLocationNetworkOperator>? DataMarketLocationNetworkOperators { get; set; }

[JsonPropertyName("dataMarketLocationMeasuringPointOperators")]
public List<MarketLocationMeasuringPointOperator>? DataMarketLocationMeasuringPointOperators { get; set; }

[JsonPropertyName("dataMarketLocationTransmissionSystemOperators")]
public List<MarketLocationTransmissionSystemOperator>? DataMarketLocationTransmissionSystemOperators { get; set; }

[JsonPropertyName("dataMarketLocationSuppliers")]
public List<MarketLocationSupplier>? DataMarketLocationSuppliers { get; set; }

[JsonPropertyName("dataMarketLocationName")]
public Name? DataMarketLocationName { get; set; }

[JsonPropertyName("dataMarketLocationAddress")]
public Address? DataMarketLocationAddress { get; set; }

[JsonPropertyName("dataMarketLocationLandParcels")]
public List<LandParcel>? DataMarketLocationLandParcels { get; set; }

[JsonPropertyName("dataMarketLocationGeographicCoordinates")]
public GeographicCoordinates? DataMarketLocationGeographicCoordinates { get; set; }
}
19 changes: 19 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/DataMarketLocationProperties.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text.Json.Serialization;
using MaLoIdentModels.JsonSettings;

namespace MaLoIdentModels;

public class DataMarketLocationProperties
{
[JsonPropertyName("marketLocationProperty")]
public MarketLocationProperty MarketLocationProperty { get; set; }

[JsonConverter(typeof(DateTimeOffsetWithTrailingZConverter))]
[JsonPropertyName("executionTimeFrom")]
public DateTimeOffset ExecutionTimeFrom { get; set; }

[JsonConverter(typeof(NullableDateTimeOffsetWithTrailingZConverter))]
[JsonPropertyName("executionTimeUntil")]
public DateTimeOffset? ExecutionTimeUntil { get; set; }
}
16 changes: 16 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/DataMeterLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class DataMeterLocation
{
[JsonPropertyName("meloId")]
public string? MeloId { get; set; }

[JsonPropertyName("meterNumber")]
public string? MeterNumber { get; set; }

[JsonPropertyName("dataMeterLocationMeasuringPointOperators")]
public List<MeterLocationMeasuringPointOperator>? DataMeterLocationMeasuringPointOperators { get; set; }
}
13 changes: 13 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/DataNetworkLocation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class DataNetworkLocation
{
[JsonPropertyName("neloId")]
public string? NeloId { get; set; }

[JsonPropertyName("dataNetworkLocationMeasuringPointOperators")]
public List<NetworkLocationMeasuringPointOperator>? DataNetworkLocationMeasuringPointOperators { get; set; }
}
9 changes: 9 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/DataTechnicalResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class DataTechnicalResource
{
[JsonPropertyName("trId")]
public string? TrId { get; set; }
}
20 changes: 20 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/DataTranche.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class DataTranche
{
[JsonPropertyName("tranchenId")]
public string? TranchenId { get; set; }

[JsonPropertyName("proportion")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public Proportion Proportion { get; set; }

[JsonPropertyName("percent")]
public decimal? Percent { get; set; }

[JsonPropertyName("dataTrancheSuppliers")]
public List<TrancheSupplier>? DataTrancheSuppliers { get; set; }
}
15 changes: 15 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/EnergyDirection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public enum EnergyDirection
{
[EnumMember(Value = "consumption")]
[JsonStringEnumMemberName("consumption")]
Consumption,

[EnumMember(Value = "production")]
[JsonStringEnumMemberName("production")]
Production,
}
28 changes: 28 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/GeographicCoordinates.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Text.Json.Serialization;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
namespace MaLoIdentModels;

public class GeographicCoordinates
{
[JsonPropertyName("latitude")]
public string Latitude { get; set; }

[JsonPropertyName("longitude")]
public string Longitude { get; set; }

[JsonPropertyName("east")]
public string East { get; set; }

[JsonPropertyName("north")]
public string North { get; set; }

[JsonConverter(typeof(JsonStringEnumConverter))]
[JsonPropertyName("zone")]
public Zone? Zone { get; set; }

[JsonPropertyName("northing")]
public string Northing { get; set; }

[JsonPropertyName("easting")]
public string Easting { get; set; }
}
22 changes: 22 additions & 0 deletions MaLoIdentModels/MaLoIdentModels/IdentificationParameter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Text.Json.Serialization;
using MaLoIdentModels.JsonSettings;

namespace MaLoIdentModels;

public class IdentificationParameter
{
[JsonConverter(typeof(DateTimeOffsetWithTrailingZConverter))]
[JsonPropertyName("identificationDateTime")]
public DateTimeOffset IdentificationDateTime { get; set; }

[JsonPropertyName("energyDirection")]
[JsonConverter(typeof(JsonStringEnumConverter))]
public EnergyDirection EnergyDirection { get; set; }

[JsonPropertyName("identificationParameterId")]
public IdentificationParameterIdentificationParameterId? IdentificationParameterId { get; set; }

[JsonPropertyName("identificationParameterAddress")]
public IdentificationParameterIdentificationParameterAddress? IdentificationParameterAddress { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class IdentificationParameterIdentificationParameterAddress
{
[JsonPropertyName("name")]
public Name? Name { get; set; }

[JsonPropertyName("address")]
public Address? Address { get; set; }

[JsonPropertyName("landParcels")]
public List<LandParcel>? LandParcels { get; set; }

[JsonPropertyName("geographicCoordinates")]
public GeographicCoordinates? GeographicCoordinates { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace MaLoIdentModels;

public class IdentificationParameterIdentificationParameterId
{
[JsonPropertyName("maloId")]
public string? MaloId { get; set; }

[JsonPropertyName("tranchenIds")]
public List<string>? TranchenIds { get; set; }

[JsonPropertyName("meloIds")]
public List<string>? MeloIds { get; set; }

[JsonPropertyName("meterNumbers")]
public List<string>? MeterNumbers { get; set; }

[JsonPropertyName("customerNumber")]
public string? CustomerNumber { get; set; }
}
12 changes: 0 additions & 12 deletions MaLoIdentModels/MaLoIdentModels/JsonSettings.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MaLoIdentModels.JsonSettings;

/// <summary>
/// A JsonConverter that ensures, that we serialize DateTimeOffsets with the "Z" suffix as required by Edi@Enery.
/// A plain '+00:00' is not sufficient.
/// </summary>
public class DateTimeOffsetWithTrailingZConverter : JsonConverter<DateTimeOffset>
{
public override DateTimeOffset Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
return DateTimeOffset.Parse(reader.GetString()!);
}

public override void Write(
Utf8JsonWriter writer,
DateTimeOffset value,
JsonSerializerOptions options
)
{
// Always convert the DateTimeOffset to UTC and use the "Z" suffix
writer.WriteStringValue(value.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace MaLoIdentModels.JsonSettings;

/// <summary>
/// Similar to <see cref="JsonSettings.DateTimeOffsetWithTrailingZConverter"/> but for nullable DateTimeOffsets.
/// </summary>
public class NullableDateTimeOffsetWithTrailingZConverter : JsonConverter<DateTimeOffset?>
{
public override DateTimeOffset? Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options
)
{
// Handle deserialization: if the value is null, return null
if (reader.TokenType == JsonTokenType.Null)
{
return null;
}

// Parse the input string to a DateTimeOffset
return DateTimeOffset.Parse(reader.GetString()!);
}

public override void Write(
Utf8JsonWriter writer,
DateTimeOffset? value,
JsonSerializerOptions options
)
{
if (value.HasValue)
{
writer.WriteStringValue(value.Value.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ssZ"));
}
else
{
writer.WriteNullValue();
}
}
}
Loading

0 comments on commit 5f458ca

Please sign in to comment.