DraftPaymentCreate - Serialization of Pointer object - throws Token PropertyName in state Property would result in an invalid JSON object. when using custom JsonConverter<T> #138
/// <summary>
/// Custom (de)serialization of MonetaryAccountReference required to provide compatibility between the two types
/// used to refer to Monetary Accounts: Pointers in requests and Monetary Account Labels in responses.
/// </summary>
public class MonetaryAccountReferenceConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var monetaryAccountReference = (MonetaryAccountReference)value;
if (monetaryAccountReference == null || monetaryAccountReference.Pointer == null)
{
writer.WriteNull();
}
else
{
writer.WriteRawValue(BunqJsonConvert.SerializeObject(monetaryAccountReference.Pointer));
}
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
JsonSerializer serializer)
{
var jObject = JObject.Load(reader);
var labelMonetaryAccount = BunqJsonConvert.DeserializeObject<LabelMonetaryAccount>(jObject.ToString());
return new MonetaryAccountReference(labelMonetaryAccount);
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof(MonetaryAccountReference);
}
}
Steps to reproduce:
What should happen:
What happens:
Extra info:
[//]: # https://stackoverflow.com/questions/54441810/token-propertyname-in-state-property-would-result-in-an-invalid-json-object-whe
Error is located in BunqSdk/Json/MonetaryAccountReferenceConverter.cs
should be changed to
Like below