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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:08:33+00:00 2026-06-09T16:08:33+00:00

I have code structured like below. public class Stats { public string URL {

  • 0

I have code structured like below.

public class Stats
{
        public string URL { get; set; }
        public string Status { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public int Length { get; set; }
}

and

 public class UrlStats
 {
        public string URL { get; set; }
        public int TotalPagesFound { get; set; }
        public List<Stats> TotalPages { get; set; }
        public int TotalTitleTags { get; set; }
        public List<Stats> TotalTitles { get; set; }
        public int NoDuplicateTitleTags { get; set; }
        public List<Stats> DuplicateTitles { get; set; }
        public int NoOverlengthTitleTags { get; set; }
        public List<Stats> OverlengthTitles { get; set; }
 }

Basically i am scanning a website for statistics like title tags, duplicate titles, etc.

I am using JQuery and making AJAX calls to webservice and retrieving url stats while the process is running to show user url stats by far collected since it takes quite a time to scan a big website. So after every 5 seconds i retrieve stats from server. Now the problem is all the List variable data i need to send at the end when scanning processing is complete, not during updates. What’s happening right now the List<Stats> variable data is also sent during updates which is big chunk of data and i want to send only int type variables which are required to show process updates.

From searching on internet i couldn’t find anything useful solving my problem and i found that Json.NET is very good library but i really don’t know how to properly use it to get what i want.

Basically i am looking for serializing properties depending on their datatype at runtime, if its possible.

  • 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-09T16:08:34+00:00Added an answer on June 9, 2026 at 4:08 pm

    There are two different approaches for your problem.

    You should choose the first one if you are going to change your classes more often because the first approach prevents that you forget to serialize a newly added property. Furthermore it is much more reusable if you want to add another classes you want to be serialized the same way.

    If you have only these two classes and it’s most likely that they’re not going to change you can choose the second approach to keep your solution simple.

    1. Use a custom converter to select all int properties

    The first approach is to use a custom JsonConverter which serializes a class or struct by only including properties which have type int. The code might look like this:

    class IntPropertyConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            // this converter can be applied to any type
            return true;
        }
    
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            // we currently support only writing of JSON
            throw new NotImplementedException();
        }
    
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value == null)
            {
                serializer.Serialize(writer, null);
                return;
            }
    
            // find all properties with type 'int'
            var properties = value.GetType().GetProperties().Where(p => p.PropertyType == typeof(int));
    
            writer.WriteStartObject();
    
            foreach (var property in properties)
            {
                // write property name
                writer.WritePropertyName(property.Name);
                // let the serializer serialize the value itself
                // (so this converter will work with any other type, not just int)
                serializer.Serialize(writer, property.GetValue(value, null));
            }
    
            writer.WriteEndObject();
        }
    }
    

    Then you have to decorate your class with a JsonConverterAttribute:

    [JsonConverter(typeof(IntPropertyConverter))]
    public class UrlStats
    {
        // ...
    }
    

    Disclaimer: This code has been tested only very roughly.


    2. Choose properties individually

    The second solution looks a bit simpler: You can use the JsonIgnoreAttribute to decorate the attributes you want to exclude for serialization. Alternatively you can switch from “blacklisting” to “whitelisting” by explicitly including the attributes you want to serialize. Here is a bit of sample code:

    Blacklisting: (I’ve reordered the properties for a better overview)

    [JsonObject(MemberSerialization.OptOut)] // this is default and can be omitted
    public class UrlStats
    {
        [JsonIgnore] public string URL { get; set; }
        [JsonIgnore] public List<Stats> TotalPages { get; set; }
        [JsonIgnore] public List<Stats> TotalTitles { get; set; }
        [JsonIgnore] public List<Stats> DuplicateTitles { get; set; }
        [JsonIgnore] public List<Stats> OverlengthTitles { get; set; }
    
        public int TotalPagesFound { get; set; }
        public int TotalTitleTags { get; set; }
        public int NoDuplicateTitleTags { get; set; }
        public int NoOverlengthTitleTags { get; set; }
    }
    

    Whitelisting: (also reordered)

    [JsonObject(MemberSerialization.OptIn)] // this is important!
    public class UrlStats
    {
        public string URL { get; set; }
        public List<Stats> TotalPages { get; set; }
        public List<Stats> TotalTitles { get; set; }
        public List<Stats> DuplicateTitles { get; set; }
        public List<Stats> OverlengthTitles { get; set; }
    
        [JsonProperty] public int TotalPagesFound { get; set; }
        [JsonProperty] public int TotalTitleTags { get; set; }
        [JsonProperty] public int NoDuplicateTitleTags { get; set; }
        [JsonProperty] public int NoOverlengthTitleTags { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following structure. public class ToolSettings { public string Extension { get;
I have a class as below: public class Node { public int NodeID {
I have some code below. This code is a basic push/pop stack class that
I have a project with a structure like this: project code art config Art
let's say I have a html structure like this -> (note: in my code
I understand that i cannot do something like List<List<string,string>> . So i have to
Consider the code below. Looks like perfectly valid C# code right? //Project B using
1- I have a class structure as shown below. namespace ViewStateSeoHelper { class ViewStateSeoModule
Update: I have tried Sheldon's code below but I have struck a couple of
Suppose I have a class structure like the following, where I have a temporary

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.