#Cannot serialize payload

1 messages · Page 1 of 1 (latest)

deft stream
#
#[derive(Clone, serde::Serialize, PartialEq, Eq, PartialOrd, Ord)]
struct SomethingKey {
    x: u16,
    y: u16,
}

#[derive(Clone, serde::Serialize, PartialEq, Eq, PartialOrd, Ord)]
struct Something {
    title: String,
    path: PathBuf,
}

#[derive(Clone, serde::Serialize)]
pub struct Payload {
    stuff: HashMap<String, BTreeMap<SomethingKey, Something>>,
}

I cannot send the payload over to the frontend, likely because of the u16 keys of the BTreeMap. Is it possible to somehow convert the keys to string before sending them, maybe? Or some other solution? I've tried using #[serde_as] but to no avail

pseudo solar
#

What is the actual issue you're observing? Any errors? Or just weird behavior? In case of the latter, what is the behavior?

solemn drum
#

99% sure the issue is that you're using SomethingKey as map key, since JSON only allows string keys. I don't think there's a good way to solve this apart from just making the map key type something supported by serde_json

#

Maybe it's possible to override ser/de for the whole SomethingKey struct? idk

pseudo solar
deft stream
pseudo solar
#

Maybe it's possible to override ser/de for the whole SomethingKey struct? idk
ig the example for the custom error type can be a starting point for that: https://tauri.app/v1/guides/features/command#error-handling - the custom Serialize impl. But of course instead of the self.to_string() you'd do something that converts x and y to a single string

deft stream
#

talking about the key of the btreemap

solemn drum
#

Yeah making the key a string would work

#

The more js-y way of doing it would be to do nested map

deft stream
#

I had to get rid of the SomethingKey.... it really had to be done