[][src]Struct jsonapi::api::JsonApiDocument

pub struct JsonApiDocument {
    pub data: Option<PrimaryData>,
    pub included: Option<Resources>,
    pub links: Option<Links>,
    pub meta: Option<Meta>,
    pub errors: Option<JsonApiErrors>,
    pub jsonapi: Option<JsonApiInfo>,
}

The specification refers to this as a top-level document

Fields

data: Option<PrimaryData>included: Option<Resources>links: Option<Links>meta: Option<Meta>errors: Option<JsonApiErrors>jsonapi: Option<JsonApiInfo>

Methods

impl JsonApiDocument[src]

Top-level JSON-API Document

pub fn is_valid(&self) -> bool[src]

This function returns false if the JsonApiDocument contains any violations of the specification. See DocumentValidationError

The spec dictates that the document must have least one of data, errors or meta. Of these, data and errors must not co-exist. The optional field included may only be present if the data field is present too.

use jsonapi::api::{JsonApiDocument, PrimaryData, JsonApiErrors};
let doc = JsonApiDocument {
    data: Some(PrimaryData::None),
    errors: Some(JsonApiErrors::new()),
    ..Default::default()
};

assert_eq!(doc.is_valid(), false);

pub fn validate(&self) -> Option<Vec<DocumentValidationError>>[src]

This function returns a Vec with identified specification violations enumerated in DocumentValidationError

use jsonapi::api::{JsonApiDocument, PrimaryData, JsonApiErrors, DocumentValidationError};

let doc = JsonApiDocument {
    data: Some(PrimaryData::None),
    errors: Some(JsonApiErrors::new()),
    ..Default::default()
};

match doc.validate() {
  Some(errors) => {
    assert!(
      errors.contains(
        &DocumentValidationError::DataWithErrors
      )
    )
  }
  None => assert!(false)
}

Trait Implementations

impl Clone for JsonApiDocument[src]

impl Debug for JsonApiDocument[src]

impl Default for JsonApiDocument[src]

impl<'de> Deserialize<'de> for JsonApiDocument[src]

impl FromStr for JsonApiDocument[src]

type Err = Error

The associated error which can be returned from parsing.

fn from_str(s: &str) -> Result<Self>[src]

Instantiate from string

use jsonapi::api::JsonApiDocument;
use std::str::FromStr;

let serialized = r#"{
  "data" : [
    { "id":"1", "type":"post", "attributes":{}, "relationships":{}, "links" :{} },
    { "id":"2", "type":"post", "attributes":{}, "relationships":{}, "links" :{} },
    { "id":"3", "type":"post", "attributes":{}, "relationships":{}, "links" :{} }
  ]
}"#;
let doc = JsonApiDocument::from_str(&serialized);
assert_eq!(doc.is_ok(), true);

impl PartialEq<JsonApiDocument> for JsonApiDocument[src]

impl Serialize for JsonApiDocument[src]

impl StructuralPartialEq for JsonApiDocument[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.