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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:01:23+00:00 2026-05-20T02:01:23+00:00

I have an object called MyObject that has several properties. MyList is a list

  • 0

I have an object called MyObject that has several properties. MyList is a list of MyObject that I populate with a linq query and then I serialize MyList into json. I end up with something like this

    List<MyObject> MyList = new List<MyObject>();

    MyList = TheLinqQuery(TheParam);

    var TheJson = new System.Web.Script.Serialization.JavaScriptSerializer();
    string MyJson = TheJson.Serialize(MyList);

What I want to do is serialize only parts of MyObject. For instance, I might have Property1, Property2…Propertyn and I want MyJson to only include Property3, Property5 and Property8.

I thought of a way to do this by creating a new object with only the properties I want and from there create a new list for the serialization. Is this the best way or is there a better/faster way?

Thanks.

  • 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-05-20T02:01:24+00:00Added an answer on May 20, 2026 at 2:01 am
    // simple dummy object just showing what "MyObject" could potentially be
    public class MyObject
    {
        public String Property1;
        public String Property2;
        public String Property3;
        public String Property4;
        public String Property5;
        public String Property6;
    }
    
    // custom converter that tells the serializer what to do when it sees one of
    // the "MyObject" types. Use our custom method instead of reflection and just
    // dumping properties.
    public class MyObjectConverter : JavaScriptConverter
    {
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            throw new ApplicationException("Serializable only");
        }
    
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            // create a variable we can push the serailized results to
            Dictionary<string, object> result = new Dictionary<string, object>();
    
            // grab the instance of the object
            MyObject myobj = obj as MyObject;
            if (myobj != null)
            {
                // only serailize the properties we want
                result.Add("Property1", myobj.Property1);
                result.Add("Property3", myobj.Property3);
                result.Add("Property5", myobj.Property5);
            }
    
            // return those results
            return result;
        }
    
        public override IEnumerable<Type> SupportedTypes
        {
            // let the serializer know we can accept your "MyObject" type.
            get { return new Type[] { typeof(MyObject) }; }
        }
    }
    

    And then where ever you’re serializing:

    // create an instance of the serializer
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    // register our new converter so the serializer knows how to handle our custom object
    serializer.RegisterConverters(new JavaScriptConverter[] { new MyObjectConverter() });
    // and get the results
    String result = serializer.Serialize(MyObjectInstance);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey. I have an object that has a string property called BackgroundColor. This string
I have a property called IsSecureConnection that is part of my object's interface. This
I have a collection of an object called Bookmarks which is made up of
I have a c# object with a property called Gender which is declared as
I have object A which in turn has a property of type Object B
I have an object that is mapped to a cookie as a serialized base-64
I have an object in a multi-threaded environment that maintains a collection of information,
I'm having a small problem in Java. I have an interface called Modifiable. Objects
I have an object graph serialized to xaml. A rough sample of what it
I have an object of the type System.Drawing.Image and want to make every pixel

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.