1

I want to deserialize the following JSON:

[
  {
    "name": "one",
    "path": "/path/to/one"
  },
  {
    "name": "two",
    "path": "/path/to/two"
  },
  {
    "name": "three",
    "path": "/path/to/three"
  }
]

Into a Vec<Worskpace>. Workspace is defined below:

#[derive(Serialize, Deserialize)]
struct Workspace {
    name: String,
    path: String,
}

Is there a way to do that without having to do something like:

#[derive(Serialize, Deserialize)]
struct Workspacesss {
    values: Vec<Workspace>,
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
sargas
  • 5,820
  • 7
  • 50
  • 69
  • What part of your proposed solution do you not like? How would you use your `Workspacesss`? – Shepmaster Mar 06 '18 at 20:24
  • @Shepmaster You're right. I thought that getting a `Vec` was tricky because what is deserializable (done by me) is `Workspace`. Turns out serde_json already got Vec's covered. – sargas Mar 06 '18 at 20:36

1 Answers1

4

Just deserialize the vector directly:

let workspaces = serde_json::from_str::<Vec<Workspace>>(input);
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366