-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JsonConverters for both COM _and_ ENUM
Verwendungszweck
wer soll das verstehen?
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
BO4E/meta/LenientConverters/LenientSystemTextVerwendungszweckConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
59
BO4E/meta/LenientConverters/LenientVerwendungszweckConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |