Skip to content

Commit

Permalink
Corrige serialização da resposta do pedido de saque
Browse files Browse the repository at this point in the history
  • Loading branch information
rscouto committed Apr 14, 2017
1 parent 4ae14b7 commit e042ba4
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
13 changes: 13 additions & 0 deletions iugu.net.UnitTests/ModelAsJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,18 @@ public async Task Given_a_request_withdraw_respose_when_serialize_should_be_succ
Assert.That(serializesResponse.Amount, Is.EqualTo("R$ 10,00"));
Assert.That(serializesResponse.BankInfo.Name, Is.EqualTo("Bradesco"));
}

[Test]
public async Task Given_a_account_request_withdraw_respose_when_serialize_should_be_success()
{
// Arrage
var inputJson = @"{'id': '2B925E434B324F46827580F6BC0638AB','amount': 'R$ 10,00'}";

// Act
var serializesResponse = JsonConvert.DeserializeObject<AccountRequestWithdrawResponse>(inputJson);

Assert.That(serializesResponse.OperationId, Is.EqualTo("2B925E434B324F46827580F6BC0638AB"));
Assert.That(serializesResponse.WithdrawValue, Is.EqualTo(10.00m));
}
}
}
42 changes: 38 additions & 4 deletions iugu.net/JsonCustomConverters/CustomConverters.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Globalization;

namespace iugu.net.JsonCustomConverters
{
Expand All @@ -16,12 +17,17 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var value = JToken.Load(reader).Value<string>();
var value = JToken.Load(reader);

if (CanApplyCustomConverter(value))
value = value.Replace(@"""{", "}").Replace(@"}""", "}").Trim();
if (value.Type == JTokenType.Object)
return value.ToObject<T>(serializer);

var badFormatValue = value.Value<string>();

if (value.Type == JTokenType.String && CanApplyCustomConverter(badFormatValue))
return JsonConvert.DeserializeObject<T>(badFormatValue.Replace(@"""{", "}").Replace(@"}""", "}").Trim());

return JsonConvert.DeserializeObject<T>(value);
return null;
}

public override bool CanConvert(Type objectType)
Expand All @@ -40,4 +46,32 @@ private static bool CanApplyCustomConverter(object value)
return false;
}
}

internal class BrazilianDecimalConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(decimal) || objectType == typeof(decimal?));
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);

if (token.Type == JTokenType.Float || token.Type == JTokenType.Integer)
return token.ToObject<decimal>();
if (token.Type == JTokenType.String)
return decimal.Parse(token.ToString(), NumberStyles.Currency, CultureInfo.GetCultureInfo("pt-BR"));
if (token.Type == JTokenType.Null && objectType == typeof(decimal?))
return null;

throw new JsonSerializationException("Unexpected token type: " +
token.Type.ToString());
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
writer.WriteValue(value);
}
}
}
6 changes: 4 additions & 2 deletions iugu.net/Response/AccountRequestWithdrawResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using iugu.net.JsonCustomConverters;
using Newtonsoft.Json;

namespace iugu.net.Response
{
Expand All @@ -10,13 +11,14 @@ public class AccountRequestWithdrawResponse
/// <summary>
/// Id que identifica o pedido de saque efetuado
/// </summary>
[JsonProperty("account_id")]
[JsonProperty("id")]
public string OperationId { get; set; }

/// <summary>
/// Valor solicitado para saque
/// </summary>
[JsonProperty("amount")]
[JsonConverter(typeof(BrazilianDecimalConverter))]
public decimal WithdrawValue { get; set; }
}
}

0 comments on commit e042ba4

Please sign in to comment.