2015-07-08 19:48:13 -04:00
# jsonapi
2016-07-22 21:31:33 -04:00
[![Build Status ](https://travis-ci.org/google/jsonapi.svg?branch=master )](https://travis-ci.org/google/jsonapi)
2015-07-13 14:34:03 -04:00
2015-07-08 19:48:13 -04:00
A serailizer/deserializer for json payloads that comply to the
2015-08-18 17:22:19 -04:00
[JSON API - jsonapi.org ](http://jsonapi.org ) spec in go.
2015-07-08 19:48:13 -04:00
2016-07-22 21:31:33 -04:00
Also visit, [Godoc ](http://godoc.org/github.com/google/jsonapi ).
2015-07-13 17:02:26 -04:00
2015-07-19 13:32:22 -04:00
## Installation
```
2016-07-22 21:31:33 -04:00
go get -u github.com/google/jsonapi
2015-07-19 13:32:22 -04:00
```
2015-07-19 13:55:51 -04:00
Or, see [Alternative Installation ](#alternative-installation ).
2015-07-08 19:48:13 -04:00
## Background
You are working in your Go web application and you have a struct that is
2015-08-18 17:22:19 -04:00
organized similarly to your database schema. You need to send and
receive json payloads that adhere to the JSON API spec. Once you realize that
2015-07-19 13:32:22 -04:00
your json needed to take on this special form, you go down the path of
2015-08-18 17:22:19 -04:00
creating more structs to be able to serialize and deserialize JSON API
payloads. Then there are more models required with this additional
structure. Ugh! With JSON API, you can keep your model structs as is and
2015-07-19 13:32:22 -04:00
use [StructTags ](http://golang.org/pkg/reflect/#StructTag ) to indicate
2015-08-18 17:22:19 -04:00
to JSON API how you want your response built or your request
deserialized. What about your relationships? JSON API supports
2015-07-19 13:32:22 -04:00
relationships out of the box and will even put them in your response
into an `included` side-loaded slice--that contains associated records.
2015-07-08 19:48:13 -04:00
2015-07-10 16:50:51 -04:00
## Introduction
2015-08-18 17:22:19 -04:00
JSON API uses [StructField ](http://golang.org/pkg/reflect/#StructField )
2015-07-19 13:32:22 -04:00
tags to annotate the structs fields that you already have and use in
2015-08-18 17:22:19 -04:00
your app and then reads and writes [JSON API ](http://jsonapi.org )
output based on the instructions you give the library in your JSON API
2015-07-19 13:32:22 -04:00
tags. Let's take an example. In your app, you most likely have structs
2015-08-18 17:22:19 -04:00
that look similar to these:
2015-07-10 16:50:51 -04:00
```go
type Blog struct {
2016-07-22 21:31:33 -04:00
ID int `json:"id"`
2015-07-10 16:50:51 -04:00
Title string `json:"title"`
Posts []*Post `json:"posts"`
CurrentPost *Post `json:"current_post"`
CurrentPostId int `json:"current_post_id"`
CreatedAt time.Time `json:"created_at"`
ViewCount int `json:"view_count"`
}
type Post struct {
2016-07-22 21:31:33 -04:00
ID int `json:"id"`
BlogID int `json:"blog_id"`
2015-07-10 16:50:51 -04:00
Title string `json:"title"`
Body string `json:"body"`
Comments []*Comment `json:"comments"`
}
type Comment struct {
Id int `json:"id"`
2016-07-22 21:31:33 -04:00
PostID int `json:"post_id"`
2015-07-10 16:50:51 -04:00
Body string `json:"body"`
2016-07-16 11:24:21 -04:00
Likes uint `json:"likes_count,omitempty"`
2015-07-10 16:50:51 -04:00
}
```
These structs may or may not resemble the layout of your database. But
these are the ones that you want to use right? You wouldn't want to use
2015-08-18 17:22:19 -04:00
structs like those that JSON API sends because it is difficult to get at
2015-07-19 13:32:22 -04:00
all of your data easily.
## Example App
2016-07-22 21:31:33 -04:00
[examples/app.go ](https://github.com/google/jsonapi/blob/master/examples/app.go )
2015-07-19 13:32:22 -04:00
This runnable file demonstrates the implementation of a create, a show,
and a list [http.Handler ](http://golang.org/pkg/net/http#Handler ). It
2015-08-18 17:22:19 -04:00
outputs some example requests and responses as well as serialized
2015-07-19 13:32:22 -04:00
examples of the source/target structs to json. That is to say, I show
2015-08-18 17:22:19 -04:00
you that the library has successfully taken your JSON API request and
2015-07-19 13:32:22 -04:00
turned it into your struct types.
2015-07-10 16:50:51 -04:00
2015-07-19 13:32:22 -04:00
To run,
2015-07-10 16:50:51 -04:00
2015-07-19 13:32:22 -04:00
* Make sure you have go installed
* Create the following directories or similar: `~/go`
* `cd` there
* Set `GOPATH` to `PWD` in your shell session, `export GOPATH=$PWD`
2016-07-22 21:31:33 -04:00
* `go get github.com/google/jsonapi` . (Append `-u` after `get` if you
2015-07-19 13:32:22 -04:00
are updating.)
2016-07-22 21:31:33 -04:00
* `go run src/github.com/google/jsonapi/examples/app.go` or `cd
src/github.com/google/jsonapi/examples & & go run app.go`
2015-07-19 13:32:22 -04:00
## `jsonapi` Tag Reference
2015-07-13 14:59:11 -04:00
### Example
2015-07-10 16:50:51 -04:00
2015-07-19 13:32:22 -04:00
The `jsonapi` [StructTags ](http://golang.org/pkg/reflect/#StructTag )
2015-08-18 17:22:19 -04:00
tells this library how to marshal and unmarshal your structs into
JSON API payloads and your JSON API payloads to structs, respectively.
Then Use JSON API's Marshal and Unmarshal methods to construct and read
2015-07-19 13:32:22 -04:00
your responses and replies. Here's an example of the structs above
2015-08-18 17:22:19 -04:00
using JSON API tags:
2015-07-10 16:50:51 -04:00
```go
type Blog struct {
2016-07-22 21:31:33 -04:00
ID int `jsonapi:"primary,blogs"`
2015-07-10 16:50:51 -04:00
Title string `jsonapi:"attr,title"`
Posts []*Post `jsonapi:"relation,posts"`
CurrentPost *Post `jsonapi:"relation,current_post"`
2016-07-22 21:31:33 -04:00
CurrentPostID int `jsonapi:"attr,current_post_id"`
2015-07-10 16:50:51 -04:00
CreatedAt time.Time `jsonapi:"attr,created_at"`
ViewCount int `jsonapi:"attr,view_count"`
}
type Post struct {
2016-07-22 21:31:33 -04:00
ID int `jsonapi:"primary,posts"`
BlogID int `jsonapi:"attr,blog_id"`
2015-07-10 16:50:51 -04:00
Title string `jsonapi:"attr,title"`
Body string `jsonapi:"attr,body"`
Comments []*Comment `jsonapi:"relation,comments"`
}
type Comment struct {
2016-07-22 21:31:33 -04:00
ID int `jsonapi:"primary,comments"`
PostID int `jsonapi:"attr,post_id"`
2015-07-10 16:50:51 -04:00
Body string `jsonapi:"attr,body"`
2016-07-16 11:24:21 -04:00
Likes uint `jsonapi:"attr,likes-count,omitempty"`
2015-07-10 16:50:51 -04:00
}
```
2015-07-19 13:32:22 -04:00
### Permitted Tag Values
2015-07-13 14:59:11 -04:00
#### `primary`
```
`jsonapi:"primary,<type field output>"`
```
2015-08-18 17:22:19 -04:00
This indicates this is the primary key field for this struct type.
2015-07-19 13:32:22 -04:00
Tag value arguments are comma separated. The first argument must be,
`primary` , and the second must be the name that should appear in the
`type` \* field for all data objects that represent this type of model.
2015-08-18 17:22:19 -04:00
\* According the [JSON API ](http://jsonapi.org ) spec, the plural record
2015-07-19 13:32:22 -04:00
types are shown in the examples, but not required.
2015-07-13 14:59:11 -04:00
#### `attr`
```
2016-07-16 11:24:21 -04:00
`jsonapi:"attr,<key name in attributes hash>,<optional: omitempty>"`
2015-07-13 14:59:11 -04:00
```
2015-07-19 13:32:22 -04:00
These fields' values will end up in the `attributes` hash for a record.
The first argument must be, `attr` , and the second should be the name
2016-07-16 11:24:21 -04:00
for the key to display in the `attributes` hash for that record. The optional
third argument is `omitempty` - if it is present the field will not be present
in the `"attributes"` if the field's value is equivalent to the field types
empty value (ie if the `count` field is of type `int` , `omitempty` will omit the
field when `count` has a value of `0` ). Lastly, the spec indicates that
`attributes` key names should be dasherized for multiple word field names.
2015-07-13 14:59:11 -04:00
#### `relation`
```
2017-01-20 19:13:04 -05:00
`jsonapi:"relation,<key name in relationships hash>,<optional: omitempty>"`
2015-07-13 14:59:11 -04:00
```
2015-07-19 13:32:22 -04:00
Relations are struct fields that represent a one-to-one or one-to-many
2015-08-18 17:22:19 -04:00
relationship with other structs. JSON API will traverse the graph of
relationships and marshal or unmarshal records. The first argument must
2015-07-19 13:32:22 -04:00
be, `relation` , and the second should be the name of the relationship,
2017-01-20 19:13:04 -05:00
used as the key in the `relationships` hash for the record. The optional
third argument is `omitempty` - if present will prevent non existent to-one and
to-many from being serialized.
2015-07-13 14:59:11 -04:00
## Methods Reference
2015-07-10 16:50:51 -04:00
2015-07-13 14:23:03 -04:00
**All `Marshal` and `Unmarshal` methods expect pointers to struct
instance or slices of the same contained with the `interface{}` s**
2015-07-13 13:44:47 -04:00
2015-07-19 13:32:22 -04:00
Now you have your structs prepared to be seralized or materialized, What
about the rest?
2015-07-10 16:50:51 -04:00
2015-07-13 14:59:11 -04:00
### Create Record Example
2015-07-10 16:50:51 -04:00
2015-08-18 17:22:19 -04:00
You can Unmarshal a JSON API payload using
2016-07-22 21:31:33 -04:00
[jsonapi.UnmarshalPayload ](http://godoc.org/github.com/google/jsonapi#UnmarshalPayload ).
2015-07-19 13:32:22 -04:00
It reads from an [io.Reader ](https://golang.org/pkg/io/#Reader )
2015-08-18 17:22:19 -04:00
containing a JSON API payload for one record (but can have related
2015-07-19 13:32:22 -04:00
records). Then, it materializes a struct that you created and passed in
(using new or & ). Again, the method supports single records only, at
the top level, in request payloads at the moment. Bulk creates and
updates are not supported yet.
After saving your record, you can use,
2016-07-22 21:31:33 -04:00
[MarshalOnePayload ](http://godoc.org/github.com/google/jsonapi#MarshalOnePayload ),
2015-08-18 17:22:19 -04:00
to write the JSON API response to an
2015-07-19 13:32:22 -04:00
[io.Writer ](https://golang.org/pkg/io/#Writer ).
2015-07-13 13:07:12 -04:00
#### `UnmarshalPayload`
```go
UnmarshalPayload(in io.Reader, model interface{})
```
2016-07-22 21:31:33 -04:00
Visit [godoc ](http://godoc.org/github.com/google/jsonapi#UnmarshalPayload )
2015-07-13 13:07:12 -04:00
#### `MarshalOnePayload`
```go
2015-07-13 13:44:47 -04:00
MarshalOnePayload(w io.Writer, model interface{}) error
2015-07-13 13:07:12 -04:00
```
2016-07-22 21:31:33 -04:00
Visit [godoc ](http://godoc.org/github.com/google/jsonapi#MarshalOnePayload )
2015-07-13 13:07:12 -04:00
2015-08-18 17:22:19 -04:00
Writes a JSON API response, with related records sideloaded, into an
2015-07-19 13:32:22 -04:00
`included` array. This method encodes a response for a single record
only. If you want to serialize many records, see,
[MarshalManyPayload ](#marshalmanypayload ).
2015-07-13 13:44:47 -04:00
2017-01-26 18:25:13 -05:00
##### Handler Example Code
2015-07-13 13:07:12 -04:00
2015-07-10 16:50:51 -04:00
```go
func CreateBlog(w http.ResponseWriter, r *http.Request) {
blog := new(Blog)
if err := jsonapi.UnmarshalPayload(r.Body, blog); err != nil {
2017-01-20 21:06:44 -05:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2015-07-10 16:50:51 -04:00
return
}
2015-07-19 13:32:22 -04:00
// ...save your blog...
2015-07-10 16:50:51 -04:00
2017-01-20 21:06:44 -05:00
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", jsonapi.MediaType)
2015-07-10 16:50:51 -04:00
if err := jsonapi.MarshalOnePayload(w, blog); err != nil {
2017-01-20 21:06:44 -05:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2015-07-10 16:50:51 -04:00
}
}
```
2015-07-08 19:48:13 -04:00
2015-07-13 14:59:11 -04:00
### List Records Example
2015-07-13 13:44:47 -04:00
2015-07-13 13:07:12 -04:00
#### `MarshalManyPayload`
2015-07-13 13:10:11 -04:00
```go
MarshalManyPayload(w io.Writer, models []interface{}) error
```
2016-07-22 21:31:33 -04:00
Visit [godoc ](http://godoc.org/github.com/google/jsonapi#MarshalManyPayload )
2015-07-13 13:10:11 -04:00
2015-07-13 13:44:47 -04:00
Takes an `io.Writer` and an slice of `interface{}` . Note, if you have a
type safe array of your structs, like,
```go
var blogs []*Blog
```
2015-07-19 13:32:22 -04:00
you will need to iterate over the slice of `Blog` pointers and append
2015-07-13 13:44:47 -04:00
them to an interface array, like,
```go
blogInterface := make([]interface{}, len(blogs))
for i, blog := range blogs {
blogInterface[i] = blog
}
```
Alternatively, you can insert your `Blog` s into a slice of `interface{}`
the first time. For example when you fetch the `Blog` s from the db
2015-07-19 13:32:22 -04:00
`append` them to an `[]interface{}` rather than a `[]*Blog` . So your
method signature to reach into your data store may look something like
this,
2015-07-13 13:44:47 -04:00
```go
2015-07-19 13:32:22 -04:00
func FetchBlogs() ([]interface{}, error)
2015-07-13 13:44:47 -04:00
```
2017-01-26 18:25:13 -05:00
##### Handler Example Code
2015-07-13 13:10:11 -04:00
```go
func ListBlogs(w http.ResponseWriter, r *http.Request) {
2015-07-19 13:32:22 -04:00
// ...fetch your blogs, filter, offset, limit, etc...
2015-07-13 13:10:11 -04:00
2015-07-19 13:32:22 -04:00
// but, for now
2015-07-13 13:10:11 -04:00
blogs := testBlogsForList()
2017-01-20 21:06:44 -05:00
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", jsonapi.MediaType)
2015-07-13 13:10:11 -04:00
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
2017-01-20 21:06:44 -05:00
http.Error(w, err.Error(), http.StatusInternalServerError)
2015-07-13 13:10:11 -04:00
}
}
```
2015-07-13 14:23:03 -04:00
2017-01-26 18:25:13 -05:00
### Create Records Example
#### `UnmarshalManyPayload`
```go
UnmarshalManyPayload(in io.Reader, t reflect.Type) ([]interface{}, error)
```
Visit [godoc ](http://godoc.org/github.com/google/jsonapi#UnmarshalManyPayload )
Takes an `io.Reader` and a `reflect.Type` representing the uniform type
contained within the `"data"` JSON API member.
##### Handler Example Code
```go
func CreateBlogs(w http.ResponseWriter, r *http.Request) {
// ...create many blogs at once
blogs, err := UnmarshalManyPayload(r.Body, reflect.TypeOf(new(Blog)))
if err != nil {
t.Fatal(err)
}
for _, blog := range blogs {
b, ok := blog.(*Blog)
// ...save each of your blogs
}
w.WriteHeader(http.StatusCreated)
w.Header().Set("Content-Type", jsonapi.MediaType)
if err := jsonapi.MarshalManyPayload(w, blogs); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
```
2017-01-20 20:55:59 -05:00
### Links
If you need to include [link objects ](http://jsonapi.org/format/#document-links ) along with response data, implement the `Linkable` interface for document-links, and `RelationshipLinkable` for relationship links:
```go
func (post Post) JSONAPILinks() *map[string]interface{} {
return & map[string]interface{}{
"self": "href": fmt.Sprintf("https://example.com/posts/%d", post.ID),
}
}
// Invoked for each relationship defined on the Post struct when marshaled
func (post Post) JSONAPIRelationshipLinks(relation string) *map[string]interface{} {
if relation == "comments" {
return & map[string]interface{}{
"related": fmt.Sprintf("https://example.com/posts/%d/comments", post.ID),
}
}
return nil
}
```
2015-07-13 14:23:03 -04:00
## Testing
### `MarshalOnePayloadEmbedded`
```go
MarshalOnePayloadEmbedded(w io.Writer, model interface{}) error
```
2016-07-22 21:31:33 -04:00
Visit [godoc ](http://godoc.org/github.com/google/jsonapi#MarshalOnePayloadEmbedded )
2015-07-13 14:23:03 -04:00
2015-07-19 13:32:22 -04:00
This method is not strictly meant to for use in implementation code,
2015-08-18 17:22:19 -04:00
although feel free. It was mainly created for use in tests; in most cases,
2015-07-19 13:32:22 -04:00
your request payloads for create will be embedded rather than sideloaded
for related records. This method will serialize a single struct pointer
into an embedded json response. In other words, there will be no,
`included` , array in the json; all relationships will be serialized
inline with the data.
2015-07-13 14:23:03 -04:00
2015-07-19 13:32:22 -04:00
However, in tests, you may want to construct payloads to post to create
methods that are embedded to most closely model the payloads that will
be produced by the client. This method aims to enable that.
2015-07-13 14:23:03 -04:00
### Example
```go
out := bytes.NewBuffer(nil)
// testModel returns a pointer to a Blog
jsonapi.MarshalOnePayloadEmbedded(out, testModel())
h := new(BlogsHandler)
w := httptest.NewRecorder()
2017-01-20 21:06:44 -05:00
r, _ := http.NewRequest(http.MethodPost, "/blogs", out)
2015-07-13 14:23:03 -04:00
h.CreateBlog(w, r)
blog := new(Blog)
jsonapi.UnmarshalPayload(w.Body, blog)
// ... assert stuff about blog here ...
```
2015-07-19 13:55:51 -04:00
## Alternative Installation
I use git subtrees to manage dependencies rather than `go get` so that
the src is committed to my repo.
```
2016-07-22 21:31:33 -04:00
git subtree add --squash --prefix=src/github.com/google/jsonapi https://github.com/google/jsonapi.git master
2015-07-19 13:55:51 -04:00
```
To update,
```
2016-07-22 21:31:33 -04:00
git subtree pull --squash --prefix=src/github.com/google/jsonapi https://github.com/google/jsonapi.git master
2015-07-19 13:55:51 -04:00
```
This assumes that I have my repo structured with a `src` dir containing
a collection of packages and `GOPATH` is set to the root
folder--containing `src` .
2015-07-19 13:32:22 -04:00
2015-07-13 14:23:03 -04:00
## Contributing
Fork, Change, Pull Request *with tests* .