Skip to content

Commit

Permalink
Add JsonConverters for both COM _and_ ENUM Verwendungszweck
Browse files Browse the repository at this point in the history
wer soll das verstehen?
  • Loading branch information
hf-kklein committed Jan 16, 2024
1 parent deec840 commit 9315268
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using BO4E.ENUM;
using Verwendungszweck = BO4E.COM.Verwendungszweck; // not the enum!!


namespace BO4E.meta.LenientConverters
{
/// <summary>
/// A converter for Verwendungszweck (COM)
/// </summary>
public class VerwendungszweckJsonConverter : JsonConverter<Verwendungszweck>
{
private JsonSerializerOptions defaultOptions = LenientParsing.MOST_LENIENT.GetJsonSerializerOptions();

/// <inheritdoc />
public VerwendungszweckJsonConverter()
{
defaultOptions.Converters.Remove(defaultOptions.Converters.First(c => c.GetType() == typeof(JsonStringEnumConverter)));
defaultOptions.Converters.Add(new JsonStringEnumConverter());
}

/// <inheritdoc />
public override Verwendungszweck? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.StartArray)
{
var retList = new List<BO4E.ENUM.Verwendungszweck>();
reader.Read();
while (reader.TokenType != JsonTokenType.EndArray)
{
retList.Add(System.Text.Json.JsonSerializer.Deserialize<BO4E.ENUM.Verwendungszweck>(ref reader, defaultOptions));
//read end object
reader.Read();
}

return new BO4E.COM.Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
}

if (reader.TokenType == JsonTokenType.String)
{
var retList = new List<BO4E.ENUM.Verwendungszweck>
{
Enum.Parse<BO4E.ENUM.Verwendungszweck>(reader.GetString().ToUpper())
};
return new BO4E.COM.Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
}

return reader.TokenType switch
{
JsonTokenType.Null => null,
JsonTokenType.StartObject => System.Text.Json.JsonSerializer.Deserialize<BO4E.COM.Verwendungszweck>(ref reader, defaultOptions),
_ => null
};
}

/// <inheritdoc />
public override void Write(Utf8JsonWriter writer, BO4E.COM.Verwendungszweck value, JsonSerializerOptions options)
{
System.Text.Json.JsonSerializer.Serialize(writer, value, defaultOptions);
}
}
}
59 changes: 59 additions & 0 deletions BO4E/meta/LenientConverters/LenientVerwendungszweckConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using BO4E.COM;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Marktrolle = BO4E.ENUM.Marktrolle;

namespace BO4E.meta.LenientConverters
{
/// <summary>
/// A converter for Verwendungszweck (ENUM)
/// </summary>
public class VerwendungszweckJsonNetConverter : JsonConverter
{
/// <inheritdoc />
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Verwendungszweck);
}

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

if (reader.TokenType == JsonToken.Integer)
{
var retList = new List<ENUM.Verwendungszweck>
{
Enum.Parse<ENUM.Verwendungszweck>(Enum.GetName(typeof(ENUM.Verwendungszweck), reader.Value))
};
return new Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
}

if (reader.TokenType == JsonToken.String)
{
var retList = new List<ENUM.Verwendungszweck>
{
Enum.Parse<ENUM.Verwendungszweck>((string)reader.Value)
};
return new Verwendungszweck { Marktrolle = Marktrolle.LF, Zweck = retList };
}

return JToken.ReadFrom(reader).ToObject<Verwendungszweck>();
}

/// <inheritdoc />
public override bool CanWrite => true;

/// <inheritdoc />
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
}
}

0 comments on commit 9315268

Please sign in to comment.