Skip to content

Commit 03031ca

Browse files
committed
update README.md
1 parent 597c34b commit 03031ca

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

README.md

+63-48
Original file line numberDiff line numberDiff line change
@@ -25,46 +25,15 @@ You're now ready to serialize/deserialize JSON API objects with cool Moshi inter
2525

2626
```java
2727
String json = "...";
28-
Type type = Types.newParameterizedType(Document.class, Article.class); // Type of Document<Article>
29-
JsonAdapter<Document<Article>> adapter = ((JsonAdapter<Document<Article>>) moshi.adapter(type));
30-
ArrayDocument<Article> articles = adapter.fromJson(json).asArrayDocument();
28+
ArrayDocument<Article> articles = moshi.adapter(Document.class).fromJson(json).asArrayDocument();
3129
for (Article article : articles) {
3230
System.out.println(article.title);
3331
}
3432
```
3533

36-
### Retrofit
37-
38-
Simply add a [retrofit converter](https://gist.github.com/kamikat/baa7d086f932b0dc4fc3f9f02e37a485) and you get all the
39-
cool stuff in Retrofit!
40-
41-
```java
42-
public interface MyAPI {
43-
44-
@GET("posts")
45-
Call<Post[]> listPosts();
46-
47-
@GET("posts/{id}")
48-
Call<Post> getPost(@Path("id") String id);
49-
50-
@GET("posts/{id}/comments")
51-
Call<Comment[]> getComments(@Path("id") String id);
52-
53-
@POST("posts/{id}/comments")
54-
Call<Document> addComment(@Path("id") String id, @Body Comment comment);
55-
56-
@DELETE("posts/{id}/relationships/comments")
57-
Call<Document> removeComments(@Path("id") String id, @Body ResourceIdentifier[] commentIds);
58-
59-
@GET("posts/{id}/relationships/comments")
60-
Call<ResourceIdentifier[]> getCommentRels(@Path("id") String id);
61-
}
62-
```
63-
64-
No annoying `Call<Document<RESOURCE>>` declaration required and `Document` is wrap/unwrapped automatically by the converter.
65-
And use that declaration when need `Document` to collecting errors or any other information.
34+
## Usage
6635

67-
## Modelling
36+
### Resource Object
6837

6938
Extend a `Resource` class to create a model for resource object.
7039

@@ -129,14 +98,22 @@ public class Article extends Resource {
12998

13099
### Document
131100

101+
`Document` interfaces denotes a JSON API document, document object contains one of the following attributes:
102+
103+
- `data` the primary data, can be null, resource object or array of resource object
104+
- `error` error object
105+
- `meta`
106+
107+
To keep consistency with the specification, moshi-jsonapi implements `ArrayDocument<T>` and `ObjectDocument<T>`.
108+
`Document` object can be converted with `Document.<T>asXDocument()` function.
109+
132110
```java
133-
Document<Article> document = new ObjectDocument<>();
111+
ObjectDocument<Article> document = new ObjectDocument<>();
134112
document.set(article);
135113
document.include(author);
136114

137115
// Serialize
138-
JsonAdapter<Document<Article>> adapter = moshi.adapter(document.getType());
139-
System.out.println(adapter.toJson(document));
116+
System.out.println(moshi.adapter(Document.class).toJson(document));
140117
// => {
141118
// data: { "type": "articles", "relationships": { "author": { "data": "type": "people", id: "1" } } },
142119
// included: [
@@ -145,17 +122,17 @@ System.out.println(adapter.toJson(document));
145122
// }
146123

147124
// Deserialize
148-
Document<Article> document2 = adapter.fromJson(...);
149-
assert document2.get() instanceof Article
150-
assert document2.get().getDocument() == document2
125+
Document document2 = adapter.fromJson(...);
126+
ObjectDocument<Article> document3 = document2.asObjectDocument();
127+
assert document3.get() instanceof Article
128+
assert document3.get().getDocument() == document3
151129
```
152130

153-
All resources added/included in a `Document` will keep a reference which can be accessed from `Resource.getDocument`.
131+
The linkage (relationship) of a resource object is resolved in document of the resource object (check `Resource.getDocument()`).
154132

155-
### Fallback
133+
### Default Resource Type
156134

157-
Deserialization will fail when processing an unknown type of resource.
158-
Create a `default` typed model to avoid this problem and parses all unknown type of resource object into the default model.
135+
Create a `default` typed class to have all unknown type parsed in the class to avoid deserialization error processing unknown type of resource.
159136

160137
```java
161138
@JsonApi(type = "default")
@@ -169,13 +146,50 @@ class Unknown extends Resource {
169146
You'd like to access `meta`/`links`/`jsonapi` value on `Document` for example.
170147

171148
```java
172-
Document<Article> document = ...;
149+
Document document = ...;
173150
document.getMeta() // => JsonBuffer
174151
```
175152

176153
As `meta` and `links` can contain a variant of objects, they are not been parsed when access with `getMeta` and `getLinks`.
177154
You will get a `JsonBuffer` and you're expected to implement your `JsonAdapter<T>` to read/write these objects.
178155

156+
### Retrofit
157+
158+
Retrofit extension library (see following section) provides `JsonApiConverterFactory` to get integrate with Retrofit 2.
159+
Here's an example:
160+
161+
```java
162+
Retrofit retrofit = new Retrofit.Builder()
163+
// ...
164+
.addConverterFactory(JsonApiConverterFactory.create(moshi))
165+
.build()
166+
retrofit.create(MyAPI.class);
167+
```
168+
169+
And `MyAPI` interface:
170+
171+
```java
172+
public interface MyAPI {
173+
174+
@GET("posts")
175+
Call<Post[]> listPosts();
176+
177+
@GET("posts/{id}")
178+
Call<Post> getPost(@Path("id") String id);
179+
180+
@GET("posts/{id}/comments")
181+
Call<List<Comment>> getComments(@Path("id") String id);
182+
183+
@POST("posts/{id}/comments")
184+
Call<Document> addComment(@Path("id") String id, @Body Comment comment);
185+
186+
@GET("posts/{id}/relationships/comments")
187+
Call<ResourceIdentifier[]> getCommentRels(@Path("id") String id);
188+
}
189+
```
190+
191+
Note that the body can either be serialized/deserialized to resource object or document object with additional information.
192+
179193
## Download
180194

181195
In gradle build script:
@@ -186,12 +200,13 @@ repositories {
186200
}
187201
188202
dependencies {
189-
implementation 'com.squareup.moshi:moshi:1.4.0'
190-
implementation 'moe.banana:moshi-jsonapi:<version>'
203+
implementation 'com.squareup.moshi:moshi:1.4.0' // required, peer dependency to moshi
204+
implementation 'moe.banana:moshi-jsonapi:<version>' // required, core library
205+
implementation 'moe.banana:moshi-jsonapi-retrofit-converter:<version>' // optional, for retrofit
191206
}
192207
```
193208

194-
For library version >= 3.6, moshi is removed from runtime dependencies of the library to become a peer dependency.
209+
For library version >= 3.5, moshi is removed from runtime dependencies of the library to become a peer dependency.
195210

196211
Use snapshot version:
197212

0 commit comments

Comments
 (0)