Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when trying to deserialize json string with json.Net

Tags:

c#

json.net

I am encountering a strange error when I try to deserialize the following simple json string:

{ "items": [ "IZ01", "MTSQ", "VM16", "CR05" ]}

The error that I am getting from the json.net dll is the following:

Invalid property identifier character: &. Path '', line 1, position 2.

The full stack trace is as follows:

at Newtonsoft.Json.JsonTextReader.ParseProperty() at Newtonsoft.Json.JsonTextReader.ParseObject() at Newtonsoft.Json.JsonTextReader.ReadInternal() at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CheckedRead(JsonReader reader) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Metastorm.Runtime.Models.SIMSCommonLib.UtilitiesLib.JSONDeserialize(String jsonText, Type valueType) at Metastorm.Runtime.Models.JobProject.ExternalExtendedHelper.GetMaintenanceCodesForVehicle(String LPEntityCode, String UMC, String VIN, String EquipmentList)

This is the method that I am using for deserialising a json string:

    public static object JSONDeserialize(string jsonText, Type valueType)
{
    Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();

    json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
    json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
    json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
    json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

    StringReader sr = new StringReader(jsonText);
    Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);            
    object result = json.Deserialize(reader, valueType);
    reader.Close();

    return result;
}

I am having this issue only in the UAT environment of our application. The strange thing is that the same code and the same data work correctly in our Development and Test environments.

Any idea why json.net is reporting an issue with character "&"? It does not seem to make much sense...

like image 369
xavigar Avatar asked Oct 06 '22 01:10

xavigar


1 Answers

I managed to resolve the issue myself.

It turns out that the string that I was sending to method 'JSONDeserialize' was HTML encoded.

The JSON string, instead of being the following:

{ "items": [ "IZ01", "MTSQ", "VM16", "CR05" ]}

Was actually the following:

{ "items": [ "IZ01", "MTSQ", "VM16", "CR05" ]}

In order to resolve the problem I updated the 'JSONDeserialize' method as follows:

    public static object JSONDeserialize(string jsonText, Type valueType)
{
    // HTML-decode the string, in case it has been HTML encoded
    jsonText = System.Web.HttpUtility.HtmlDecode(jsonText);

    Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();

    json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
    json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
    json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
    json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

    StringReader sr = new StringReader(jsonText);
    Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr);            
    object result = json.Deserialize(reader, valueType);
    reader.Close();

    return result;
}
like image 139
xavigar Avatar answered Oct 10 '22 02:10

xavigar