2015-07-13 14:59:11 -04:00
|
|
|
/*
|
2015-08-02 17:10:26 -04:00
|
|
|
Package jsonapi provides a serializer and deserializer for jsonapi.org spec payloads.
|
2015-07-13 14:59:11 -04:00
|
|
|
|
|
|
|
You can keep your model structs as is and use struct field tags to indicate to jsonapi
|
2021-02-17 16:33:12 -05:00
|
|
|
how you want your response built or your request deserialized. What about my relationships?
|
2015-07-13 14:59:11 -04:00
|
|
|
jsonapi supports relationships out of the box and will even side load them in your response
|
|
|
|
into an "included" array--that contains associated objects.
|
|
|
|
|
|
|
|
jsonapi uses StructField tags to annotate the structs fields that you already have and use
|
|
|
|
in your app and then reads and writes jsonapi.org output based on the instructions you give
|
|
|
|
the library in your jsonapi tags.
|
|
|
|
|
|
|
|
Example structs using a Blog > Post > Comment structure,
|
|
|
|
|
2015-07-13 18:04:21 -04:00
|
|
|
type Blog struct {
|
2016-07-05 21:32:15 -04:00
|
|
|
ID int `jsonapi:"primary,blogs"`
|
2015-07-13 18:04:21 -04:00
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Posts []*Post `jsonapi:"relation,posts"`
|
|
|
|
CurrentPost *Post `jsonapi:"relation,current_post"`
|
2016-07-05 21:32:15 -04:00
|
|
|
CurrentPostID int `jsonapi:"attr,current_post_id"`
|
2015-07-13 18:04:21 -04:00
|
|
|
CreatedAt time.Time `jsonapi:"attr,created_at"`
|
|
|
|
ViewCount int `jsonapi:"attr,view_count"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Post struct {
|
2016-07-05 21:32:15 -04:00
|
|
|
ID int `jsonapi:"primary,posts"`
|
|
|
|
BlogID int `jsonapi:"attr,blog_id"`
|
2015-07-13 18:04:21 -04:00
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Body string `jsonapi:"attr,body"`
|
|
|
|
Comments []*Comment `jsonapi:"relation,comments"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Comment struct {
|
2016-07-05 21:32:15 -04:00
|
|
|
ID int `jsonapi:"primary,comments"`
|
|
|
|
PostID int `jsonapi:"attr,post_id"`
|
2015-07-13 18:04:21 -04:00
|
|
|
Body string `jsonapi:"attr,body"`
|
|
|
|
}
|
2015-07-13 14:59:11 -04:00
|
|
|
|
|
|
|
jsonapi Tag Reference
|
|
|
|
|
|
|
|
Value, primary: "primary,<type field output>"
|
|
|
|
|
|
|
|
This indicates that this is the primary key field for this struct type. 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.
|
|
|
|
|
2016-09-22 16:58:07 -04:00
|
|
|
Value, attr: "attr,<key name in attributes hash>[,<extra arguments>]"
|
2015-07-13 14:59:11 -04:00
|
|
|
|
|
|
|
These fields' values should end up in the "attribute" hash for a record. The first
|
|
|
|
argument must be, "attr', and the second should be the name for the key to display in
|
2018-10-05 15:52:00 -04:00
|
|
|
the "attributes" hash for that record.
|
2015-07-13 14:59:11 -04:00
|
|
|
|
2016-09-22 16:58:07 -04:00
|
|
|
The following extra arguments are also supported:
|
|
|
|
|
|
|
|
"omitempty": excludes the fields value from the "attribute" hash.
|
|
|
|
"iso8601": uses the ISO8601 timestamp format when serialising or deserialising the time.Time value.
|
|
|
|
|
2015-07-13 14:59:11 -04:00
|
|
|
Value, relation: "relation,<key name in relationships hash>"
|
|
|
|
|
|
|
|
Relations are struct fields that represent a one-to-one or one-to-many to other structs.
|
|
|
|
jsonapi will traverse the graph of relationships and marshal or unmarshal records. The first
|
|
|
|
argument must be, "relation", and the second should be the name of the relationship, used as
|
|
|
|
the key in the "relationships" hash for the record.
|
|
|
|
|
|
|
|
Use the methods below to Marshal and Unmarshal jsonapi.org json payloads.
|
|
|
|
|
2016-07-22 21:31:33 -04:00
|
|
|
Visit the readme at https://github.com/google/jsonapi
|
2015-07-13 14:59:11 -04:00
|
|
|
*/
|
|
|
|
package jsonapi
|