0

I am trying to serialize the data of courses. I get course id(C_id) and course Name(C_Name). I am getting one more thing that is Icoolection of students. When I try to serialize this, I am not able to get the nested list of students who have enrolled for the course.

var u = (from g in t.courses 
         select g)
        .ToList();

List<course> ui = u
    .Select(d => new course() 
        { C_Name = d.C_Name, 
          C_Id = d.C_Id, 
          student = d.student 
        })
    .ToList();

ASCIIEncoding objASCIIEncoding = new ASCIIEncoding();

string strData = JsonConvert
    .SerializeObject(ui, Formatting.Indented, new JsonSerializerSettings()
{
    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
});

In the above code I am getting proper data with nested students list but when this line

string strData = JsonConvert.SerializeObject(ui, Formatting.Indented, new JsonSerializerSettings()
{
    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
});

executes I only get Course ID(C_ID) and course Name(C_Name) . The nested student list is not serialized.

mihai
  • 4,592
  • 3
  • 29
  • 42
user2998990
  • 970
  • 5
  • 18
  • 36
  • Looks like its something to do with the reference loop handling and your data having infinite nesting. See http://stackoverflow.com/questions/11979637/what-does-referenceloophandling-ignore-in-newtonsoft-json-exactly-do for more details – link64 Dec 10 '14 at 06:18

2 Answers2

0

From the documentation:

Json.NET will ignore objects in reference loops and not serialize them. The first time an object is encountered it will be serialized as usual but if the object is encountered as a child object of itself the serializer will skip serializing it.

http://james.newtonking.com/json/help/index.html?topic=html/SerializationSettings.htm

I am assuming your students contain references to their courses? Perhaps you should make sure they dont by doing something similar to the below to remove the reference to the course:

student = d.student.Select(s => new {s.studentId, s.studentName}).ToList()
link64
  • 1,944
  • 1
  • 26
  • 41
0

Hope this helps http://www.newtonsoft.com/json/help/html/PreserveObjectReferences.htm

The PreserveReferencesHandling setting on the JsonSerializer will change how all objects are serialized and deserialized. For fine grain control over which objects and members should be serialized as a reference there is the IsReference property on the JsonObjectAttribute, JsonArrayAttribute and JsonPropertyAttribute. Setting IsReference on JsonObjectAttribute or JsonArrayAttribute to true will mean the JsonSerializer will always serialize the type the attribute is against as a reference. Setting IsReference on the JsonPropertyAttribute to true will serialize only that property as a reference.

[JsonObject(IsReference = true)]
public class EmployeeReference
{
    public string Name { get; set; }
    public EmployeeReference Manager { get; set; }
}
prashant
  • 2,181
  • 2
  • 22
  • 37