From 4ae14b72159aa34c995ccd19917ba507d8ac6a1d Mon Sep 17 00:00:00 2001 From: Rodrigo Couto Date: Tue, 11 Apr 2017 03:02:29 -0300 Subject: [PATCH] =?UTF-8?q?Ajusta=20custom=20converter=20para=20mudan?= =?UTF-8?q?=C3=A7a=20de=20resposta=20da=20api=20de=20saque?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- iugu.net.UnitTests/ModelAsJsonTests.cs | 18 +++++++++++++ .../JsonCustomConverters/CustomConverters.cs | 25 ++++++++++++++++--- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/iugu.net.UnitTests/ModelAsJsonTests.cs b/iugu.net.UnitTests/ModelAsJsonTests.cs index 47a7663..725af88 100644 --- a/iugu.net.UnitTests/ModelAsJsonTests.cs +++ b/iugu.net.UnitTests/ModelAsJsonTests.cs @@ -4,6 +4,7 @@ using Ploeh.AutoFixture; using FluentAssertions; using System.Threading.Tasks; +using iugu.net.Response; namespace iugu.net.UnitTests { @@ -48,5 +49,22 @@ public async Task Given_a_complete_account_model_when_serialize_should_be_create expectedJson.Should().BeEquivalentTo(serialilizeObj); } + + [Test] + public async Task Given_a_request_withdraw_respose_when_serialize_should_be_success() + { + // Arrage + var inputJson = @"{'id': '530706A3862D4BB49C8AC9637B850CDE','status': 'pending','created_at': '2015-11-26T10:02:23-02:00','updated_at': '2015-11-26T10:02:23-02:00', + 'reference': null,'amount': 'R$ 10,00','account_name': 'Conta','account_id': 'A682CECA59D74527B984CA529D7C2ED4','feedback': null, + 'bank_address': {'bank': 'Bradesco','bank_cc': '11231-2','bank_ag': '1234','account_type': 'Corrente'}}"; + + // Act + var serializesResponse = JsonConvert.DeserializeObject(inputJson); + + Assert.That(serializesResponse.OperationId, Is.EqualTo("530706A3862D4BB49C8AC9637B850CDE")); + Assert.That(serializesResponse.AccountId, Is.EqualTo("A682CECA59D74527B984CA529D7C2ED4")); + Assert.That(serializesResponse.Amount, Is.EqualTo("R$ 10,00")); + Assert.That(serializesResponse.BankInfo.Name, Is.EqualTo("Bradesco")); + } } } diff --git a/iugu.net/JsonCustomConverters/CustomConverters.cs b/iugu.net/JsonCustomConverters/CustomConverters.cs index a194af8..782e2ec 100644 --- a/iugu.net/JsonCustomConverters/CustomConverters.cs +++ b/iugu.net/JsonCustomConverters/CustomConverters.cs @@ -8,19 +8,36 @@ internal sealed class EscapeInvalidQuoteConverter : JsonConverter { public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - writer.WriteValue(value.ToString().Replace(@"""{", "}").Replace(@"}""", "}")); + if (CanApplyCustomConverter(value)) + { + writer.WriteValue(value.ToString().Replace(@"""{", "}").Replace(@"}""", "}")); + } } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var value = JToken.Load(reader).Value(); - var newValue = value.Replace(@"""{", "}").Replace(@"}""", "}").Trim(); - return JsonConvert.DeserializeObject(newValue); + + if (CanApplyCustomConverter(value)) + value = value.Replace(@"""{", "}").Replace(@"}""", "}").Trim(); + + return JsonConvert.DeserializeObject(value); } public override bool CanConvert(Type objectType) { return objectType == typeof(string); } + + private static bool CanApplyCustomConverter(object value) + { + if (value != null) + { + var plainValue = value.ToString(); + return plainValue.Contains(@"""{") && plainValue.Contains(@"}"""); + } + + return false; + } } -} +} \ No newline at end of file