Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
575
Parsing datasource to JSON
posted

I wanted to get a bit of a better understanding about how the grid parses the datasource objects to JSON when calling GridModel.GetData().

From what I understand, it seems to look at the object, iterate through its properties and generate JSON from that.  E.g. if your object is like this:

class User
{
   int id  { get; set; }
   String name  { get; set; }
   String description { get; set; }
}

Then you'll get a JSON object like this:

{ "id": "1", 
  "name": "steve"; 
  "description": "steve is awesome" 
}

So I'm assuming then if you have a complex type in your class, it tries to convert that to JSON as well.  E.g:

class Project
{
   int id  { get; set; }
   String code { get; set; }
   User user { get; set; }
}

Then you'll get a JSON object like this:

{ "id": "10", 
  "code": "Project123", 
  "user": { "id": "1", 
            "name": "steve"; 
            "description": "steve is awesome" 
          }
}

If I am right so far, then that would mean if I have a complicated model such that a circular reference could occur, then the JSON parsing will break.  I know that I have hit that issue already using the igGrid which lead me to create view models for all my grids.

Is this what is happening? And is there a way around the circular reference thing, such as telling the parser what properties to parse and what not to parse?

  • 24671
    posted

    Hi,

    Very good question -:)  I will try to answer as detailed as I can but in case it doesn't fully answer your question or new ones pop up , please let me know.

    In general the grid traverses (expands) the transformed IQueryable, and generates a dictionary out of it, so that this dictionary structure can be serialized directly by the ASP.NET MVC serializer. 

    if AutoGenerateColumns is false, the grid fills in this Dictionary only properties that are defined in the colums. So if you have  a column for ProductID, an entry for it will go into this Dictionary.

    If AutoGenerateColumns is true, everything from the data source goes into teh Dictionary. For sure if you have Paging / Sorting / Filtering / other features defined, they will be processed first, before expanding the IQueryable, so that you don't have any obvious performance overhead. 

    Now, the problem with the ASP.NET MVC Serializer, particularly in the context of EntityFramework, is that it doesn't support circular references. If you directly bind to an EF Model, in most cases you will have those circular references generated already and this won't work. One solution would be to mark the relationships in the EF model with "Internal" Modifier instead of with "Public" one, so that it doesn't generate the extra references. Another approach is to create plain simple classes which map to the EF classes. I prefer this approach, and you can do it in a very clean way, in your controller, like this:

    DevelopmentDBEntities entities = new DevelopmentDBEntities();

    var query = from d in entities.Developers

    orderby d.DeveloperID

    select new MyDeveloper

    {

    DeveloperId = d.DeveloperID,

    ProjectId = d.ProjectID,

    Name = d.Name,

    Position = d.Position,

    HireDate = d.HireDate,

    Salary = d.Salary

    };

    return query;

    }

    What happens in the above code is that i have a simple class called MyDeveloper which has the properties ProjectId, Name, Position, and so on. My Database table is called "Developers" and my Entity Framework entity name is called "Developer". The above code creates an IQueryable of simple "MyDeveloper" objects, and thus avoids the circular reference issue. Note that you can use the same approach no matter how many levels or nestings you have. 

    Thanks,

    Angel