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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:18:33+00:00 2026-06-15T06:18:33+00:00

I need to be able to control how/whether certain properties on a class are

  • 0

I need to be able to control how/whether certain properties on a class are serialized. The simplest case is [ScriptIgnore]. However, I only want these attributes to be honored for this one specific serialization situation I am working on – if other modules downstream in the application also want to serialize these objects, none of these attributes should get in the way.

So my thought is to use a custom attribute MyAttribute on the properties, and initialize the specific instance of JsonSerializer with a hook that knows to look for that attribute.

At first glance, I don’t see any of the available hook points in JSON.NET will provide the PropertyInfo for the current property to do such an inspection – only the property’s value. Am I missing something? Or a better way to approach this?

  • 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-15T06:18:34+00:00Added an answer on June 15, 2026 at 6:18 am

    You have a few options. I recommend you read the Json.Net documentation article on the subject before reading below.

    The article presents two methods:

    1. Create a method that returns a bool value based on a naming convention that Json.Net will follow to determine whether or not to serialize the property.
    2. Create a custom contract resolver that ignores the property.

    Of the two, I favor the latter. Skip attributes altogether — only use them to ignore properties across all forms of serialization. Instead, create a custom contract resolver that ignores the property in question, and only use the contract resolver when you want to ignore the property, leaving other users of the class free to serialize the property or not at their own whim.

    Edit To avoid link rot, I’m posting the code in question from the article

    public class ShouldSerializeContractResolver : DefaultContractResolver
    {
       public new static readonly ShouldSerializeContractResolver Instance =
                                     new ShouldSerializeContractResolver();
    
       protected override JsonProperty CreateProperty( MemberInfo member,
                                        MemberSerialization memberSerialization )
       {
          JsonProperty property = base.CreateProperty( member, memberSerialization );
    
          if( property.DeclaringType == typeof(Employee) &&
                property.PropertyName == "Manager" )
          {
             property.ShouldSerialize = instance =>
             {
                // replace this logic with your own, probably just  
                // return false;
                Employee e = (Employee)instance;
                return e.Manager != e;
             };
          }
    
          return property;
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to to be able to control the alignment of text in the
I need to be able to tell from within a VB.NET class (not a
Users of the website need to able to store images in their area ,
Need to be able to pull Magento products into an external template. Need to
I need to be able to access a list throughout my Rails project. For
I need to be able to search through multiple 2D arrays and search the
I need to be able to create an id for a hyperlink. I have
I need to be able to strip out a condition in a string, here
I need to be able do the following on a TR: onmouseover highlight the
I need to be able to map out in coordinates real world cities and

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.