deniszykov / msgpack-unity3d

MessagePack and JSON serializer for Unity3D

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ignore Field Names In Serialization

sadradelir opened this issue · comments

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

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.

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

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)
);

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