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.
58 lines
1.4 KiB
58 lines
1.4 KiB
using System.Text.Json; |
|
using System.Text.Json.Serialization; |
|
|
|
namespace StabilityMatrix.Core.Converters.Json; |
|
|
|
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)); |
|
} |
|
} |
|
}
|
|
|