You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.4 KiB
59 lines
1.4 KiB
1 year ago
|
using System.Text.Json;
|
||
1 year ago
|
using System.Text.Json.Serialization;
|
||
|
|
||
1 year ago
|
namespace StabilityMatrix.Core.Converters.Json;
|
||
1 year ago
|
|
||
|
public class LaunchOptionValueJsonConverter : JsonConverter<object?>
|
||
|
{
|
||
|
public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
var boolValue = reader.GetBoolean();
|
||
|
return boolValue;
|
||
|
}
|
||
|
catch (InvalidOperationException)
|
||
|
{
|
||
|
// ignored
|
||
|
}
|
||
|
|
||
|
try
|
||
|
{
|
||
|
var intValue = reader.GetInt32();
|
||
|
return intValue;
|
||
|
}
|
||
|
catch (InvalidOperationException)
|
||
|
{
|
||
|
// ignored
|
||
|
}
|
||
|
|
||
|
try
|
||
|
{
|
||
|
var strValue = reader.GetString();
|
||
|
return strValue;
|
||
|
}
|
||
|
catch (InvalidOperationException)
|
||
|
{
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public override void Write(Utf8JsonWriter writer, object? value, JsonSerializerOptions options)
|
||
|
{
|
||
|
switch (value)
|
||
|
{
|
||
|
case bool boolValue:
|
||
|
writer.WriteBooleanValue(boolValue);
|
||
|
break;
|
||
|
case int intValue:
|
||
|
writer.WriteNumberValue(intValue);
|
||
|
break;
|
||
|
case string strValue:
|
||
|
writer.WriteStringValue(strValue);
|
||
|
break;
|
||
|
default:
|
||
|
throw new ArgumentOutOfRangeException(nameof(value));
|
||
|
}
|
||
|
}
|
||
|
}
|