1

How can I de/serialize struct without keys ? Since i will not be changing the order of fields, Indexes can act as keys which will reduce the size of payload.

I'm working with serde_json and ciborium crates which both have top level array value.

for example,

following cbor/json

["string1", 1, 1.1, [4, 34, 4, 4]]

should be converted into this

struct Foo {
    a: String,
    b: u128,
    c: f64,
    d: Vec<u8>,
}

I could manually do this by using Value::array enum of ciborium crate but then I will end up writing more code and won't be able to use serde_bytes crate to efficiently decode Vec<u8>

1

1 Answer 1

2

Simply use serde_tuple that is dedicated to "De/serialize struct as an array of values" issue #637:

#[derive(serde_tuple::Serialize_tuple, serde_tuple::Deserialize_tuple)]
struct Foo {
    a: String,
    b: u128,
    c: f64,
    d: Vec<u8>,
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.