mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2024-11-19 09:54:30 +01:00
36 lines
1.1 KiB
C#
36 lines
1.1 KiB
C#
using System;
|
|
using System.Reflection;
|
|
using NBitcoin.JsonConverters;
|
|
using Newtonsoft.Json;
|
|
|
|
namespace BTCPayServer.JsonConverters
|
|
{
|
|
public class UriJsonConverter : JsonConverter
|
|
{
|
|
public override bool CanConvert(Type objectType)
|
|
{
|
|
return typeof(Uri).GetTypeInfo().IsAssignableFrom(objectType.GetTypeInfo());
|
|
}
|
|
|
|
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
|
|
{
|
|
try
|
|
{
|
|
return reader.TokenType == JsonToken.Null ? null :
|
|
Uri.TryCreate((string)reader.Value, UriKind.Absolute, out var result) ? result :
|
|
throw new JsonObjectException("Invalid Currency value", reader);
|
|
}
|
|
catch (InvalidCastException)
|
|
{
|
|
throw new JsonObjectException("Invalid Currency value", reader);
|
|
}
|
|
}
|
|
|
|
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
|
|
{
|
|
if (value != null)
|
|
writer.WriteValue(((Uri)value).AbsoluteUri);
|
|
}
|
|
}
|
|
}
|