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

C++ - Deserialize on a "non-serialized" bonded<T> should copy #1057

Open
bch29 opened this issue Jul 2, 2020 · 1 comment
Open

C++ - Deserialize on a "non-serialized" bonded<T> should copy #1057

bch29 opened this issue Jul 2, 2020 · 1 comment

Comments

@bch29
Copy link

bch29 commented Jul 2, 2020

I understand that the below is fundamentally useless, but I see no reason why it shouldn't work. It's essentially a transcoding from an in-memory value to the same representation. I think it should be supported for consistency's sake. This bit me when trying to write a unit test for some code where I wanted to fake receiving a bond encoded object from a server.

MyStruct value;
bond::bonded<MyStruct> bonded(value);

MyStruct value2;
bonded.Deserialize(value2);
@chwarr
Copy link
Member

chwarr commented Dec 8, 2020

If I remember correctly, we decided not to implement "cloning via Bond" so that it wasn't easy to fall into a performance trap using the serialization/deserialization APIs. The "escape hatch" is to explicitly serialize to a buffer and then create a bond::bonded instance over the buffer:

template <typename ValueType>
static bond::bonded<typename ValueType> SerializeToBonded(const typename ValueType& value)
{
    bond::OutputBuffer outputBuffer;
    bond::CompactBinaryWriter<bond::OutputBuffer> writer(outputBuffer);
    bond::Apply(bond::SerializeTo(writer), value);

    bond::InputBuffer inputBuffer(outputBuffer.GetBuffer());
    bond::CompactBinaryReader<bond::InputBuffer> reader(inputBuffer);
    return bond::bonded<ValueType>(reader, false /*isBaseType*/);
}

Writing unit tests is the only case that I can remember where the need for this API comes up. (I've run into this exact problem myself before, and I implemented the aforementioned serialization function.)

I'd be fine with a behavior change here if someone wanted to submit it. Unit tests would be needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants