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 7890657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:24:44+00:00 2026-06-03T06:24:44+00:00

I only want the first depth level of an object (I do not want

  • 0

I only want the first depth level of an object (I do not want any children). I am willing to use any library available. Most libraries will merely throw an exception when the recursion depth is reached, instead of just ignoring. If this isn’t possible, is there a way to ignore serialization of certain members given a certain datatype?

Edit:
Let’s say I have an object like so:

class MyObject
{
    String name = "Dan";
    int age = 88;
    List<Children> myChildren = ...(lots of children with lots of grandchildren);
}

I want to remove any children (complex types even) to return an object like this:

class MyObject
{
    String name = "Dan";
    int age = 88;
    List<Children> myChildren = null;
}
  • 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-03T06:24:45+00:00Added an answer on June 3, 2026 at 6:24 am

    This is possible in Json.NET using some coordination between the JsonWriter and the serializer’s ContractResolver.

    A custom JsonWriter increments a counter when an object is started and then decrements it again when it ends.

    public class CustomJsonTextWriter : JsonTextWriter
    {
        public CustomJsonTextWriter(TextWriter textWriter) : base(textWriter) {}
    
        public int CurrentDepth { get; private set; }
    
        public override void WriteStartObject()
        {
            CurrentDepth++;
            base.WriteStartObject();
        }
    
        public override void WriteEndObject()
        {
            CurrentDepth--;
            base.WriteEndObject();
        }
    }
    

    A custom ContractResolver applies a special ShouldSerialize predicate on all properties that will be used to verify the current depth.

    public class CustomContractResolver : DefaultContractResolver
    {
        private readonly Func<bool> _includeProperty;
    
        public CustomContractResolver(Func<bool> includeProperty)
        {
            _includeProperty = includeProperty;
        }
    
        protected override JsonProperty CreateProperty(
            MemberInfo member, MemberSerialization memberSerialization)
        {
            var property = base.CreateProperty(member, memberSerialization);
            var shouldSerialize = property.ShouldSerialize;
            property.ShouldSerialize = obj => _includeProperty() &&
                                              (shouldSerialize == null ||
                                               shouldSerialize(obj));
            return property;
        }
    }
    

    The following method shows how these two custom classes work together.

    public static string SerializeObject(object obj, int maxDepth)
    {
        using (var strWriter = new StringWriter())
        {
            using (var jsonWriter = new CustomJsonTextWriter(strWriter))
            {
                Func<bool> include = () => jsonWriter.CurrentDepth <= maxDepth;
                var resolver = new CustomContractResolver(include);
                var serializer = new JsonSerializer {ContractResolver = resolver};
                serializer.Serialize(jsonWriter, obj);
            }
            return strWriter.ToString();
        }
    }
    

    The following test code demonstrates limiting the maximum depth to 1 and 2 levels respectively.

    var obj = new Node {
        Name = "one",
        Child = new Node {
            Name = "two",
            Child = new Node {
                Name = "three"
            }
        }
    };
    var txt1 = SerializeObject(obj, 1);
    var txt2 = SerializeObject(obj, 2);
    
    public class Node
    {
        public string Name { get; set; }
        public Node Child { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a query to a MongoDB and I only want the first object.
In the following text, I want to match only the first [ID], but not
I have a System.Data.DataView object but only want the first ten records, sorted by
I have 500 li 's but I only want to show 5 at first,
goal: I have the string 1234432144 I want to only replace the first 2
I want to replace only the first occurrence of a word in a sentence
I want to replace only the first matching element in a string instead of
I have a table for which I want to display only the first row
I want to run a basic query, but return only the first ten rows
I want to do a string replace in Python, but only do the first

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.