@@ -206,18 +206,17 @@ UnmarshalPayload(in io.Reader, model interface{})
206
206
207
207
Visit [ godoc] ( http://godoc.org/github.com/google/jsonapi#UnmarshalPayload )
208
208
209
- #### ` MarshalOnePayload `
209
+ #### ` MarshalPayload `
210
210
211
211
``` go
212
- MarshalOnePayload (w io.Writer , model interface {}) error
212
+ MarshalPayload (w io.Writer , models interface {}) error
213
213
```
214
214
215
- Visit [ godoc] ( http://godoc.org/github.com/google/jsonapi#MarshalOnePayload )
215
+ Visit [ godoc] ( http://godoc.org/github.com/google/jsonapi#MarshalPayload )
216
216
217
217
Writes a JSON API response, with related records sideloaded, into an
218
- ` included ` array. This method encodes a response for a single record
219
- only. If you want to serialize many records, see,
220
- [ MarshalManyPayload] ( #marshalmanypayload ) .
218
+ ` included ` array. This method encodes a response for either a single record or
219
+ many records.
221
220
222
221
##### Handler Example Code
223
222
@@ -235,63 +234,7 @@ func CreateBlog(w http.ResponseWriter, r *http.Request) {
235
234
w.Header ().Set (" Content-Type" , jsonapi.MediaType )
236
235
w.WriteHeader (http.StatusCreated )
237
236
238
- if err := jsonapi.MarshalOnePayload (w, blog); err != nil {
239
- http.Error (w, err.Error (), http.StatusInternalServerError )
240
- }
241
- }
242
- ```
243
-
244
- ### List Records Example
245
-
246
- #### ` MarshalManyPayload `
247
-
248
- ``` go
249
- MarshalManyPayload (w io.Writer , models []interface {}) error
250
- ```
251
-
252
- Visit [ godoc] ( http://godoc.org/github.com/google/jsonapi#MarshalManyPayload )
253
-
254
- Takes an ` io.Writer ` and an slice of ` interface{} ` . Note, if you have a
255
- type safe array of your structs, like,
256
-
257
- ``` go
258
- var blogs []*Blog
259
- ```
260
-
261
- you will need to iterate over the slice of ` Blog ` pointers and append
262
- them to an interface array, like,
263
-
264
- ``` go
265
- blogInterface := make ([]interface {}, len (blogs))
266
-
267
- for i , blog := range blogs {
268
- blogInterface[i] = blog
269
- }
270
- ```
271
-
272
- Alternatively, you can insert your ` Blog ` s into a slice of ` interface{} `
273
- the first time. For example when you fetch the ` Blog ` s from the db
274
- ` append ` them to an ` []interface{} ` rather than a ` []*Blog ` . So your
275
- method signature to reach into your data store may look something like
276
- this,
277
-
278
- ``` go
279
- func FetchBlogs () ([]interface {}, error )
280
- ```
281
-
282
- ##### Handler Example Code
283
-
284
- ``` go
285
- func ListBlogs (w http .ResponseWriter , r *http .Request ) {
286
- // ...fetch your blogs, filter, offset, limit, etc...
287
-
288
- // but, for now
289
- blogs := testBlogsForList ()
290
-
291
- w.Header ().Set (" Content-Type" , jsonapi.MediaType )
292
- w.WriteHeader (http.StatusOK )
293
-
294
- if err := jsonapi.MarshalManyPayload (w, blogs); err != nil {
237
+ if err := jsonapi.MarshalPayload (w, blog); err != nil {
295
238
http.Error (w, err.Error (), http.StatusInternalServerError )
296
239
}
297
240
}
@@ -329,7 +272,7 @@ func CreateBlogs(w http.ResponseWriter, r *http.Request) {
329
272
w.Header ().Set (" Content-Type" , jsonapi.MediaType )
330
273
w.WriteHeader (http.StatusCreated )
331
274
332
- if err := jsonapi.MarshalManyPayload (w, blogs); err != nil {
275
+ if err := jsonapi.MarshalPayload (w, blogs); err != nil {
333
276
http.Error (w, err.Error (), http.StatusInternalServerError )
334
277
}
335
278
}
0 commit comments