You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
@pp4p since posting that sample code the Marshal API has been significantly simplified. You no longer need to populate a slice of interfaces []interface{}. Furthermore, Marshal first return will be a value that implements the Payloader interface. Since we passed Marshal a slice, the Payloader returned will always be a *jsonapi.ManyPayload.
You can now get away with just doing main.go:
package main
import (
"bytes""encoding/json""fmt""github.com/google/jsonapi"
)
typeAlbumstruct {
IDint`jsonapi:"primary,album"`Namestring`jsonapi:"attr,name"`
}
typeAlbumsstruct {
Albums []*Album`jsonapi:"relation,albums"`
}
funcmain() {
albums:= []*Album{
&Album{ID: 3, Name: "JSON API paints my bikeshed!"},
}
p, err:=jsonapi.Marshal(albums)
iferr!=nil {
panic(err)
}
payload, ok:=p.(*jsonapi.ManyPayload)
if!ok {
panic(fmt.Errorf("was not a many payloader"))
}
payload.Meta=&jsonapi.Meta{
"total-pages": 13,
}
payload.Links=&jsonapi.Links{
"self": "http://example.com/articles?page[number]=3&page[size]=1",
"first": "http://example.com/articles?page[number]=1&page[size]=1",
"prev": "http://example.com/articles?page[number]=2&page[size]=1",
"next": "http://example.com/articles?page[number]=4&page[size]=1",
"last": "http://example.com/articles?page[number]=13&page[size]=1",
}
w:=bytes.NewBuffer(nil)
iferr:=json.NewEncoder(w).Encode(payload); err!=nil {
panic(err)
}
fmt.Println(w)
}
I thought about submitting a PR to implement this in the library, since the official site recommends top level links be included alongside resource-level and relationship links. I think it's important to keep the library simple, so I decided against writing this feature into the library. Would it make sense to write some documentation for how to achieve this recommended functionality.
The solution provided here only addresses marshaling links into a structure. We also need a solution for unmarshalling links from a structure that uses jsonapi structure tags for other fields. Adding additional links/meta tags to the top document as the original poster requested would really make the jsonapi much easier to use. And while an example of the solution given in #68 is provided in https://github.com/google/jsonapi/blob/master/request_test.go#L694, that example does not show how to demarshal a structure that uses jsonapi structure tags.
Activity
aren55555 commentedon Jun 28, 2017
You can do something like the following:
Which will print the following JSON:
{ "data": [{ "type": "album", "id": "3", "attributes": { "name": "JSON API paints my bikeshed!" } }], "links": { "first": "http://example.com/articles?page[number]=1&page[size]=1", "last": "http://example.com/articles?page[number]=13&page[size]=1", "next": "http://example.com/articles?page[number]=4&page[size]=1", "prev": "http://example.com/articles?page[number]=2&page[size]=1", "self": "http://example.com/articles?page[number]=3&page[size]=1" }, "meta": { "total-pages": 13 } }imhuytq commentedon Aug 3, 2017
@aren55555 Now, MarshalMany isn't public function, how can I use it?
aren55555 commentedon Aug 3, 2017
@pp4p since posting that sample code the Marshal API has been significantly simplified. You no longer need to populate a slice of interfaces
[]interface{}. Furthermore,Marshalfirst return will be a value that implements thePayloaderinterface. Since we passedMarshala slice, thePayloaderreturned will always be a*jsonapi.ManyPayload.You can now get away with just doing
main.go:go run main.gowill produce:{ "data": [{ "type": "album", "id": "3", "attributes": { "name": "JSON API paints my bikeshed!" } }], "links": { "first": "http://example.com/articles?page[number]=1\u0026page[size]=1", "last": "http://example.com/articles?page[number]=13\u0026page[size]=1", "next": "http://example.com/articles?page[number]=4\u0026page[size]=1", "prev": "http://example.com/articles?page[number]=2\u0026page[size]=1", "self": "http://example.com/articles?page[number]=3\u0026page[size]=1" }, "meta": { "total-pages": 13 } }imhuytq commentedon Aug 3, 2017
@aren55555 thank you.
mrhwick commentedon Nov 29, 2017
I thought about submitting a PR to implement this in the library, since the official site recommends top level
linksbe included alongside resource-level and relationship links. I think it's important to keep the library simple, so I decided against writing this feature into the library. Would it make sense to write some documentation for how to achieve this recommended functionality.rberlind commentedon Nov 9, 2020
The solution provided here only addresses marshaling links into a structure. We also need a solution for unmarshalling links from a structure that uses jsonapi structure tags for other fields. Adding additional links/meta tags to the top document as the original poster requested would really make the jsonapi much easier to use. And while an example of the solution given in #68 is provided in https://github.com/google/jsonapi/blob/master/request_test.go#L694, that example does not show how to demarshal a structure that uses jsonapi structure tags.