How to serialize only certain properties of an object

For such an 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 wrote the 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 { oA, oD }); 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 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, maybe there are some better or existing alternatives?

EDIT:

There have been suggestions for using attributes to indicate serializable fields. I am looking for a more dynamic solution. So I can serialize A, B and next time C, D.

EDIT 2:

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

+5
source share
4 answers

In the past, I did something similar to the Javascript Serializer. In my case, I only wanted to serialize properties with a null value in the object containing the value. I did this using reflection, checking the property for the value and adding the property to the dictionary, for example.

 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 ie (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 in the dictionary and click on your uncle.

+4
source

Pretty easy - just decorate the methods you want to ignore with the [ScriptIgnore] attribute.

+24
source

There are attributes that can be applied to classes and / or properties that control serialization. Attributes that control serialization .

+3
source

What about the attribute tag [NonSerialized()] ?

  class Foo { field A; [NonSerialized()] field B; [NonSerialized()] field C; field D; } 
+1
source

Source: https://habr.com/ru/post/1285741/


All Articles