Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In C# implementation, how to parse to structs separated by BT_STOP_BASE #1162

Closed
dend opened this issue Aug 19, 2022 · 2 comments
Closed

In C# implementation, how to parse to structs separated by BT_STOP_BASE #1162

dend opened this issue Aug 19, 2022 · 2 comments
Labels

Comments

@dend
Copy link

dend commented Aug 19, 2022

Let's say I have an example, where I have a BT_STRUCT with field ID 0, with data (which in turn comes with field IDs of 0, 1, and 2). I then encounter a BT_STOP_BASE and there is a new struct, also with field ID 0, with its own nested data (set of fields, with IDs of 0, 1, and 2).

Given that the structs themselves are not part of a specific field, and are sequential - what is the way to represent that in a C# class, so that I can deserialize/serialize the data? I am assuming some kind of entity with nested sub-classes, but so far I have been unsuccessful at replicating this.

Tagging @chwarr since he helped with some previous questions related to this (#74 (comment)).

@chwarr
Copy link
Member

chwarr commented Aug 19, 2022

BT_STOP_BASE is used to delimit where a base class ends when inheritance is used.

The following Bond

struct Foo
{
    0: string foo_string;
}

struct Bar : Foo
{
    0: string bar_string;
}

gets turned into C# that looks roughly like this:

class Foo
{
    public string foo_string { get; set; }
}

class Bar : Foo
{
    public string bar_string { get; set; }
}

Say you serialize one of these to Compact Binary v1:

var obj = new Bar { foo_string = "foo", bar_string = "bar" };

var output = new OutputBuffer();
var writer = new CompactBinaryWriter<OutputBuffer>(output);
Serialize.To(writer, src);

The Compact Binary payload would be, in pseudo payload:

  1. BT_STRING { id: 0, length: 3 }
  2. foo
  3. BT_STOP_BASE
  4. BT_STRING { id: 0, length: 3 }
  5. bar
  6. BT_STOP

If you added another layer, struct Baz : Bar { 0: string baz_string; }, there would be two BT_STOP_BASE tokens, one after the Foo and one after the Bar:

  1. BT_STRING { id: 0, length: 3 }
  2. foo
  3. BT_STOP_BASE
  4. BT_STRING { id: 0, length: 3 }
  5. bar
  6. BT_STOP_BASE
  7. BT_STRING { id: 0, length: 3 }
  8. baz
  9. BT_STOP

@dend
Copy link
Author

dend commented Aug 20, 2022

@chwarr amazing - this works really well, thank you for a very thorough and concrete explanation!

@dend dend closed this as completed Aug 20, 2022
@chwarr chwarr changed the title [Question] In C# implementation, how to parse to structs separated by BT_STOP_BASE In C# implementation, how to parse to structs separated by BT_STOP_BASE Aug 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants