Home > Net >  .NET Core : deserialization issue how to get better insight into non-useful error text
.NET Core : deserialization issue how to get better insight into non-useful error text

Time:07-07

I have the following JSON which I ran through a validator and 'Pasted JSON as classes' to build a model from the schema in Visual Studio:

{
  "entity": {
    "Successful Requests": [
      {
        "Status": 201,
        "Resource ID": 34715818,
        "Message": "Created resource 1 at URI: /rest/equipment/ids/34715818"
      },
      {
        "Status": 201,
        "Resource ID": 34715838,
        "Message": "Created resource 2 at URI: /rest/equipment/ids/34715838"
      }
    ],
    "Failed Requests": [
      {
        "Status": 500,
        "Resource ID": -1,
        "Message": "Failed to create new resource 1. Bulk update failed. Cause: Template 'xxx' not found."
      },
      {
        "Status": 500,
        "Resource ID": -1,
        "Message": "Failed to create new resource 2. Bulk update failed. Cause: Template 'xxx' not found."
      }
    ]
  },
  "variant": {
    "language": null,
    "mediaType": {
      "type": "application",
      "subtype": "json",
      "parameters": {},
      "wildcardType": false,
      "wildcardSubtype": false
    },
    "encoding": null,
    "languageString": null
  },
  "annotations": [],
  "mediaType": {
    "type": "application",
    "subtype": "json",
    "parameters": {},
    "wildcardType": false,
    "wildcardSubtype": false
  },
  "language": null,
  "encoding": null
}

This builds the following classes (I added the JsonProperties in the entity class because the property names contain spaces, I also tried without them and I get the same error):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace xxx.Data.Models
{
    public class Rootobject
    {
        public Entity entity { get; set; }

        public Variant variant { get; set; }
        public object[] annotations { get; set; }
        public Mediatype1 mediaType { get; set; }
        public object language { get; set; }
        public object encoding { get; set; }
    }

    public class Entity
    {
        [JsonProperty("Successful Requests")]
        public SuccessfulRequest[] SuccessfulRequests { get; set; }
        [JsonProperty("Failed Requests")]
        public FailedRequest[] FailedRequests { get; set; }
    }

    public class SuccessfulRequest
    {
        public int Status { get; set; }
        public int ResourceID { get; set; }
        public string Message { get; set; }
    }

    public class FailedRequest
    {
        public int Status { get; set; }
        public int ResourceID { get; set; }
        public string Message { get; set; }
    }

    public class Variant
    {
        public object language { get; set; }
        public Mediatype mediaType { get; set; }
        public object encoding { get; set; }
        public object languageString { get; set; }
    }

    public class Mediatype
    {
        public string type { get; set; }
        public string subtype { get; set; }
        public Parameters parameters { get; set; }
        public bool wildcardType { get; set; }
        public bool wildcardSubtype { get; set; }
    }

    public class Parameters
    {
    }

    public class Mediatype1
    {
        public string type { get; set; }
        public string subtype { get; set; }
        public Parameters1 parameters { get; set; }
        public bool wildcardType { get; set; }
        public bool wildcardSubtype { get; set; }
    }

    public class Parameters1
    {
    }
}

I make an API call in code:

var response = await _repositorySvc.PutBulkEquipment(requestBody, Guid.NewGuid());
var result = response.Content.ReadAsStringAsync().Result;
_logger.LogWarning("------------------Result:");
_logger.LogWarning(result);

Which returns the following JSON string in the console:

{
  "entity": "{\"Successful Requests\":[{\"Status\":201,\"Resource ID\":34715872,\"Message\":\"Created resource 1 at URI: /rest/equipment/ids/34715872\"},{\"Status\":201,\"Resource ID\":34715892,\"Message\":\"Created resource 2 at URI: /rest/equipment/ids/34715892\"}],\"Failed Requests\":[]}",
  "variant": {
    "language": null,
    "mediaType": {
      "type": "application",
      "subtype": "json",
      "parameters": {},
      "wildcardType": false,
      "wildcardSubtype": false
    },
    "encoding": null,
    "languageString": null
  },
  "annotations": [],
  "mediaType": {
    "type": "application",
    "subtype": "json",
    "parameters": {},
    "wildcardType": false,
    "wildcardSubtype": false
  },
  "language": null,
  "encoding": null
}

Then, when I attempt to deserialize the string to the Entity class:

var deserializedResponse = JsonConvert.DeserializeObject<Rootobject>(result);

I get the following error:

Error converting value "{"Successful Requests":[{"Status":201,"Resource ID":34715872,"Message":"Created resource 1 at URI: /rest/equipment/ids/34715872"},{"Status":201,"Resource ID":34715892,"Message":"Created resource 2 at URI: /rest/equipment/ids/34715892"}],"Failed Requests":[]}"
to type 'xxx.Data.Models.Entity'.
Path 'entity', line 1, position 290.

  1. Would anyone be able to spot any mistakes I'm making that I might be missing in the above scenario that could be contributing to me banging my head on a wall for the past day and a half on this? Everything looks correct to me and I'm stumped - I do a lot of deserialization and usually it's ez-mode so I'm not sure what I'm missing.

  2. Is there any way to get better insight into deserialization issues in .NET Core?

Thank you!

CodePudding user response:

If this is the JSON response:

"entity": "{\"Successful Requests\":[...],\"Failed Requests\":[]}",

then entity is of type string. Note the quotes in the beginning and the end. Also note that all the inner quotes are escaped using backslashes.

And the error message says exactly that, it cannot convert that string into an object. it did expect something more like

"entity": {"Successful Requests":[...], "Failed Requests":[]},
  • Related