Document implements Serializable { public <DATA extends ResourceIdentifier> ObjectDocument<DATA> asObjectDocument() { return asObjectDocument(0); } Document();  Document(Document document); @Deprecated boolean include(Resource resource); @Deprecated boolean exclude(Resource resource); boolean addInclude(Resource resource); Collection<Resource> getIncluded(); @SuppressWarnings({"SuspiciousMethodCalls", "unchecked"}) T find(ResourceIdentifier resourceIdentifier); T find(String type, String id); @Deprecated boolean errors(List<Error> errors); @Deprecated List<Error> errors(); boolean addError(Error error); boolean setErrors(Collection<Error> errors); List<Error> getErrors(); boolean hasError(); JsonBuffer getMeta(); void setMeta(JsonBuffer meta); JsonBuffer getLinks(); void setLinks(JsonBuffer links); JsonBuffer getJsonApi(); void setJsonApi(JsonBuffer jsonApi); @SuppressWarnings("unchecked") ArrayDocument<DATA> asArrayDocument(); ObjectDocument<DATA> asObjectDocument(); @SuppressWarnings("unchecked") ObjectDocument<DATA> asObjectDocument(int position); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void deserialize_unparameterized() throws Exception { Document document = getDocumentAdapter(null, Person.class) .fromJson("{\"data\":{\"type\":\"people\",\"id\":\"5\"}}"); assertThat(document, instanceOf(ObjectDocument.class)); assertThat(document.asObjectDocument().get().getType(), equalTo("people")); assertThat(document.asObjectDocument().get(), instanceOf(Person.class)); }
@Test public void deserialize_object_to_object_typed_document() throws Exception { Moshi moshi = TestUtil.moshi(Article.class); JsonAdapter<?> adapter = moshi.adapter(Types.newParameterizedType(ObjectDocument.class, Article.class)); assertThat(adapter, instanceOf(ResourceAdapterFactory.DocumentAdapter.class)); ObjectDocument<Article> objectDocument = ((ObjectDocument<Article>) adapter.fromJson(TestUtil.fromResource("/single.json"))); assertThat(objectDocument, instanceOf(ObjectDocument.class)); assertOnArticle1(objectDocument.<Article>asObjectDocument().get()); }
@Test public void deserialize_resource_identifier() throws Exception { ObjectDocument document = getDocumentAdapter(ResourceIdentifier.class) .fromJson(TestUtil.fromResource("/relationship_single.json")).asObjectDocument(); assertThat(document.get(), instanceOf(ResourceIdentifier.class)); assertThat(document.get().getId(), equalTo("12")); assertFalse(document.isNull()); }
@Test public void deserialize_with_null_data() throws Exception { ObjectDocument document = getDocumentAdapter(ResourceIdentifier.class) .fromJson(TestUtil.fromResource("/relationship_single_null.json")) .asObjectDocument(); assertTrue(document.hasData()); assertNull(document.get()); }
@Test public void deserialize_without_data() throws Exception { assertFalse(getDocumentAdapter(ResourceIdentifier.class) .fromJson(TestUtil.fromResource("/meta.json")).asObjectDocument().hasData()); }
@Test public void deserialize_photo_from_value() { Map<String, Object> photoMap = new HashMap(); Map<String, Object> linksNode = new HashMap(); linksNode.put("self", "http: photoMap.put("links", linksNode); Map<String, Object> dataNode = new HashMap(); dataNode.put("type", "photos"); dataNode.put("id", "1"); Map<String, Object> attributesNode = new HashMap(); attributesNode.put("url", "http: attributesNode.put("title", "My Photo"); attributesNode.put("color", "#EF5350"); attributesNode.put("shutter", 23.641); Map<String, Object> locationNode = new HashMap(); locationNode.put("latitude", 39.9042); locationNode.put("longitude", 116.4074); attributesNode.put("location", locationNode); dataNode.put("attributes", attributesNode); photoMap.put("data", dataNode); Document document = getDocumentAdapter(null, Photo.class).fromJsonValue(photoMap); Photo photo = (Photo) document.asObjectDocument().get(); assertEquals(new Double(23.641), photo.getShutter()); assertEquals(new Double(39.9042), photo.getLocation().latitude); }
@Test public void deserialize_object() throws Exception { Document document = getDocumentAdapter(Article.class) .fromJson(TestUtil.fromResource("/single.json")); assertThat(document, instanceOf(ObjectDocument.class)); assertOnArticle1(document.<Article>asObjectDocument().get()); }
@Test public void deserialize_object_null() throws Exception { Document document = getDocumentAdapter(Article.class) .fromJson(TestUtil.fromResource("/single_null.json")); assertNull(document.asObjectDocument().get()); }
@Test public void deserialize_private_type() throws Exception { Document document = getDocumentAdapter(Article2.class) .fromJson(TestUtil.fromResource("/single.json")); assertOnArticle1(document.<Article>asObjectDocument().get()); }
@Test public void deserialize_polymorphic_type() throws Exception { Resource resource = getDocumentAdapter(Resource.class, Article.class) .fromJson(TestUtil.fromResource("/single.json")).<Article>asObjectDocument() .get(); assertThat(resource, instanceOf(Article.class)); assertOnArticle1(((Article) resource)); }
@Test public void deserialize_polymorphic_fallback() throws Exception { Resource resource = getDocumentAdapter(Resource.class) .fromJson(TestUtil.fromResource("/single.json")) .<Resource>asObjectDocument().get(); assertThat(resource.getId(), equalTo("1")); assertThat(resource, instanceOf(TestUtil.Default.class)); }