borsh-rs icon indicating copy to clipboard operation
borsh-rs copied to clipboard

Support for adding new fields to previously serialized structs

Open yanliu38 opened this issue 8 months ago • 2 comments

Hi there,

We have some existing serialized data of something like

use borsh::{BorshDeserialize, BorshSerialize};

#[derive(BorshSerialize, BorshDeserialize)]
struct MyStruct {
    field1: u32,
    field2: String,
}

We would now like to add a new field to the existing struct to have something like:

use borsh::{BorshDeserialize, BorshSerialize};

#[derive(BorshSerialize, BorshDeserialize)]
struct MyStruct {
    field1: u32,
    field2: String,
    field3: Option<u64>,
}

However, when deserializing existing data with the new MyStruct, the following error is returned:

Custom { kind: InvalidData, error: "Unexpected length of input" }

What is the recommended way to add fields to an existing struct, that already has serialized data?

yanliu38 avatar Jun 02 '24 20:06 yanliu38