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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:02:50+00:00 2026-06-12T00:02:50+00:00

Currently I’m trying to utilize Json.Net serializer in my projects. One of the problems

  • 0

Currently I’m trying to utilize Json.Net serializer in my projects.

One of the problems that I’ve faced recentry is that deserialized object gets all of it’s private list items duplicated.
It can be reproduced like this:

  1. Create class with private field of List<> type, where List<> generic parameter is some other class
  2. Add public property, that gets/sets private field
  3. Create instance of that class, add 1 item to inner list, using property.
  4. Serialize with new DefaultContractResolver that is setup to see private class data;
  5. Deserialize

Code to reproduce the problem:

    public class ClassParam
    {
        public int? ParamOne
        {
            get;
            set;
        }
    }
    public class ClassWithParams
    {
        private List<ClassParam> _privateFieid = new List<ClassParam>();

        public List<ClassParam> PropertWithBackingField
        {
            get { return _privateFieid; }
            set { _privateFieid = value; }
        }
        
        public void AddElementToPrivateField(ClassParam classParam)
        {
            _privateFieid.Add(classParam);
        }
        
    }
    [Test]
    public void Test()
    {
            var instance = new ClassWithParams();

        var param1 = new ClassParam { ParamOne = 1 };

        instance.PropertWithBackingField.Add(param1);

        var contractResolver = new DefaultContractResolver();
        contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;

        string serializedInstance = JsonConvert.SerializeObject(instance,
                                                                   Formatting.Indented,
                                                                   new JsonSerializerSettings()
                                                                   {
                                                                       ContractResolver = contractResolver
                                                                      
                                                                   });

        var deserializeInstance = JsonConvert.DeserializeObject(serializedInstance, typeof(ClassWithParams),
                                                                         new JsonSerializerSettings()
                                                                         {
                                                                             ContractResolver = contractResolver
                                                                         });
    }

1

When I remove public property PropertWithBackingField from ClassWithParams it’s all ok.
The problem is gone as well when I don’t use custom setup for ContractResolver. But I do need to serialize private data of my classes as soon as not all of it is exposed via public properties.

What’s wrong with my code? Are there any subtleties, using Json.Net or is it a bug?

  • 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-12T00:02:51+00:00Added an answer on June 12, 2026 at 12:02 am

    For this code

    var s = JsonConvert.SerializeObject(instance);
    var desInst = JsonConvert.DeserializeObject<ClassWithParams>(s);
    

    Your json will be {"PropertWithBackingField":[{"ParamOne":1}]}

    But you say to include private field in serialization/deserialization with

    contractResolver.DefaultMembersSearchFlags |= BindingFlags.NonPublic;
    

    and get a json

       {
      "_privateFieid": [
        {
          "ParamOne": 1
        }
      ],
      "PropertWithBackingField": [
        {
          "ParamOne": 1
        }
      ]
     }
    

    So there are two deserializations, one for _privateFieid and one for PropertWithBackingField

    Either use BindingFlags.Public or use my code above with is much simpler.

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

Sidebar

Related Questions

Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently I'm starting a new Activity and calling finish on a current one. Is
Currently working with converting SQLException error messages into messages that are more useful for
Currently I am trying to use a bunch of custom perl modules, test.pm as
Currently I am adding object by creating it like: type TRecord = class private
Currently, my MVC 3 app has a dependency on a static class that is
Currently I am using Amazon Cloudfront to service static objects on my ASP.Net MVC3
Currently, I have a log file of messages in one table in a MySQL
Currently, Enum.Parse supports only the comma as the value separator, so that MemberOne,MemberThree will
Currently I'm using the Jackson JSON Processor to write preference data and whatnot to

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.