Question

I use fixed-point types, e.g.

public struct Fixed16 {
    public int N;
    public static implicit operator double(Fixed16 x)
        { return (double)x / 65536.0; }
    public static implicit operator Fixed16(double x)
        { return Fixed16.FromFixed((int)(x * 65536.0)); }
    public static Fixed16 FromFixed(int n) { return new Fixed16 { N = n }; }
    ...
}

For the sake of efficiency, in protocol buffers I want to store this as a plain integer every time it is used within other types. How?

Was it helpful?

Solution

AFAIK, protobuf-net does not currently support primitive types as surrogates. It could probably be made to work, but it will take a little time of me looking at it. Options:

  • defer on those changes happening, and use primitive surrogate
  • add pass-thru properties to the code that has Fixed16 instances, to expose the data as int, and mark those for serialization - i.e.

    public Fixed16 Foo {get;set;}
    [ProtMember(17)]
    private int FooSerialized { get { return Foo; } set { Foo = value; } }
    
  • mark up Fixed16 to serialize a single internal header, and accept the extra field-header

Note that the first two options should be byte-compatible; the third option has a different layout and is not byte-compatible with the other two.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top