jsonrustserdecbor

How to deserialize/serialize struct as array without keys


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>


Solution

  • 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>,
    }