Switch JsonConverters from 'JsonConverter' to 'JsonConverter<T>'
MODIFY
WalletWasabi.Fluent/Converters/WindowStateAfterStartJsonConverter.cs
WalletWasabi.Fluent/Converters/WindowStateAfterStartJsonConverter.cs
@@ -3,16 +3,10 @@ using Newtonsoft.Json;
namespace WalletWasabi.Fluent.Converters;
-public class WindowStateAfterStartJsonConverter : JsonConverter
+public class WindowStateAfterStartJsonConverter : JsonConverter<string>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
- {
- return objectType == typeof(string);
- }
-
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
+ public override string? ReadJson(JsonReader reader, Type objectType, string? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
try
{
@@ -33,7 +27,7 @@ public class WindowStateAfterStartJsonConverter : JsonConverter
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, string? value, JsonSerializer serializer)
{
writer.WriteValue(value);
}

MODIFY
WalletWasabi/JsonConverters/KeyPathJsonConverter.cs
WalletWasabi/JsonConverters/KeyPathJsonConverter.cs
@@ -3,18 +3,12 @@ using Newtonsoft.Json;
namespace WalletWasabi.JsonConverters;
-public class KeyPathJsonConverter : JsonConverter
+public class KeyPathJsonConverter : JsonConverter<KeyPath>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
+ public override KeyPath? ReadJson(JsonReader reader, Type objectType, KeyPath? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(KeyPath);
- }
-
- /// <inheritdoc />
- public override object? ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- var s = (string)reader.Value;
+ var s = (string?)reader.Value;
if (string.IsNullOrWhiteSpace(s))
{
return null;
@@ -25,11 +19,9 @@ public class KeyPathJsonConverter : JsonConverter
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, KeyPath? value, JsonSerializer serializer)
{
- var kp = (KeyPath)value;
-
- var s = kp.ToString();
+ var s = value?.ToString() ?? throw new ArgumentNullException(nameof(value));
writer.WriteValue(s);
}
}

MODIFY
WalletWasabi/JsonConverters/NetworkJsonConverter.cs
WalletWasabi/JsonConverters/NetworkJsonConverter.cs
@@ -7,19 +7,19 @@ namespace WalletWasabi.JsonConverters;
/// Converter used to convert <see cref="Network"/> to and from JSON.
/// </summary>
/// <seealso cref="JsonConverter" />
-public class NetworkJsonConverter : JsonConverter
+public class NetworkJsonConverter : JsonConverter<Network>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
+ public override Network? ReadJson(JsonReader reader, Type objectType, Network? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(Network);
- }
+ // check additional strings that are not checked by GetNetwork
+ var networkString = ((string?)reader.Value)?.Trim();
- /// <inheritdoc />
- public override object? ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ if (networkString is null)
{
- // check additional strings that are not checked by GetNetwork
- string networkString = ((string)reader.Value).Trim();
+ throw new ArgumentNullException(nameof(networkString));
+ }
+
if ("regression".Equals(networkString, StringComparison.OrdinalIgnoreCase))
{
return Network.RegTest;
@@ -29,8 +29,9 @@ public class NetworkJsonConverter : JsonConverter
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, Network? value, JsonSerializer serializer)
{
- writer.WriteValue(((Network)value).ToString());
+ var network = value?.ToString() ?? throw new ArgumentNullException(nameof(value));
+ writer.WriteValue(network);
}
}

MODIFY
WalletWasabi/JsonConverters/OutPointAsTxoRefJsonConverter.cs
WalletWasabi/JsonConverters/OutPointAsTxoRefJsonConverter.cs
@@ -4,28 +4,38 @@ using Newtonsoft.Json.Linq;
namespace WalletWasabi.JsonConverters;
-public class OutPointAsTxoRefJsonConverter : JsonConverter
+public class OutPointAsTxoRefJsonConverter : JsonConverter<OutPoint>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
+ public override OutPoint? ReadJson(JsonReader reader, Type objectType, OutPoint? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(OutPoint);
+ JObject item = JObject.Load(reader);
+
+ string? hash = item.GetValue("TransactionId", StringComparison.OrdinalIgnoreCase)?.Value<string>();
+ uint? n = item.GetValue("Index", StringComparison.OrdinalIgnoreCase)?.Value<uint>();
+
+ if (hash is null)
+ {
+ throw new ArgumentNullException(nameof(hash));
}
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ if (n is null)
{
- JObject item = JObject.Load(reader);
+ throw new ArgumentNullException(nameof(n));
+ }
- var hash = item.GetValue("TransactionId", StringComparison.OrdinalIgnoreCase).Value<string>();
- var n = item.GetValue("Index", StringComparison.OrdinalIgnoreCase).Value<uint>();
- return new OutPoint(uint256.Parse(hash), n);
+ return new OutPoint(uint256.Parse(hash), n.Value);
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, OutPoint? value, JsonSerializer serializer)
+ {
+ if (value is null)
{
- var outpoint = value as OutPoint;
+ throw new ArgumentNullException(nameof(value));
+ }
+
+ var outpoint = value;
writer.WriteStartObject();
writer.WritePropertyName("TransactionId");
writer.WriteValue(outpoint.Hash.ToString());

MODIFY
WalletWasabi/JsonConverters/OutPointJsonConverter.cs
WalletWasabi/JsonConverters/OutPointJsonConverter.cs
@@ -3,27 +3,24 @@ using Newtonsoft.Json;
namespace WalletWasabi.JsonConverters;
-public class OutPointJsonConverter : JsonConverter
+public class OutPointJsonConverter : JsonConverter<OutPoint>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
+ public override OutPoint? ReadJson(JsonReader reader, Type objectType, OutPoint? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(OutPoint);
- }
-
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ if (reader.Value is string serialized)
{
- var value = (string)reader.Value;
var op = new OutPoint();
- op.FromHex(value);
+ op.FromHex(serialized);
return op;
}
+ throw new ArgumentException($"No valid serialized {nameof(OutPoint)} passed.");
+ }
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, OutPoint? value, JsonSerializer serializer)
{
- string opHex = ((OutPoint)value).ToHex();
+ string opHex = value?.ToHex() ?? throw new ArgumentNullException(nameof(value));
writer.WriteValue(opHex);
}
}

MODIFY
WalletWasabi/JsonConverters/PubKeyJsonConverter.cs
WalletWasabi/JsonConverters/PubKeyJsonConverter.cs
@@ -3,24 +3,25 @@ using Newtonsoft.Json;
namespace WalletWasabi.JsonConverters;
-public class PubKeyJsonConverter : JsonConverter
+public class PubKeyJsonConverter : JsonConverter<PubKey>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
+ public override PubKey? ReadJson(JsonReader reader, Type objectType, PubKey? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(PubKey);
- }
+ string? hex = ((string?)reader.Value)?.Trim();
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ if (hex is null)
{
- return new PubKey(((string)reader.Value).Trim());
+ throw new ArgumentNullException(nameof(hex));
+ }
+
+ return new PubKey(hex);
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, PubKey? value, JsonSerializer serializer)
{
- var pubKey = (PubKey)value;
- writer.WriteValue(pubKey.ToHex());
+ var pubKeyHex = value?.ToHex() ?? throw new ArgumentNullException(nameof(value));
+ writer.WriteValue(pubKeyHex);
}
}

MODIFY
WalletWasabi/JsonConverters/SmartLabelJsonConverter.cs
WalletWasabi/JsonConverters/SmartLabelJsonConverter.cs
@@ -3,24 +3,23 @@ using WalletWasabi.Blockchain.Analysis.Clustering;
namespace WalletWasabi.JsonConverters;
-public class SmartLabelJsonConverter : JsonConverter
+public class SmartLabelJsonConverter : JsonConverter<SmartLabel>
{
- public override bool CanConvert(Type objectType)
+ /// <inheritdoc />
+ public override SmartLabel? ReadJson(JsonReader reader, Type objectType, SmartLabel? existingValue, bool hasExistingValue, JsonSerializer serializer)
+ {
+ if (reader.Value is string serialized)
{
- return objectType == typeof(SmartLabel);
+ return new SmartLabel(serialized);
}
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- var s = reader.Value as string;
- return new SmartLabel(s);
+ throw new ArgumentException($"No valid serialized {nameof(SmartLabel)} passed.");
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, SmartLabel? value, JsonSerializer serializer)
{
- var label = value as SmartLabel;
+ var label = value;
writer.WriteValue(label ?? "");
}
}

MODIFY
WalletWasabi/JsonConverters/Timing/TimeSpanJsonConverter.cs
WalletWasabi/JsonConverters/Timing/TimeSpanJsonConverter.cs
@@ -2,16 +2,10 @@ using Newtonsoft.Json;
namespace WalletWasabi.JsonConverters.Timing;
-public class TimeSpanJsonConverter : JsonConverter
+public class TimeSpanJsonConverter : JsonConverter<TimeSpan>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
- {
- return objectType == typeof(TimeSpan);
- }
-
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ public override TimeSpan ReadJson(JsonReader reader, Type objectType, TimeSpan existingValue, bool hasExistingValue, JsonSerializer serializer)
{
var stringValue = reader.Value as string;
return Parse(stringValue);
@@ -38,9 +32,9 @@ public class TimeSpanJsonConverter : JsonConverter
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, TimeSpan value, JsonSerializer serializer)
{
- var ts = (TimeSpan)value;
+ var ts = value;
writer.WriteValue($"{ts.Days}d {ts.Hours}h {ts.Minutes}m {ts.Seconds}s");
}
}

MODIFY
WalletWasabi/JsonConverters/TransactionJsonConverter.cs
WalletWasabi/JsonConverters/TransactionJsonConverter.cs
@@ -3,25 +3,20 @@ using Newtonsoft.Json;
namespace WalletWasabi.JsonConverters;
-public class TransactionJsonConverter : JsonConverter
+public class TransactionJsonConverter : JsonConverter<Transaction>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
+ public override Transaction? ReadJson(JsonReader reader, Type objectType, Transaction? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(Transaction);
- }
-
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
- {
- var txHex = reader.Value.ToString();
+ var txHex = reader.Value?.ToString();
var tx = Transaction.Parse(txHex, Network.Main);
return tx;
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, Transaction? value, JsonSerializer serializer)
{
- writer.WriteValue(((Transaction)value).ToHex());
+ var txHex = value?.ToHex() ?? throw new ArgumentNullException(nameof(value));
+ writer.WriteValue(txHex);
}
}

MODIFY
WalletWasabi/JsonConverters/Uint256JsonConverter.cs
WalletWasabi/JsonConverters/Uint256JsonConverter.cs
@@ -4,25 +4,18 @@ using WalletWasabi.Helpers;
namespace WalletWasabi.JsonConverters;
-public class Uint256JsonConverter : JsonConverter
+public class Uint256JsonConverter : JsonConverter<uint256>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
+ public override uint256? ReadJson(JsonReader reader, Type objectType, uint256? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(uint256);
+ string? value = (string?)reader.Value;
+ return value is null ? default : new uint256(value);
}
/// <inheritdoc />
- public override object? ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ public override void WriteJson(JsonWriter writer, uint256? value, JsonSerializer serializer)
{
- var value = Guard.Correct((string)reader.Value);
-
- return string.IsNullOrWhiteSpace(value) ? default : new uint256(value);
- }
-
- /// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
- {
- writer.WriteValue(((uint256)value)?.ToString());
+ writer.WriteValue(value?.ToString());
}
}

MODIFY
WalletWasabi/JsonConverters/UnblindedSignatureJsonConverter.cs
WalletWasabi/JsonConverters/UnblindedSignatureJsonConverter.cs
@@ -6,16 +6,10 @@ using WalletWasabi.Crypto;
namespace WalletWasabi.JsonConverters;
-public class UnblindedSignatureJsonConverter : JsonConverter
+public class UnblindedSignatureJsonConverter : JsonConverter<UnblindedSignature>
{
/// <inheritdoc />
- public override bool CanConvert(Type objectType)
- {
- return objectType == typeof(UnblindedSignature);
- }
-
- /// <inheritdoc />
- public override object ReadJson(JsonReader reader, Type? objectType, object? existingValue, JsonSerializer? serializer)
+ public override UnblindedSignature? ReadJson(JsonReader reader, Type objectType, UnblindedSignature? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
JArray arr = JArray.Load(reader);
@@ -29,7 +23,7 @@ public class UnblindedSignatureJsonConverter : JsonConverter
return sig;
}
- private BigInteger StringToBigInteger(string num)
+ private BigInteger StringToBigInteger(string? num)
{
if (string.IsNullOrWhiteSpace(num) || num.Length > 78)
{
@@ -44,9 +38,9 @@ public class UnblindedSignatureJsonConverter : JsonConverter
}
/// <inheritdoc />
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer? serializer)
+ public override void WriteJson(JsonWriter writer, UnblindedSignature? value, JsonSerializer serializer)
{
- var signature = (UnblindedSignature)value;
+ var signature = value ?? throw new ArgumentNullException(nameof(value));
var c = new BigInteger(signature.C.ToBytes(), isUnsigned: true, isBigEndian: true);
var s = new BigInteger(signature.S.ToBytes(), isUnsigned: true, isBigEndian: true);
writer.WriteStartArray();

MODIFY
WalletWasabi/WabiSabi/Crypto/Serialization/GroupElementJsonConverter.cs
WalletWasabi/WabiSabi/Crypto/Serialization/GroupElementJsonConverter.cs
@@ -3,14 +3,10 @@ using WalletWasabi.Crypto.Groups;
namespace WalletWasabi.WabiSabi.Crypto.Serialization;
-public class GroupElementJsonConverter : JsonConverter
+public class GroupElementJsonConverter : JsonConverter<GroupElement>
{
- public override bool CanConvert(Type objectType)
- {
- return objectType == typeof(GroupElement);
- }
-
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ /// <inheritdoc />
+ public override GroupElement? ReadJson(JsonReader reader, Type objectType, GroupElement? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value is string serialized)
{
@@ -19,7 +15,8 @@ public class GroupElementJsonConverter : JsonConverter
throw new ArgumentException($"No valid serialized {nameof(GroupElement)} passed.");
}
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ /// <inheritdoc />
+ public override void WriteJson(JsonWriter writer, GroupElement? value, JsonSerializer serializer)
{
if (value is GroupElement ge)
{

MODIFY
WalletWasabi/WabiSabi/Crypto/Serialization/ScalarJsonConverter.cs
WalletWasabi/WabiSabi/Crypto/Serialization/ScalarJsonConverter.cs
@@ -3,14 +3,10 @@ using Newtonsoft.Json;
namespace WalletWasabi.WabiSabi.Crypto.Serialization;
-public class ScalarJsonConverter : JsonConverter
+public class ScalarJsonConverter : JsonConverter<Scalar>
{
- public override bool CanConvert(Type objectType)
- {
- return objectType == typeof(Scalar);
- }
-
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ /// <inheritdoc />
+ public override Scalar ReadJson(JsonReader reader, Type objectType, Scalar existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.Value is string serialized)
{
@@ -19,7 +15,8 @@ public class ScalarJsonConverter : JsonConverter
throw new ArgumentException($"No valid serialized {nameof(Scalar)} passed.");
}
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ /// <inheritdoc />
+ public override void WriteJson(JsonWriter writer, Scalar value, JsonSerializer serializer)
{
if (value is Scalar scalar)
{

MODIFY
WalletWasabi/WabiSabi/Models/Serialization/OwnershipProofJsonConverter.cs
WalletWasabi/WabiSabi/Models/Serialization/OwnershipProofJsonConverter.cs
@@ -4,22 +4,22 @@ using WalletWasabi.Crypto;
namespace WalletWasabi.WabiSabi.Models.Serialization;
-public class OwnershipProofJsonConverter : JsonConverter
+public class OwnershipProofJsonConverter : JsonConverter<OwnershipProof>
{
- public override bool CanConvert(Type objectType)
+ /// <inheritdoc />
+ public override OwnershipProof? ReadJson(JsonReader reader, Type objectType, OwnershipProof? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(OwnershipProof);
- }
-
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ if (reader.Value is string serialized)
{
- var value = (string)reader.Value;
- return OwnershipProof.FromBytes(ByteHelpers.FromHex(value));
+ return OwnershipProof.FromBytes(ByteHelpers.FromHex(serialized));
+ }
+ throw new ArgumentException($"No valid serialized {nameof(OwnershipProof)} passed.");
}
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ /// <inheritdoc />
+ public override void WriteJson(JsonWriter writer, OwnershipProof? value, JsonSerializer serializer)
{
- var bytes = ((OwnershipProof)value).ToBytes();
+ var bytes = value.ToBytes();
writer.WriteValue(ByteHelpers.ToHex(bytes));
}
}

MODIFY
WalletWasabi/WabiSabi/Models/Serialization/WitScriptJsonConverter.cs
WalletWasabi/WabiSabi/Models/Serialization/WitScriptJsonConverter.cs
@@ -3,22 +3,22 @@ using Newtonsoft.Json;
namespace WalletWasabi.WabiSabi.Models.Serialization;
-public class WitScriptJsonConverter : JsonConverter
+public class WitScriptJsonConverter : JsonConverter<WitScript>
{
- public override bool CanConvert(Type objectType)
+ /// <inheritdoc />
+ public override WitScript? ReadJson(JsonReader reader, Type objectType, WitScript? existingValue, bool hasExistingValue, JsonSerializer serializer)
{
- return objectType == typeof(WitScript);
- }
-
- public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
+ if (reader.Value is string serialized)
{
- var value = (string)reader.Value;
- return new WitScript(ByteHelpers.FromHex(value));
+ return new WitScript(ByteHelpers.FromHex(serialized));
+ }
+ throw new ArgumentException($"No valid serialized {nameof(WitScript)} passed.");
}
- public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
+ /// <inheritdoc />
+ public override void WriteJson(JsonWriter writer, WitScript? value, JsonSerializer serializer)
{
- var bytes = ((WitScript)value).ToBytes();
+ var bytes = value?.ToBytes() ?? throw new ArgumentNullException(nameof(value));
writer.WriteValue(ByteHelpers.ToHex(bytes));
}
}

Switch JsonConverters from 'JsonConverter' to 'JsonConverter<T>'