Open GoogleCodeExporter opened 9 years ago
I'm on mobile and haven't had chance to look at the example yet, but I'm
**guessing** the default array values are set in a constructor; in which case
this is not unexpected: options;
- use SkipConstructor to bypass the default initialization
- use a before-deserialization callback for the same
I will investigate to confirm, though
Original comment by marc.gravell
on 26 Feb 2012 at 10:51
I am returning the array directly from the get accessor, I didn't even bother
creating an explicit ctor.
namespace protobuf_array
{
[ProtoContract]
public class FloatArray
{
[ProtoMember(1)]
public float[] floatArray
{
get
{
return new float[] { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f };
}
set
{
float[] t = value;
}
}
}
}
Dan
Original comment by danny.p...@gmail.com
on 27 Feb 2012 at 12:15
Right; indeed. Protobuf is designed (by Google, not me) as an
appendable/mergeable format, which means that new list items are **appended**.
If your "get" always returns data, then deserialization is essentially "get [6
items]; append [now 12 items]; set [12 items]". There are ways of tweaking this
- for example, you can use `OverwriteList = true` in the `[ProtoMember(...)]`
to tell it to replace rather than append. However, fundamentally this is *not*
how array properties are usually implemented, and the unusual scenario is
causing unusual results. A more common scenario would be simple:
[ProtoMember(1)]
public float[] FloatArray {get;set;}
or, for a list:
private readonly List<float> list = new List<float>();
[ProtoMember(1)]
public List<float> FloatList { get { return list; } }
both of which would work fine. If the *constructor* (or field-initializer)
places default items in the list/array, then there are mechanisms (as mentioned
previously) of avoiding that too.
Original comment by marc.gravell
on 27 Feb 2012 at 6:58
That makes perfect sense, now. I wasn't anticipating the "append" behaviour
(Protocol Buffers is just a black-box to me).
I agree that it's not how arrays are usually implemented, but I wanted to
force-feed this application with some predictable data at the front end, so
that I could more easily understand what was coming out of the back of it.
Thank you for your time and help!
Dan
Original comment by danny.p...@gmail.com
on 27 Feb 2012 at 10:25
Original issue reported on code.google.com by
danny.p...@gmail.com
on 26 Feb 2012 at 9:18Attachments: