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

fix(string): fix deserialization of string with default value #37

Conversation

finger563
Copy link
Contributor

@finger563 finger563 commented May 14, 2024

If I have a struct with a std::string in it that has a default value, e.g.

struct MyStruct {
  std::string my_string = "default_value";
};

And I want to serialize an instance of that struct such as:

MyStruct my_struct {.my_string = "custom_value"};

It will serialize properly, containing only the length byte and the bytes for "custom_value".

However, when I deserialize it I will get a struct which contains a string:

"default_valuecustom_value"

This is because the deserialization creates a default instance of the struct (using T()), and then the string from_bytes(...) does a value += character for each byte in the stream.

This PR fixes that by clearing out the string when deserializing it, so that the deserialized struct's string will only contain

"custom_value"

@p-ranav p-ranav merged commit bcef014 into p-ranav:master May 14, 2024
7 checks passed
@finger563 finger563 deleted the fix/string-deserialization-with-default-value branch May 14, 2024 15:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants