btcpayserver/BTCPayServer/JsonConverters/PaymentMethodIdJsonConverter.cs

29 lines
1.2 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
2020-06-24 03:34:09 +02:00
using BTCPayServer.Payments;
2020-06-28 10:55:27 +02:00
using NBitcoin.JsonConverters;
using Newtonsoft.Json;
2020-06-24 03:34:09 +02:00
namespace BTCPayServer.JsonConverters
{
public class PaymentMethodIdJsonConverter : JsonConverter<PaymentMethodId>
{
public override PaymentMethodId ReadJson(JsonReader reader, Type objectType, PaymentMethodId existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
if (reader.TokenType != JsonToken.String)
throw new JsonObjectException("A payment method id should be a string", reader);
if (PaymentMethodId.TryParse((string)reader.Value, out var result))
return result;
2022-08-15 18:45:21 +02:00
return null;
// We need to do this gracefully as we have removed support for a payment type in the past which will throw here on your store each time it is loaded.
// throw new JsonObjectException($"Invalid payment method id ({(string)reader.Value})", reader);
2020-06-24 03:34:09 +02:00
}
public override void WriteJson(JsonWriter writer, PaymentMethodId value, JsonSerializer serializer)
{
if (value != null)
writer.WriteValue(value.ToString());
}
}
}