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

Strange results deserializing an array of structs #25

Open
jsoneaday opened this issue May 26, 2021 · 1 comment
Open

Strange results deserializing an array of structs #25

jsoneaday opened this issue May 26, 2021 · 1 comment

Comments

@jsoneaday
Copy link

jsoneaday commented May 26, 2021

Hello I have an array of structs coming from a Rust app that I am trying to deserialize using buffer-layout. It sort of works but the values are coming in a strange sort of offset. Basically my struct has two fields: archive_id and created_on but the created_on is being filled into the next item in the array instead of together with the matching archive_id.

// this is the rust struct that is coming into my client js code
#[derive(BorshSerialize, BorshDeserialize, Debug)] pub struct ChatMessage { pub archive_id: String, pub created_on: String }

// this is the deserialization code in javascript
const archive_id = lo.cstr("archive_id"); const created_on = lo.cstr("created_on"); const dataStruct = lo.struct([archive_id, created_on], "ChatMessage"); const ds = lo.seq(dataStruct, CHAT_MESSAGE_ELEMENTS_COUNT); const messages = ds.decode(sentAccount.data);

// this is the how the results come in. note on rust side the logs look normal
image

@jsoneaday
Copy link
Author

jsoneaday commented May 26, 2021

Ok guys I'm almost there. I added some padding to the end after each field and now things align. But for some reason array elements get pushed up one.
Also what's up with the single space and plus sign on the last character of the strings?

// new deserialization code. note I'm using u8 because that is the format used by the rust program
const archive_id = lo.cstr("archive_id"); const created_on = lo.cstr("created_on"); const dataStruct = lo.struct( [archive_id, lo.seq(lo.u8(), 3), created_on, lo.seq(lo.u8(), 3)], "ChatMessage" ); const ds = lo.seq(dataStruct, CHAT_MESSAGE_ELEMENTS_COUNT); const messages = ds.decode(sentAccount.data);

image

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

No branches or pull requests

1 participant