Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DraftPaymentCreate - Serialization of Pointer object - throws Token PropertyName in state Property would result in an invalid JSON object. when using custom JsonConverter<T> #138

Open
Tieno opened this issue Apr 25, 2020 · 1 comment

Comments

@Tieno
Copy link

Tieno commented Apr 25, 2020

Steps to reproduce:

  1. Try to create a draftpayment

What should happen:

  1. DraftPayment is created

What happens:

  1. Throws exception Token PropertyName in state Property would result in an invalid JSON object. when using custom JsonConverter

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

writer.WriteRaw(BunqJsonConvert.SerializeObject(monetaryAccountReference.Pointer));

should be changed to

writer.WriteRawValue(BunqJsonConvert.SerializeObject(monetaryAccountReference.Pointer));

Like below

/// <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);
        }
    }
@aimi-theo
Copy link

Is this issue solved at the end? I am still receiving the same error :/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants