0

I have a class which Implements IEnumerable and another Interface IModel:

    public class Model : IEnumerable, IModel {
        private readonly List<string> list = new List<string> {"aa", "bb"};
        public IEnumerator GetEnumerator() {
            return this.list.GetEnumerator();
        }

        public string Name { get; set; }
    }

When I call

    JsonConvert.SerializeObject(new Model() { Name = "Test" });

Json.NET is only serializing the Elements of the List. The Result is:

    ["aa","bb"]

What do I have to do, that Json.NET will also serialize the Property Name? Note: This is only a small Sample and I cannot change the original class because I am not the developer of the class.

Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
BennoDual
  • 5,865
  • 15
  • 67
  • 153
  • 1
    This behaviour is pretty common in serializers - pretty sure that `XmlSerializer` works similarly, and protobuf-net does that *by default* (although can be flipped); generally, things should look *either* like a collection *or* like a leaf node - here the serializer is saying "that looks kinda like a collection to me - I'll treat it as a json array and iterate it" – Marc Gravell Nov 30 '17 at 12:03
  • 1
    Start from [here](https://stackoverflow.com/q/21692193/1997232). – Sinatr Nov 30 '17 at 12:04
  • 2
    And how resulting json should look like? What name to use for property with array of items? – Evk Nov 30 '17 at 12:04

0 Answers0