Home > Back-end >  System.Text.Json - Unable to deserialize
System.Text.Json - Unable to deserialize

Time:11-24

I'm using System.Text.Json and for some reason it recently broke and doesn't want to deserialize it. This is the exception I'm getting:

{"The JSON value could not be converted to QSGEngine.Web.Exchanges.Exchanges.Binance.AccountTradeResponse. Path: $ | LineNumber: 0 | BytePositionInLine: 1."}

I also want to know how to do a proper error handling, because it used to throw InvalidOperationException (without the current try catch block) and I barely found out that the actual deserialization was causing it.

Executor

try
{
    var asd = JsonSerializer.Deserialize<T>(json, new JsonSerializerOptions
    {
        PropertyNameCaseInsensitive = true,
        NumberHandling = JsonNumberHandling.AllowReadingFromString
    });
}
catch (Exception ex)
{
    Console.WriteLine(ex);
    throw;
}

Model

public class AccountTradeResponse
{
    /// <summary>
    ///     The symbol the trade is for.
    /// </summary>
    public string? Symbol { get; set; }

    /// <summary>
    ///     The id of the trade.
    /// </summary>
    public long Id { get; set; }

    /// <summary>
    ///     The order id the trade belongs to.
    /// </summary>
    public long OrderId { get; set; }

    /// <summary>
    ///     Id of the order list this order belongs to.
    /// </summary>
    public long OrderListId { get; set; }

    /// <summary>
    ///     The price of the trade.
    /// </summary>
    public decimal Price { get; set; }

    /// <summary>
    ///     The quantity of the trade.
    /// </summary>
    [JsonPropertyName("qty")]
    public decimal Quantity { get; set; }

    /// <summary>
    ///     The quote quantity of the trade.
    /// </summary>
    [JsonPropertyName("quoteQty")]
    public decimal QuoteQuantity { get; set; }

    /// <summary>
    ///     The commission paid for the trade.
    /// </summary>
    public decimal Commission { get; set; }

    /// <summary>
    ///     The asset the commission is paid in.
    /// </summary>
    public string CommissionAsset { get; set; } = string.Empty;

    /// <summary>
    ///     The time the trade was made.
    /// </summary>
    [JsonPropertyName("time")]
    [JsonConverter(typeof(UnixEpochDateTimeConverter))]
    public DateTime TradeTime { get; set; }

    /// <summary>
    ///     Whether account was the buyer in the trade.
    /// </summary>
    public bool IsBuyer { get; set; }

    /// <summary>
    ///     Whether account was the maker in the trade.
    /// </summary>
    public bool IsMaker { get; set; }

    /// <summary>
    ///     Whether trade was made with the best match.
    /// </summary>
    public bool IsBestMatch { get; set; }
}

JSON

[
    {
        "symbol": "DOGEUSDT",
        "id": 204483102,
        "orderId": 966983423,
        "orderListId": -1,
        "price": "0.50374000",
        "qty": "1610.10000000",
        "quoteQty": "811.07177400",
        "commission": "0.00091971",
        "commissionAsset": "BNB",
        "time": 1620808028965,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204483833,
        "orderId": 966986806,
        "orderListId": -1,
        "price": "0.50377000",
        "qty": "751.40000000",
        "quoteQty": "378.53277800",
        "commission": "0.00042795",
        "commissionAsset": "BNB",
        "time": 1620808052680,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204483834,
        "orderId": 966986806,
        "orderListId": -1,
        "price": "0.50378000",
        "qty": "451.10000000",
        "quoteQty": "227.25515800",
        "commission": "0.00025692",
        "commissionAsset": "BNB",
        "time": 1620808052680,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204483835,
        "orderId": 966986806,
        "orderListId": -1,
        "price": "0.50379000",
        "qty": "846.60000000",
        "quoteQty": "426.50861400",
        "commission": "0.00048216",
        "commissionAsset": "BNB",
        "time": 1620808052680,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204550496,
        "orderId": 967283575,
        "orderListId": -1,
        "price": "0.49833000",
        "qty": "1343.90000000",
        "quoteQty": "669.70568700",
        "commission": "0.00075325",
        "commissionAsset": "BNB",
        "time": 1620810257212,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204550497,
        "orderId": 967283575,
        "orderListId": -1,
        "price": "0.49831000",
        "qty": "705.30000000",
        "quoteQty": "351.45804300",
        "commission": "0.00039530",
        "commissionAsset": "BNB",
        "time": 1620810257212,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204555645,
        "orderId": 967309447,
        "orderListId": -1,
        "price": "0.49957000",
        "qty": "243.50000000",
        "quoteQty": "121.64529500",
        "commission": "0.00013686",
        "commissionAsset": "BNB",
        "time": 1620810455874,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204555646,
        "orderId": 967309447,
        "orderListId": -1,
        "price": "0.49957000",
        "qty": "1200.00000000",
        "quoteQty": "599.48400000",
        "commission": "0.00067450",
        "commissionAsset": "BNB",
        "time": 1620810455874,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 204555647,
        "orderId": 967309447,
        "orderListId": -1,
        "price": "0.49957000",
        "qty": "600.70000000",
        "quoteQty": "300.09169900",
        "commission": "0.00033764",
        "commissionAsset": "BNB",
        "time": 1620810455874,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 205319583,
        "orderId": 970888125,
        "orderListId": -1,
        "price": "0.44893000",
        "qty": "2789.50000000",
        "quoteQty": "1252.29023500",
        "commission": "0.00148158",
        "commissionAsset": "BNB",
        "time": 1620837978895,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 205319584,
        "orderId": 970888125,
        "orderListId": -1,
        "price": "0.44892000",
        "qty": "864.70000000",
        "quoteQty": "388.18112400",
        "commission": "0.00045925",
        "commissionAsset": "BNB",
        "time": 1620837978895,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 205329838,
        "orderId": 970925901,
        "orderListId": -1,
        "price": "0.45696000",
        "qty": "600.00000000",
        "quoteQty": "274.17600000",
        "commission": "0.00032178",
        "commissionAsset": "BNB",
        "time": 1620838119542,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 205329839,
        "orderId": 970925901,
        "orderListId": -1,
        "price": "0.45698000",
        "qty": "2989.80000000",
        "quoteQty": "1366.27880400",
        "commission": "0.00160345",
        "commissionAsset": "BNB",
        "time": 1620838119542,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207823718,
        "orderId": 980565418,
        "orderListId": -1,
        "price": "0.40569000",
        "qty": "1232.10000000",
        "quoteQty": "499.85064900",
        "commission": "0.00062775",
        "commissionAsset": "BNB",
        "time": 1620897795111,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207823719,
        "orderId": 980565418,
        "orderListId": -1,
        "price": "0.40568000",
        "qty": "214.60000000",
        "quoteQty": "87.05892800",
        "commission": "0.00010933",
        "commissionAsset": "BNB",
        "time": 1620897795111,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207823720,
        "orderId": 980565418,
        "orderListId": -1,
        "price": "0.40566000",
        "qty": "1283.50000000",
        "quoteQty": "520.66461000",
        "commission": "0.00065388",
        "commissionAsset": "BNB",
        "time": 1620897795111,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207823721,
        "orderId": 980565418,
        "orderListId": -1,
        "price": "0.40565000",
        "qty": "246.40000000",
        "quoteQty": "99.95216000",
        "commission": "0.00012552",
        "commissionAsset": "BNB",
        "time": 1620897795111,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207823722,
        "orderId": 980565418,
        "orderListId": -1,
        "price": "0.40565000",
        "qty": "246.40000000",
        "quoteQty": "99.95216000",
        "commission": "0.00012552",
        "commissionAsset": "BNB",
        "time": 1620897795111,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207823723,
        "orderId": 980565418,
        "orderListId": -1,
        "price": "0.40565000",
        "qty": "246.40000000",
        "quoteQty": "99.95216000",
        "commission": "0.00012552",
        "commissionAsset": "BNB",
        "time": 1620897795111,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207823724,
        "orderId": 980565418,
        "orderListId": -1,
        "price": "0.40565000",
        "qty": "120.40000000",
        "quoteQty": "48.84026000",
        "commission": "0.00006133",
        "commissionAsset": "BNB",
        "time": 1620897795111,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207832507,
        "orderId": 980586120,
        "orderListId": -1,
        "price": "0.39999000",
        "qty": "497.70000000",
        "quoteQty": "199.07502300",
        "commission": "0.00025315",
        "commissionAsset": "BNB",
        "time": 1620897886281,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 207832508,
        "orderId": 980586120,
        "orderListId": -1,
        "price": "0.40000000",
        "qty": "3142.90000000",
        "quoteQty": "1257.16000000",
        "commission": "0.00159865",
        "commissionAsset": "BNB",
        "time": 1620897886281,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210005557,
        "orderId": 988560664,
        "orderListId": -1,
        "price": "0.46916000",
        "qty": "1783.90000000",
        "quoteQty": "836.93452400",
        "commission": "0.83693452",
        "commissionAsset": "USDT",
        "time": 1620946151486,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210008659,
        "orderId": 988565404,
        "orderListId": -1,
        "price": "0.47263000",
        "qty": "1856.70000000",
        "quoteQty": "877.53212100",
        "commission": "0.87753212",
        "commissionAsset": "USDT",
        "time": 1620946156213,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210303850,
        "orderId": 989088978,
        "orderListId": -1,
        "price": "0.51366000",
        "qty": "438.00000000",
        "quoteQty": "224.98308000",
        "commission": "0.43800000",
        "commissionAsset": "DOGE",
        "time": 1620947008256,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210303851,
        "orderId": 989088978,
        "orderListId": -1,
        "price": "0.51366000",
        "qty": "791.90000000",
        "quoteQty": "406.76735400",
        "commission": "0.79190000",
        "commissionAsset": "DOGE",
        "time": 1620947008256,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210303852,
        "orderId": 989088978,
        "orderListId": -1,
        "price": "0.51366000",
        "qty": "791.90000000",
        "quoteQty": "406.76735400",
        "commission": "0.79190000",
        "commissionAsset": "DOGE",
        "time": 1620947008256,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210303853,
        "orderId": 989088978,
        "orderListId": -1,
        "price": "0.51366000",
        "qty": "791.90000000",
        "quoteQty": "406.76735400",
        "commission": "0.79190000",
        "commissionAsset": "DOGE",
        "time": 1620947008256,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210303854,
        "orderId": 989088978,
        "orderListId": -1,
        "price": "0.51366000",
        "qty": "82.50000000",
        "quoteQty": "42.37695000",
        "commission": "0.08250000",
        "commissionAsset": "DOGE",
        "time": 1620947008256,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210314125,
        "orderId": 989109658,
        "orderListId": -1,
        "price": "0.50700000",
        "qty": "175.70000000",
        "quoteQty": "89.07990000",
        "commission": "0.08907990",
        "commissionAsset": "USDT",
        "time": 1620947042630,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210314126,
        "orderId": 989109658,
        "orderListId": -1,
        "price": "0.50699000",
        "qty": "2717.60000000",
        "quoteQty": "1377.79602400",
        "commission": "1.37779602",
        "commissionAsset": "USDT",
        "time": 1620947042630,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210600756,
        "orderId": 989693156,
        "orderListId": -1,
        "price": "0.47744000",
        "qty": "1437.70000000",
        "quoteQty": "686.41548800",
        "commission": "1.43770000",
        "commissionAsset": "DOGE",
        "time": 1620948251305,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 210605202,
        "orderId": 989702997,
        "orderListId": -1,
        "price": "0.47842000",
        "qty": "1436.30000000",
        "quoteQty": "687.15464600",
        "commission": "0.68715465",
        "commissionAsset": "USDT",
        "time": 1620948281446,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212077113,
        "orderId": 994567469,
        "orderListId": -1,
        "price": "0.52883000",
        "qty": "1298.00000000",
        "quoteQty": "686.42134000",
        "commission": "1.29800000",
        "commissionAsset": "DOGE",
        "time": 1620970411707,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212077491,
        "orderId": 994568716,
        "orderListId": -1,
        "price": "0.52843000",
        "qty": "1010.60000000",
        "quoteQty": "534.03135800",
        "commission": "1.01060000",
        "commissionAsset": "DOGE",
        "time": 1620970418462,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212077492,
        "orderId": 994568716,
        "orderListId": -1,
        "price": "0.52851000",
        "qty": "288.30000000",
        "quoteQty": "152.36943300",
        "commission": "0.28830000",
        "commissionAsset": "DOGE",
        "time": 1620970418462,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212085477,
        "orderId": 994591607,
        "orderListId": -1,
        "price": "0.52413000",
        "qty": "1411.70000000",
        "quoteQty": "739.91432100",
        "commission": "0.73991432",
        "commissionAsset": "USDT",
        "time": 1620970500613,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212085478,
        "orderId": 994591607,
        "orderListId": -1,
        "price": "0.52413000",
        "qty": "850.00000000",
        "quoteQty": "445.51050000",
        "commission": "0.44551050",
        "commissionAsset": "USDT",
        "time": 1620970500613,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212085479,
        "orderId": 994591607,
        "orderListId": -1,
        "price": "0.52404000",
        "qty": "332.60000000",
        "quoteQty": "174.29570400",
        "commission": "0.17429570",
        "commissionAsset": "USDT",
        "time": 1620970500613,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212091539,
        "orderId": 994610111,
        "orderListId": -1,
        "price": "0.52217000",
        "qty": "1300.70000000",
        "quoteQty": "679.18651900",
        "commission": "1.30070000",
        "commissionAsset": "DOGE",
        "time": 1620970554383,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212093027,
        "orderId": 994613642,
        "orderListId": -1,
        "price": "0.52324000",
        "qty": "1285.10000000",
        "quoteQty": "672.41572400",
        "commission": "1.28510000",
        "commissionAsset": "DOGE",
        "time": 1620970564159,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212132871,
        "orderId": 994745888,
        "orderListId": -1,
        "price": "0.51423000",
        "qty": "2193.00000000",
        "quoteQty": "1127.70639000",
        "commission": "1.12770639",
        "commissionAsset": "USDT",
        "time": 1620971227320,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212132872,
        "orderId": 994745888,
        "orderListId": -1,
        "price": "0.51419000",
        "qty": "390.20000000",
        "quoteQty": "200.63693800",
        "commission": "0.20063694",
        "commissionAsset": "USDT",
        "time": 1620971227320,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212159085,
        "orderId": 994838323,
        "orderListId": -1,
        "price": "0.52464000",
        "qty": "2546.40000000",
        "quoteQty": "1335.94329600",
        "commission": "2.54640000",
        "commissionAsset": "DOGE",
        "time": 1620971749689,
        "isBuyer": true,
        "isMaker": false,
        "isBestMatch": true
    },
    {
        "symbol": "DOGEUSDT",
        "id": 212173954,
        "orderId": 994886589,
        "orderListId": -1,
        "price": "0.52627000",
        "qty": "1169.50000000",
        "quoteQty": "615.47276500",
        "commission": "0.61547277",
        "commissionAsset": "USDT",
        "time": 1620971991876,
        "isBuyer": false,
        "isMaker": false,
        "isBestMatch": true
    }
]

CodePudding user response:

JSON data is array in this code block. So you're trying deserialize to the object. If you chance deserialize function type like List<AccountTradeResponse> or IEnumrable<AccountTradeResponse>

  • Related