I am having an issue with deserializing decimal value correctly. A suggestion on the site was to use a constructor but it is not calling that constructor.
Here is the JSON:
{
"errors": false,
"response": {
"entities": [
{
"currency_id": "1",
"balance": 1e-8,
"address": ""
},
{
"currency_id": "2",
"balance": 0,
"address": null
},
{
"currency_id": "3",
"balance": 0.09865566,
"address": null
},
{
"currency_id": "5",
"balance": 0,
"address": null
},
{
"currency_id": "6",
"balance": 0,
"address": null
}]
},
"pagination": {
"items_per_page": 100,
"total_items": 5,
"current_page": 1,
"total_pages": 1
}
}
My classes:
public class ApiResponse<T> where T : class
{
public bool Errors { get; set; }
public T Response { get; set; }
}
public class ApiPagingResponse<T> : ApiResponse<T> where T : class
{
public Pagination Pagination { get; set; }
}
public class GetBalanceListResponse
{
public GetBalanceListResponseEntity Entity { get; set; }
}
[JsonObject]
public class GetBalanceListResponseEntity
{
[JsonConstructor]
public GetBalanceListResponseEntity([JsonProperty("currency_id")]string currencyId, [JsonProperty("balance")]string balance, [JsonProperty("address")]string address)
{
CurrencyId = currencyId;
Balance = decimal.Parse(balance, NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint,
CultureInfo.InvariantCulture);
Address = address;
}
[JsonProperty("currency_id")]
public string CurrencyId { get; set; }
[JsonProperty("balance")]
public decimal Balance { get; set; }
[JsonProperty("address")]
public string Address { get; set; }
}
I call it using this:
var result = JsonConvert.DeserializeObject<ApiPagingResponse<GetBalanceListResponse>>(stringResult);
Where stringResult
is the json string I want to deserizalize.
Currently it just returns null for the Entity property of the response. All my other serialization works fine with this method, the problem is with "balance": 1e-8,
Has anyone dealt with a similar issue and can be of assistance with this?