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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:40:18+00:00 2026-06-17T09:40:18+00:00

I have a custom collection (implements IList) which has some custom properties as shown

  • 0

I have a custom collection (implements IList) which has some custom properties as shown below:

class FooCollection : IList<Foo> {

    private List<Foo> _foos = new List<Foo>();
    public string Bar { get; set; }        

    //Implement IList, ICollection and IEnumerable members...

}

When I serialize, I use the following code:

JsonSerializerSettings jss = new JsonSerializerSettings() {
    TypeNameHandling = TypeNameHandling.Auto
};
string serializedCollection = JsonConvert.SerializeObject( value , jss );

It serializes and deserializes all the collection items properly; however, any extra properties in the FooCollection class are not taken into account.

Is there anyway to include them in the serialization?

  • 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-17T09:40:19+00:00Added an answer on June 17, 2026 at 9:40 am

    The problem is the following: when an object implements IEnumerable, JSON.net identifies it as an array of values and serializes it following the array Json syntax (that does not include properties),
    e.g. :

     [ {"FooProperty" : 123}, {"FooProperty" : 456}, {"FooProperty" : 789}]
    

    If you want to serialize it keeping the properties, you need to handle the serialization of that object by hand by defining a custom JsonConverter :

    // intermediate class that can be serialized by JSON.net
    // and contains the same data as FooCollection
    class FooCollectionSurrogate
    {
        // the collection of foo elements
        public List<Foo> Collection { get; set; }
        // the properties of FooCollection to serialize
        public string Bar { get; set; }
    }
    
    public class FooCollectionConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(FooCollection);
        }
    
        public override object ReadJson(
            JsonReader reader, Type objectType, 
            object existingValue, JsonSerializer serializer)
        {
            // N.B. null handling is missing
            var surrogate = serializer.Deserialize<FooCollectionSurrogate>(reader);
            var fooElements = surrogate.Collection;
            var fooColl = new FooCollection { Bar = surrogate.Bar };
            foreach (var el in fooElements)
                fooColl.Add(el);
            return fooColl;
        }
    
        public override void WriteJson(JsonWriter writer, object value, 
                                       JsonSerializer serializer)
        {
            // N.B. null handling is missing
            var fooColl = (FooCollection)value;
            // create the surrogate and serialize it instead 
            // of the collection itself
            var surrogate = new FooCollectionSurrogate() 
            { 
                Collection = fooColl.ToList(), 
                Bar = fooColl.Bar 
            };
            serializer.Serialize(writer, surrogate);
        }
    }
    

    Then use it as follows:

    var ss = JsonConvert.SerializeObject(collection, new FooCollectionConverter());
    
    var obj = JsonConvert.DeserializeObject<FooCollection>(ss, new FooCollectionConverter());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a custom collection class that provides some internal thread synchronization. For
I have created a custom collection class that I am trying to bind to
Possible Duplicate: Custom Collection Initializers I have a simple Pair class: public class Pair<T1,
I have created a custom generic queue which implements a generic IQueue interface, which
I have a hibernate mapping with custom collection types in it. java: class Policy
I have a custom object that implements INotifyPropertyChanged. I have a collection of these
I have a custom class that implements ICollection , and this class is readonly,
Background I have a custom collection that is binded to the datagridview this.datagridview.DataSource =
I have a my custom collection derived from List(.MyItem.) I want to trace if
I have a WCF service that needs to expose a custom collection to it's

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.