@@ -25,46 +25,15 @@ You're now ready to serialize/deserialize JSON API objects with cool Moshi inter
25
25
26
26
``` java
27
27
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();
31
29
for (Article article : articles) {
32
30
System . out. println(article. title);
33
31
}
34
32
```
35
33
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
66
35
67
- ## Modelling
36
+ ### Resource Object
68
37
69
38
Extend a ` Resource ` class to create a model for resource object.
70
39
@@ -129,14 +98,22 @@ public class Article extends Resource {
129
98
130
99
### Document
131
100
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
+
132
110
``` java
133
- Document <Article > document = new ObjectDocument<> ();
111
+ ObjectDocument <Article > document = new ObjectDocument<> ();
134
112
document. set(article);
135
113
document. include(author);
136
114
137
115
// 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));
140
117
// => {
141
118
// data: { "type": "articles", "relationships": { "author": { "data": "type": "people", id: "1" } } },
142
119
// included: [
@@ -145,17 +122,17 @@ System.out.println(adapter.toJson(document));
145
122
// }
146
123
147
124
// 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
151
129
```
152
130
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() ` ) .
154
132
155
- ### Fallback
133
+ ### Default Resource Type
156
134
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.
159
136
160
137
``` java
161
138
@JsonApi (type = " default" )
@@ -169,13 +146,50 @@ class Unknown extends Resource {
169
146
You'd like to access ` meta ` /` links ` /` jsonapi ` value on ` Document ` for example.
170
147
171
148
``` java
172
- Document< Article > document = ... ;
149
+ Document document = ... ;
173
150
document. getMeta() // => JsonBuffer
174
151
```
175
152
176
153
As ` meta ` and ` links ` can contain a variant of objects, they are not been parsed when access with ` getMeta ` and ` getLinks ` .
177
154
You will get a ` JsonBuffer ` and you're expected to implement your ` JsonAdapter<T> ` to read/write these objects.
178
155
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
+
179
193
## Download
180
194
181
195
In gradle build script:
@@ -186,12 +200,13 @@ repositories {
186
200
}
187
201
188
202
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
191
206
}
192
207
```
193
208
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.
195
210
196
211
Use snapshot version:
197
212
0 commit comments