pizheng / protobuf-net

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

Pre-defined Dictionary causing ArgumentException on Deserialization #200

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
(jimweinhart@gmail.com
==============
I'm working on a small indie game in Unity and decided to try implementing 
Proto-buf for my Save system. I'm hitting an issue though when serializing a 
dictionary that's predefined in the class.

For example:
[ProtoContract]
public class PlayerStats
{
    public enum PlayerSkills {TINKERING, COOKING, CARPENTRY, BLACKSMITHING}
    [ProtoMember(1)]
    public Dictionary<PlayerStats.PlayerSkills, float> playerSkills = new Dictionary<PlayerStats.PlayerSkills, float>()
    {
        {PlayerSkills.BLACKSMITHING, 0},
        {PlayerSkills.CARPENTRY, 0},
        {PlayerSkills.COOKING, 0},
        {PlayerSkills.TINKERING, 0}
    };
}

This all serializes fine, but when I attempt to deserialize, I get the error:

"ArgumentException: An element with the same key already exists in the 
dictionary."

I'm guessing its because technically, those keys already exist because my new 
(null) class is somehow getting preinitialized on deserialization?

Is there any way for me to prevent this from happening?

Original issue reported on code.google.com by jimweinh...@gmail.com on 4 Jul 2011 at 7:16

GoogleCodeExporter commented 9 years ago
I saw that you can define in the ProtoContract to skip the constructor, but it 
looks like this isn't supported in the Mono version? It seems like I just need 
to find some way to avoid having the Constructor run.

Original comment by jimweinh...@gmail.com on 4 Jul 2011 at 7:34