Suppose I have a object structure like this
Library 1 ---- 1+ Book 1 ---- 1+ Page
I want to serialize a json object of a book with an array of page objects.
Using JSON.net serializer, I can get this to serialize without getting a circular reference, but the JSON still includes all of the properties of the book in each page, which includes data about the library…which can have data on other books which is a ton of noise.
From the answer from this question – Serialize Entity Framework objects into JSON, I know that I can do generics, but is this really the only way? This just seems like a ton of extra work. Especially if for a Json result that is Book with and array of page objects in it.
I am using Entity Framework 4.3.1 and Json.net 4.0.30319…
You should look at the serialization attributes.
Original answer
The aforementioned way should be prefered in most of the cases, but there are some where it is not enough.
There are two ways of doing it.
You can implement a JsonConverter, and override the WriteJson method to write only the properties you want.
You can call it like that :
You can also create a JsonBook class, and serialize it.