mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-27 07:57:33 +01:00
39 lines
No EOL
1.2 KiB
C#
39 lines
No EOL
1.2 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace BTCPayServer.Shitcoins.Monero.RPC.Models
|
|
{
|
|
internal class ParseStringConverter : JsonConverter
|
|
{
|
|
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
|
|
|
|
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
|
|
{
|
|
if (reader.TokenType == JsonToken.Null) return null;
|
|
var value = serializer.Deserialize<string>(reader);
|
|
long l;
|
|
if (Int64.TryParse(value, out l))
|
|
{
|
|
return l;
|
|
}
|
|
|
|
throw new Exception("Cannot unmarshal type long");
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
|
|
{
|
|
if (untypedValue == null)
|
|
{
|
|
serializer.Serialize(writer, null);
|
|
return;
|
|
}
|
|
|
|
var value = (long)untypedValue;
|
|
serializer.Serialize(writer, value.ToString(CultureInfo.InvariantCulture));
|
|
return;
|
|
}
|
|
|
|
public static readonly ParseStringConverter Singleton = new ParseStringConverter();
|
|
}
|
|
} |