1

I have the JSON

["a", 1, 3, 4, 6]

I want to store it into

#[derive(Clone, Debug)]
pub struct Result {
    values: Vec<String>,
}

Is there some simple attribute which I'm missing to achieve this?

This is similar to Convert two types into a single type with Serde, but I'm hoping I wont have to implement anything custom.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
ditoslav
  • 4,563
  • 10
  • 47
  • 79
  • You should derive `Deserialize` on the struct. – Aplet123 Dec 04 '20 at 19:12
  • @Aplet123 that's not a valid solution to this question. – Shepmaster Dec 04 '20 at 19:25
  • It looks like your question might be answered by the answers of [How can I use Serde with a JSON array with different objects for successes and errors?](https://stackoverflow.com/q/37561593/155423); [How to transform fields during deserialization using Serde?](https://stackoverflow.com/q/46753955/155423); [Convert two types into a single type with Serde](https://stackoverflow.com/q/37870428/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Dec 04 '20 at 19:30
  • TL;DR — no, someone has to write the custom logic. – Shepmaster Dec 04 '20 at 19:32
  • I'm asking if there's something shorter than implementing my own deserializer – ditoslav Dec 04 '20 at 19:32
  • 1
    You can deserialize into [`Value`s](https://docs.serde.rs/serde_json/value/enum.Value.html) or [`RawValue`s](https://docs.serde.rs/serde_json/value/struct.RawValue.html) instead of strings – kmdreko Dec 04 '20 at 20:32

0 Answers0