Skip to content

Commit

Permalink
Ajusta custom converter para mudança de resposta da api de saque
Browse files Browse the repository at this point in the history
  • Loading branch information
rscouto committed Apr 11, 2017
1 parent b355047 commit 4ae14b7
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
18 changes: 18 additions & 0 deletions iugu.net.UnitTests/ModelAsJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Ploeh.AutoFixture;
using FluentAssertions;
using System.Threading.Tasks;
using iugu.net.Response;

namespace iugu.net.UnitTests
{
Expand Down Expand Up @@ -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<RequestWithdrawResponseMessage>(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"));
}
}
}
25 changes: 21 additions & 4 deletions iugu.net/JsonCustomConverters/CustomConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,36 @@ internal sealed class EscapeInvalidQuoteConverter<T> : 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<string>();
var newValue = value.Replace(@"""{", "}").Replace(@"}""", "}").Trim();
return JsonConvert.DeserializeObject<T>(newValue);

if (CanApplyCustomConverter(value))
value = value.Replace(@"""{", "}").Replace(@"}""", "}").Trim();

return JsonConvert.DeserializeObject<T>(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;
}
}
}
}

0 comments on commit 4ae14b7

Please sign in to comment.