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>,
}
2
  • What part of your proposed solution do you not like? How would you use your Workspacesss? Commented Mar 6, 2018 at 20:24
  • @Shepmaster You're right. I thought that getting a Vec<Workspace> was tricky because what is deserializable (done by me) is Workspace. Turns out serde_json already got Vec's covered. Commented Mar 6, 2018 at 20:36

1 Answer 1

4

Just deserialize the vector directly:

let workspaces = serde_json::from_str::<Vec<Workspace>>(input);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm a fool for not understanding the serde API.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.