ameyac
ameyac

Reputation: 21

Cannot serialize a string object with Microsoft.Bond

I am using Microsoft.Bond to serialize a class object which works perfectly fine. However, when I try to serialize a simple System.String object, the CompactBinaryWriter writes almost nothing to the output buffer. I am using this code:

string v = "test data";
var outputBuffer = new OutputBuffer();
var writer = new CompactBinaryWriter<OutputBuffer>(outputBuffer);
Serialize.To(writer, v);
var output = outputBuffer.Data;

output in this case is a one element array : {0}, irrespective of the value of v. Can someone point out why this doesn't work?

Upvotes: 2

Views: 345

Answers (1)

chwarr
chwarr

Reputation: 7202

Bond requires a top-level Bond struct to perform serialization/deserialization.

If only one value needs to be passed/returned, the type bond.Box<T> can be used to quickly wrap a value in a Bond struct. (There's nothing special about bond.Box<T>, except that it ships with Bond.)

Try this:

Serialize.To(writer, Bond.Box.Create(v));

You'll need to deserialize into a bond.Box<string>.

There's an open issue about having better behavior in cases like this.

Upvotes: 1

Related Questions