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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:51:59+00:00 2026-05-13T13:51:59+00:00

Given such object: Foo foo = new Foo { A = a, B =

  • 0

Given such object:

Foo foo = new Foo
{
    A = "a",
    B = "b",
    C = "c",
    D = "d"
};

How can I serialize and deserialize only certain properties (e.g. A and D).

Original: 
  { A = "a", B = "b", C = "c", D = "d" }

Serialized:
  { A = "a", D = "d" }

Deserialized:
  { A = "a", B = null, C = null, D = "d" }

I have wrote some code using JavaScriptSerializer from System.Web.Extensions.dll:

public string Serialize<T>(T obj, Func<T, object> filter)
{
    return new JavaScriptSerializer().Serialize(filter(obj));
}

public T Deserialize<T>(string input)
{
    return new JavaScriptSerializer().Deserialize<T>(input);
}

void Test()
{
    var filter = new Func<Foo, object>(o => new { o.A, o.D });

    string serialized = Serialize(foo, filter);
    // {"A":"a","D":"d"}

    Foo deserialized = Deserialize<Foo>(serialized);
    // { A = "a", B = null, C = null, D = "d" }
}

But I would like the deserializer to work a bit differently:

Foo output = new Foo
{
    A = "1",
    B = "2",
    C = "3",
    D = "4"
};

Deserialize(output, serialized);
// { A = "a", B = "2", C = "3", D = "d" }

Any ideas?

Also, may be there are some better or existing alternatives available?

EDIT:

There was some suggestions to use attributes to specify serializable fields. I am looking for more dynamic solution. So I can serialize A, B and the next time C, D.

EDIT 2:

Any serialization solutions (JSON, XML, Binary, Yaml, …) are fine.

  • 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-13T13:52:00+00:00Added an answer on May 13, 2026 at 1:52 pm

    I have done something similar myself with the Javascript Serializer in the past. In my case I only wanted to serialize nullable properties in the object that contained a value. I did this by using reflection, checking the property for a value and adding the property to a Dictionary e.g.

    public static Dictionary<string,object> CollectFilledProperties(object instance)
    {
        Dictionary<string,object> filledProperties = new Dictionary<string,object>();
        object data = null;
    
        PropertyInfo[] properties = instance.GetType().GetProperties();
        foreach (PropertyInfo property in properties)
        {
            data = property.GetValue(instance, null);
    
            if (IsNullable(property.PropertyType) && data == null)
            {
                // Nullable fields without a value i.e. (still null) are ignored.
                continue;
            }
    
            // Filled has data.
            filledProperties[property.Name] = data;
        }
    
        return filledProperties;
    }
    
    public static bool IsNullable(Type checkType)
    {
        if (checkType.IsGenericType && checkType.GetGenericTypeDefinition() == typeof(Nullable<>))
        {
            // We are a nullable type - yipee.
            return true;
        }
        return false;
    }
    

    Then instead of serializing the original object you pass the dictionary and bob’s your uncle.

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

Sidebar

Related Questions

Given a string such as: new/path - path/path/03 - filename.ext, how can I use
Given that foo is a valid datetime object in python, One can change the
Given a string such as: 23,234,456 first second third How can I split string
Given a dictionary-like object in Javascript such as {a:1, b:-2, c:42} , is there
Given an EntityFramework POCO object model such as: public class Group { public int
Given the array below, how do I get values of object(stdClass) such as Pcv
IE9 users can disable an add-on, such as a browser helper object, by clicking
Given a function func(*args) and a list, how can I 'unpack' the list such
Given such a code segment: #include <iostream> #include <iterator> #include <fstream> #include <string> using
Another stupid question here regarding a SQL scenario. Given data such as: FieldKey DocumentKey

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.