Newtonsoft JSON.NET deserialization error

I have a bunch of long json output in separate files. I need to read these files and deserialize them into the entities that json originally generated (I have access to the source objects). Each file has json output, which was generated by serializing an object of type IEnumerable<Response<MyEntity>>.

I get an exception when trying to deserialize, but I don't understand why. Here is my attempt to deserialize:

List<string> jsonOutputStrings;
// Read json from files and populate jsonOutputStrings list
List<Response<MyEntity>> apiResponses = new List<Response<MyEntity>>();

foreach (string json in jsonOutputStrings)
{
    apiResponses.AddRange(JsonConvert.DeserializeObject<List<Response<MyEntity>>>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }).ToList());
}

I also tried deserializing IEnumerable instead of a list:

apiResponses.AddRange(JsonConvert.DeserializeObject<IEnumerable<Response<MyEntity>>>(json, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All }).ToList());

I get the following exception:

The first random exception like "Newtonsoft.Json.JsonSerializationException" occurred in Newtonsoft.Json.dll

: System.Linq.Enumerable + WhereSelectListIterator`2 [Entities.Requirement, Entities.RequirementEntity]. '$ [0].ReturnedEntity.Summaries. $ [0].Requirements. $', 1, 715.

json ( ), json, :

"Requirements":{"$id":"7","$type":"System.Linq.Enumerable+WhereSelectListIterator`2[[Entities.Requirement, Entities],[Entities.RequirementEntity, API.Entities]], System.Core","$values":[...]}

, ( , ), Requirements IEnumerable<Entities.RequirementEntity>.

, MyEntity, . ?

+4
2

Response<> MyEntity , . , Where linq. , :

class MyEntity
{
    public MyEntity()
    {
        Data = new List<string>().Where(x => true);
    }

    public IEnumerable<string> Data { get; set; }

}

class Program
{
    static void Main(string[] args)
    {
        string data = @"[{""Data"":[""a"",""b""]}]";
        var j = JsonConvert.DeserializeObject<IEnumerable<MyEntity>>(data);
    }
}

- json. 2 :

  • TypeNameHandling TypeNameHandling.None ( - );

TypeNameHandling.None exmaple, IEnumerable<BaseType>, BaseType.

. , , , , List.

:

class MyEntity
{
    public IEnumerable<string> Data { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        IList<MyEntity> entities = new MyEntity[] {
            new MyEntity { Data = new [] { "1", "2" }.Where(x => x != string.Empty) },
            new MyEntity { Data = new [] { "A", "B" }.AsQueryable().Where(x => x != string.Empty) },
            new MyEntity { Data = new List<string> { "A", "B" } },
        };

        string data = JsonConvert.SerializeObject(entities, Formatting.Indented, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
        data = Regex.Replace(data, "\"\\$type\":\\s+\"System.Linq.Enumerable\\+WhereArrayIterator(.+?), System.Core\",", "\"$type\": \"System.Collections.Generic.List$1, mscorlib\",", RegexOptions.Singleline);

        var j = JsonConvert.DeserializeObject<IEnumerable<MyEntity>>(data, new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
    }
}
+3

Linq ? , IEnumerable Linq Where? , . .

+2

Source: https://habr.com/ru/post/1568056/


All Articles