Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8789701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:28:58+00:00 2026-06-13T22:28:58+00:00

Suppose I have a object structure like this Library 1 —- 1+ Book 1

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:28:59+00:00Added an answer on June 13, 2026 at 10:28 pm

    You should look at the serialization attributes.

    [JsonObject(MemberSerialization.OptIn)]
    public class Page
    {
        [JsonProperty]
        public string Text { get; set; }
    
        // not serialized because mode is opt-in
        public Book Book { get; set; }
    }
    

    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.

    class BookConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(Book);
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value.GetType() == typeof(T2))
            {
                JObject obj = new JObject();
                Book b = value as Book;
                obj["titre"] = b.Name;
                obj["pages"] = b.Pages;
                // This line can also be 
                // obj.WriteTo(writer, this);
                // if you also need change the way pages are serialized.
                obj.WriteTo(writer, null);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
    }
    

    You can call it like that :

    string result = JsonConvert.SerializeObject(
        book,
        new JsonSerializerSettings
        {
            Converters = new JsonConverter[] { new BookConverter() }
        });
    

    You can also create a JsonBook class, and serialize it.

    class JsonBook{
        public JsonBook(Book b){/*...*/}
        public List<Pages> l;
        public string title;
        // No reference to Library.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a javascript object like this: window.config config.UI = { opacity: {
Suppose i have a json object friends as: { title: Friends link: /friends.json description:
I have a JSON structure; { books: [ {id: book1, title: Book One}, {id:
Suppose you have this table structure: Patient -> PatientTag -> Tag A typical N:M
Suppose we have an arbitrary graph represented by nodes and pointers like this: class
Suppose I have some simple struct like this: public struct WeightedInt { public int
Suppose I have a class structure like the following, where I have a temporary
suppose I have a object: obj:{ child:{ x:12, y:50 }, key1:value1, key2:value2 } if
Suppose I have an object file ( source.o ) without function main . a
In C#, suppose you have an object (say, myObject ) that is an instance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.