Yes, protobuf-net works without .proto files. In fact, for protobuf-net the core was written long before the .proto support. All you need is something that allows it to obtain a unique number for each member you want to serialize. In "v2" (not released) you can do this externally, but in the available dll this means attributes. This can be the bespoke protobuf-net attributes, or you can use (for example) the DataContractSerializer
attributes if that is more convenient:
[DataContract]
public class Foo {
[DataMember(Order=1)]
public int Abc {get;set;}
[DataMember(Order=2)]
public string Def {get;set;}
}
There is support for generating a .proto file (Serializer.GetProto
) - however, there are some caveats:
- protobuf-net supports inheritance; there is no definition of inheritance in the Google protobuf spec. If you think interop is a concern down the line, it may be best to avoid inheritance (although there is a way of interpreting it; probofuf-net always emits a valid protobuf stream)
- other complex types may require interpretation; for example while
float
and double
map directly to protobuf types (available in every implementation), that is not true of decimal
and DateTime
; if you think interop is an issue, it may be worth simplifying this from the start - for example deciding that you are going to send a DateTime
, perhaps send a long
that represents the offset into an epoch instead? easy to interpret in any implementation.