Set a maximum depth of JSON serialization in .NET Web API -
let's have these entitites these relations: this fictious example, , not current entities.
- course
- user
- newsposts
courses have many users, users have many courses
courses has many newsposts, newsposts has many courses
users has many newsposts, newsposts has many users
i'm using entity framework code first .net web api, sends entities in form of json. when try course, sends json result relations of entites, fine, wish set limit of how many levels serializes not serialize relations beyond first or second level.
get course/ serialized to:
{ "users":[{ "id":1, "newsposts": [{ "id":1, "message":"foo" }] }], "newsposts":[{ "id":2, "message":"bar" }] }
what want serialize 1 or maybe 2 levels, result be:
{ "users":[{ "id":1, "newsposts": [] }], "newsposts":[{ "id":2, "message":"bar" }] }
i have added a:
json.serializersettings.referenceloophandling = referenceloophandling.ignore;
this remove referencelooping.
so in short: there way of setting maximum amount of nodes serialize?
isn't question of how construct data send function ?
i mean json serializer job serializing data give him, remove data, or don't include it, , won't serialized.
if it's finer tuning looking custom jsonwriter want, link posted alex filipovici give details on achieve that.
Comments
Post a Comment