msgpack-unity3d icon indicating copy to clipboard operation
msgpack-unity3d copied to clipboard

Ignore Field Names In Serialization

Open sadradelir opened this issue 8 years ago • 4 comments

Can we ignore field names in serialization ? I use SerializationOptions.SuppressTypeInformation but i have my class members names in serialization.

sadradelir avatar Mar 28 '17 12:03 sadradelir

Hi @sadradelir, if you want to ignore some field during serialization you could: a) make them private|protected|internal b) remove DataContractAttribute from class and add IgnoreDataMemberAttribute on ignored fields c) add DataContractAttribute and mark with DataMemberAttribute only required fields, non-marked will be ignored.

deniszykov avatar Mar 28 '17 13:03 deniszykov

Hi @deniszykov and thanks for your fast replay, But I mean field names not entire ignore of field, for example i pack this class : { public int test1 = 10 ; public int test2 = 20 ; } and I got this : "StringPrefix"test1"IntPrefix"10"StringPrefix"test2"IntPrefix"20 but with java library and msgPack-Cli ( standard version mentioned in msgpack.org for c# ) we got this ( with field name ignorance ) : "IntPrefix"10"IntPrefix"20

sadradelir avatar Mar 28 '17 13:03 sadradelir

Do you mean storing only objects field values? At the moment there is no such feature. You can implement your own serializer of such objects and register it in SerializationContext.ObjectSerializerFactory.

var context = new SerializationContext();
context.ObjectSerializerFactory = (t => 
    IsSpecialType(t) ? 
         new SpecialTypeSerializer(t) : 
         new ObjectSerializer(context, t)
);

deniszykov avatar Mar 28 '17 13:03 deniszykov

yes I mean this, so I should develop my serilizerFactory, thanks for your fast answer. and I appreciate your help :) .

sadradelir avatar Mar 28 '17 18:03 sadradelir