2

I am having the below protobuf text and would like to deserialize in C#. I have tried the below code but it keep throwing error 'Type is not expected, and no contract can be inferred: System.Object. I know I require a schema is there a way turn the protobuf text data into string or Json. I am using Marc Gavell library protobuf-net.

public void DeserializeProtobuf()
{
    var path = $"../../protobufSingleEntry.txt";              
    byte[] byteArray = Encoding.UTF8.GetBytes(path);
    MemoryStream stream = new MemoryStream(byteArray);

    Serializer.Deserialize<object>(stream);
}

protobufSingleEntry.txt

00000000: 7e6d 7e37 357e 6d7e 0829 1247 0a0f 7173  ~m~75~m~.).G..qs
00000001: 5f67 554d 4463 7556 514e 6837 3912 0f42  _gUMDcuVQNh79..B
00000002: 494e 414e 4345 3a42 5443 5553 4454 1a02  INANCE:BTCUSDT..
00000003: 6f6b 293d 0ad7 a3b0 7cc6 4035 0ad7 73c3  ok)=....|[email protected].
00000004: 3de1 7a04 c051 5c05 31d0 037d cc40 9a01  =.z..Q\.1..}.@..
00000005: 027b 7d7e 6d7e 3735 7e6d 7e08 2912 470a  .{}~m~75~m~.).G.
00000006: 0f71 735f 4e7a 784f 7a4a 4d79 4277 6b6b  .qs_NzxOzJMyBwkk
00000007: 120f 4249 4e41 4e43 453a 4254 4355 5344  ..BINANCE:BTCUSD
00000008: 541a 026f 6b29 3d0a d7a3 b07c c640 350a  T..ok)=....|.@5.
00000009: d773 c33d e17a 04c0 515c 0531 d003 7dcc  .s.=.z..Q\.1..}.
0000000a: 409a 0102 7b7d                           @...{}

1 Answer 1

2

The problem here is: protobuf is an ambiguous format. The same bytes could mean very different things depending on the information only known in the schema. For example, raw binary (bytes) payloads, text strings, sub-messages, and "packed" primitive values all share the exact same markings. Likewise, for most primitive types there are multiple ways of representing the same value, and yet also all the primitive types overlap in how they are encoded.

It is certainly possible to decode the data abstractly without schema - everything can be treated as an unknown field, and you can query unknown fields via an API. But that still doesn't help you actually interpret the data, because again: you need to know what you're expecting to apply the correct decoding.

IMO your best bet here is to actually reverse engineer the schema. You could start with https://protogen.marcgravell.com/decode to show what the actual payload contains, and start piecing it together from there.

Sign up to request clarification or add additional context in comments.

1 Comment

I do have the decode but it's in JavaScript. jsfiddle.net/maxspan/xyrc0w4d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.