2

I'm writing a microservice that takes a json object as input. This json object is only partially known and therefore the struct to which I map it looks like this:

#[derive(Serialize, Deserialize)]
pub struct Incoming {
    uri: String,
    payload: serde_json::Value,
    id: String
}

I then want to publish this to a RabbitMQ queue so I serialize it using bincode:

let incoming = serde_json::from_slice::<Incoming>(&incoming).expect("Fail to serialize");
// This line fails:
bincode::serialize(&incoming).expect("Failed to deserialize to binary");

The receiving service (the consumer) fails to deserialize this (even though it has the exact same model) and results in Err(DeserializeAnyNotSupported).

From my understanding this comed from the serde_json::Value-part of the struct. So how do I serialize a partially unknown JSON-object to binary in order to deserialize it on the receiving service?

Boiethios
  • 38,438
  • 19
  • 134
  • 183
AdaShoelace
  • 342
  • 1
  • 14
  • Is payload a single object? – Boiethios Dec 05 '19 at 10:00
  • @FrenchBoiethios It is a single object but it can ( and most likely will ) be nested objects. If that is what you wonder? – AdaShoelace Dec 05 '19 at 10:01
  • Yes. What is the operation which fails? – Boiethios Dec 05 '19 at 10:05
  • @FrenchBoiethios The deserializing once we consume the message from RabbitMQ: `bincode::deserialize::(delivery.data.as_slice());` – AdaShoelace Dec 05 '19 at 10:08
  • 1
    It seems the json object initially presented to you was `[u8]`? (Thus you called `serde_json::from_slice` on it). Would sending that verbatim and decoding it on the receiver side work? – edwardw Dec 05 '19 at 10:11
  • It seems that this just isn't supported by `bincode`: https://docs.rs/bincode/1.2.0/bincode/enum.ErrorKind.html#variant.DeserializeAnyNotSupported But I don't know much about that – Boiethios Dec 05 '19 at 10:13
  • @edwardw It comes from a `hyper::Chunk`. Unfortunately I need to serialize it into the struct for other purposes before publishing it to RabbitMQ. Though I might be able to clone it and publish the raw chunk anyway. – AdaShoelace Dec 05 '19 at 10:17
  • @edwardw It worked! I now publish the `hyper::Chunk` verbatim to RabbitMQ and deserialze it using `serde _json` in the consumer without any problems. – AdaShoelace Dec 05 '19 at 13:03

0 Answers0