hoangduit / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

Dictionary<string,List<T>> serialize error #403

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. when I serialize a type Dictionary<string,List<T>> it throw a error.
2. TupleSerializer.cs line : 44
3. "No serializer defined for type: Namespace.Model.ModelClass (with 
serializable and datacontract attributes)

T is a very sample class here :
    [Serializable,DataContract]
    public class ReviewTopic
    {
        public int Id { get; set; }

        public string Title { get; set; }
    }

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?
Win7 64x ,protobuf-net V 2.0.0.651,protobuf-net.Enyim v1.0.0.0

Please provide any additional information below.
   在 ProtoBuf.Serializers.TupleSerializer..ctor(RuntimeTypeModel model, ConstructorInfo ctor, MemberInfo[] members) 位置 E:\tfs-source\\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Serializers\TupleSerializer.cs:行号 50
   在 ProtoBuf.Meta.MetaType.BuildSerializer() 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\MetaType.cs:行号 439
   在 ProtoBuf.Meta.MetaType.get_Serializer() 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\MetaType.cs:行号 384
   在 ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\RuntimeTypeModel.cs:行号 761
   在 ProtoBuf.Meta.TypeModel.TrySerializeAuxiliaryType(ProtoWriter writer, Type type, DataFormat format, Int32 tag, Object value, Boolean isInsideList) 位置 E:\tfs-source\bitauto\A3车型产品研发\点评\Src\BitAutoReviews.V4\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\TypeModel.cs:行号 124
   在 ProtoBuf.Meta.TypeModel.TrySerializeAuxiliaryType(ProtoWriter writer, Type type, DataFormat format, Int32 tag, Object value, Boolean isInsideList) 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\TypeModel.cs:行号 170
   在 ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\TypeModel.cs:行号 188
   在 ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\TypeModel.cs:行号 217
   在 ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value) 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net\Meta\TypeModel.cs:行号 201
   在 ProtoBuf.Caching.Enyim.NetTranscoder.Enyim.Caching.Memcached.ITranscoder.Serialize(Object o) 位置 E:\tfs-source\plugins\protobuf-net r640\protobuf-net-source\protobuf-net.Enyim\protobuf-net.Enyim\ProtoTranscoder.cs:行号 125
   在 Enyim.Caching.MemcachedClient.PerformStore(StoreMode mode, String key, Object value, UInt32 expires, UInt64& cas, Int32& statusCode) 位置 d:\Dropbox\repo\EnyimMemcached\Enyim.Caching\MemcachedClient.cs:行号 355

Original issue reported on code.google.com by liurongw...@gmail.com on 30 Aug 2013 at 7:20

GoogleCodeExporter commented 9 years ago
The error message talks about ModelClass, but the question talks about 
ReviewTopic - can you clarify?

Original comment by marc.gravell on 30 Aug 2013 at 7:22

GoogleCodeExporter commented 9 years ago
No modelClass here(just for example), I have modify the ReviewTopic below, the 
exception gone ,But when I deserialize from memcache use the ProtoTranscoder, I 
got the values Dictionary<string,List<ReviewTopic>> data, the List<ReviewTopic> 
in dictionary has value ,but every ReviewTopic 's Property is empty .

    [Serializable,DataContract]
    public class ReviewTopic
    {
        [DataMember(Name="id")]
        public int Id { get; set; }

        [DataMember(Name = "title")]
        public string Title { get; set; }
    }

Original comment by liurongw...@gmail.com on 30 Aug 2013 at 8:10

GoogleCodeExporter commented 9 years ago
When I save the Dictionary<string,List<ReviewTopic>> data to memcache, Every 
ReviewTopic property has value .

Original comment by liurongw...@gmail.com on 30 Aug 2013 at 8:12

GoogleCodeExporter commented 9 years ago
Indeed; if using the DataMember attributes you need to use the Order to control 
serialization:

        [DataMember(Name="id", Order=1)]
        public int Id { get; set; }

        [DataMember(Name = "title", Order=2)]
        public string Title { get; set; }

protocol buffers is numbers-based, not name-based.

Original comment by marc.gravell on 30 Aug 2013 at 8:33

GoogleCodeExporter commented 9 years ago
thanks a ton ,it work,by the way , what 's best practice to use the 
Serializable,DataContract,ProtoContract for protobuff serialization, and need I 
mark all used property of a serializable class ?

I have some class which didn't have this problem ,it wase only marked as 
[Serializable].

as you say 'protocol buffers is numbers-based, not name-based.', why my other 
class worked correctly ?

    [Serializable]
    public class Impression
    {

        public int Id { get; set; }

        public int SerialId { get; set; }

        public string Keywords { get; set; }

        public int Vote { get; set; }

        public string Guid { get; set; }
    }

this class when serialize with protobuff don't has exception ? so what 's the 
correct way to use it ?

Original comment by liurongw...@gmail.com on 30 Aug 2013 at 9:38

GoogleCodeExporter commented 9 years ago
protobuf-net won't serialize that class *at all*; most likely that is working 
because it is using some other serializer implementation - presumably 
defaulting to BinaryFormatter for things that clearly aren't usable. Note, 
protobuf-net also doesn't care whether or not something is marked 
[Serializable] - it doesn't check for that and doesn't require that.

Original comment by marc.gravell on 30 Aug 2013 at 10:03