CMertens / protobuf-net

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

Serialize - Deserialize Simple Array of int results array size * 2 #261

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============

What steps will reproduce the problem?
1. Create simple class with public int[] iarray = new int[10]
2. Seriliaze to file
3. Deserialize to class, the array length is double size of orjinal

What is the expected output? What do you see instead?
We expected same size of array which seriliazed

What version of the product are you using? On what operating system?
r482

Please provide any additional information below.
Sample code:
namespace ConsoleApplication12
{
    [ProtoContract]
    public class _data
    {
        /// when deserialize, array length is 20
        private int[] dataa = new int[10];
        public _data() {

            for (int y = 0; y < 10; y++)
                dataa[y] = y * y;

        }
        [ProtoMember(2)]
        public Int32[] __data
        {
            get { return this.dataa; }
            set { this.dataa = value; }
        }
        [ProtoMember(1)]
        public string __data2 { get; set; }

        [ProtoMember(3)]
        public ulong v_ulong = 2899322L;

        [ProtoMember(4)]
        public List<_data> childs { get; set; }
    }
    class Program
    {

        static void Main(string[] args)
        {

            File.Delete("c:\\test\\test6371.file");
            _data r;

            var d = new _data() { __data2=System.DateTime.Now.ToLongTimeString() };
            d.childs = new List<_data>();
            d.childs.Add(new _data());
            d.childs.Add(new _data());

            using(Stream f  = File.OpenWrite("c:\\test\\test6371.file")) {
                ProtoBuf.Serializer.Serialize(f, d);
            }

            using(Stream s = File.OpenRead("c:\\test\\test6371.file")) {
                 r = ProtoBuf.Serializer.Deserialize<_data>(s);
            }
        }

    }
}

Original issue reported on code.google.com by emre.ser...@gmail.com on 25 Dec 2011 at 11:30

GoogleCodeExporter commented 9 years ago
That is because our constructor is adding them to a newly constructed object; 
there are a number of ways of avoiding this:

- don't use the constructor to put data into lists of new objects!
- turn off the constructor: see SkipConstuctor in ProtoContract
- tell it overwrite the list (Via ProtoMember, but not the best option here)
- use a serialization callback to clear the list at the start of deserialization

The "append" behaviour is because protobuf (the Google spec) **is** explicitly 
appendable and merge able, with any new list elements beig appended rather than 
replacing existing content 

Original comment by marc.gravell on 26 Dec 2011 at 9:10

GoogleCodeExporter commented 9 years ago
Thanks for the response, I just fill the array at constructor to see the 
situation, actually turn of constructor worked for me, "SkipConstuctor=true".

Thanks.

Original comment by emre.ser...@gmail.com on 26 Dec 2011 at 4:21