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

  • Home
  • SEARCH
  • 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 8834937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:08:32+00:00 2026-06-14T09:08:32+00:00

I have just started using Newtonsoft.Json (Json.net). In my first simple test, I ran

  • 0

I have just started using Newtonsoft.Json (Json.net). In my first simple test, I ran into a problem when deserializing generic lists. In my code sample below I serialize an object, containing three types of simple integer lists (property, member var and array).

The resulting json looks fine (the lists are converted into json-arrays). However, when I deserialize the json back to a new object of the same type, all list items are duplicated, expect for the array. I’ve illustrated that by serializing it a second time.

From searching around, I’ve read that there may be a “private” backing field to the lists that the deserializer also fills.

So my question is: Is there a (preferably simple) way to avoid duplicate items in following case?

Code

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace JsonSerializeExample
{
    public class Program
    {
        static void Main()
        {
            var data = new SomeData();
            var json = JsonConvert.SerializeObject(data);
            Console.WriteLine("First : {0}", json);
            var data2 = JsonConvert.DeserializeObject<SomeData>(json);
            var json2 = JsonConvert.SerializeObject(data2);
            Console.WriteLine("Second: {0}", json2);
        }
    }

    public class SomeData
    {
        public string SimpleField;
        public int[] IntArray;
        public IList<int> IntListProperty { get; set; }
        public IList<int> IntListMember;

        public SomeData()
        {
            SimpleField = "Some data";
            IntArray = new[] { 7, 8, 9 };
            IntListProperty = new List<int> { 1, 2, 3 };
            IntListMember = new List<int> { 4, 5, 6 };
        }
    }
}

Resulting output

First : {"SimpleField":"Some data","IntArray":[7,8,9],"IntListMember":[4,5,6],"IntListProperty":[1,2,3]}
Second: {"SimpleField":"Some data","IntArray":[7,8,9],"IntListMember":[4,5,6,4,5,6],"IntListProperty":[1,2,3,1,2,3]}

There may be some overlap here with Json.Net duplicates private list items. However, I think my problem is even simpler, and I still haven’t figured it out.

  • 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-14T09:08:35+00:00Added an answer on June 14, 2026 at 9:08 am

    That is because you are adding items in the constructor. A common approach in deserializers when processing a list is basically:

    • read the list via the getter
      • if the list is null: create a new list and assign via the property setter, if one
    • deserialize each item in turn, and append (Add) to the list

    this is because most list members don’t have setters, i.e.

    public List<Foo> Items {get {...}} // <=== no set
    

    Contrast to arrays, which must have a setter to be useful; hence the approach is usually:

    • deserialize each item in turn, and append (Add) to a temporary list
    • convert the list to an array (ToArray), and assign via the setter

    Some serializers give you options to control this behavior (others don’t); and some serializers give you the ability to bypass the constructor completely (others don’t).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just started using RSpec and I copied the very simple test on
I have just started using EF and found it cool, but I ran into
I have just started using EF Code First to implement a simple blog. I
I have just started using Ember.js framework and I have simple problem. I have
We have just started using EF 4.1 code first. I have a sql query
I have just started using SCSS for the first time. I'm noticing that the
I have just started using StructureMap, having previously worked with Spring.Net. I love the
I have just started using Ubuntu as my first Linux, have a couple of
I have just started using JSON... I am using it in my project (Spring).
I have just started using BoneCP and this is my first time using a

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.