i'm new to protocol buffers and I am using protobuf-net for VS2010. from what i'm reading here Dictionary in protocol buffers, it doesn't seem that protobuf can serialize a dictionary with object types as values. but here on his site i read this:
Notes on types
supported:
custom classes that: are marked as data-contract have a parameterless constructor for Silverlight: are public many common primitives etc single dimension arrays: T[] List / IList Dictionary / IDictionary any type which implements IEnumerable and has an Add(T) method The code assumes that types will be mutable around the elected members. Accordingly, custom structs are not supported, since they should be immutable.
which seems like it is supported.
I can successfully compile a List of objects like so:
message ValuesObject {
optional int32 SomeVal = 1;
repeated SomeClass ListOfSomeClassTypes = 2;
}
This works fine for a List<SomeClass>
. Why cannot I serialize using protobuf-net a Dictionary<int, SomeClass>
? What would the message look like to serialize a Dictionary<int, SomeClass>
?
A Dictionary<int,SomeClass>
is perfectly serailizable with protobuf-net. Protobuf-net works simplest when working code-first, so: *just have a Dictionary<int,SomeClass>
in your model. You are not required to use a .proto at all - that is provided mainly for cross-platform purposes. The .proto specification has no concept of a dictionary, but if you are required to use a .proto schema, then this is serialized as:
message KeyValuePairInt32SomeClass {
required int32 Key = 1;
required SomeClass Value = 2;
}
with the dictionary as
repeated KeyValuePairInt32SomeClass YourDictionary = [n];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With