michiel / jsonapi-rust

Rust library for serialization, deserialization and working with JSON-API data
MIT License
109 stars 30 forks source link

usage for `from_jsonapi_document` to collection of models #45

Closed jakerobers closed 6 years ago

jakerobers commented 6 years ago

I'm a beginner with rust, and apologize if this question is really basic:

What does the usage look like for converting a JsonApiDocument with an array of results to a vector of jsonapi_model structs.

It looks like I need to handle this case https://github.com/michiel/jsonapi-rust/blob/master/src/model.rs#L38 but when it hits https://github.com/michiel/jsonapi-rust/blob/master/src/model.rs#L182, the return type is the singular jsonapi_model and not a vector of them (I think).

thanks in advance

jakerobers commented 6 years ago

Never mind, I think I have it figured out. Posting solution for others:

let document = response.json::<JsonApiDocument>();
match document.data {
        Some(PrimaryData::Multiple(vacations)) => {
            for resource in vacations.iter() {
                let vacation: Vacation = Vacation::from_jsonapi_resource(resource, &document.included).unwrap();
                print!("vacation {}", vacation.start_date);
            }
        },
        _ => print!("there was an error")
    }
lock[bot] commented 5 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.