Package jsonapi converts Go data to and from the JSON API format.
go get -u github.com/smotes/jsonapi
The JSON API specification has strict requirements on the structure of any JSON request/response containing data, a structure most likely varying drastically from any backend data structures. This package solely aims to provide utilities around converting said backend data to the specification's while avoiding reflection and assuming nothing about the JSON encoding/decoding package used.
Implement multiple (mostly optional) interfaces to convert your data to and from JSON API documents and resources.
type Person struct {
ID int
Name string
Age int
}
func (p *Person) GetID() (string, error) {
return strconv.Itoa(p.ID), nil
}
func (p *Person) GetType() (string, error) {
return "people", nil
}
This package converts your data to and from a common Resource struct, which is compatible with any third-party JSON package with the "encoding/json" API, or can work with byte slices.
var person = &Person{ID: 1, Name: "John", Age: 42}
resource, err := jsonapi.ToResource(&person, false)
handle(err)
b, err := json.Marshal(&resource)
handle(err)
fmt.Println(string(b))
// {"id": "1", "type": "people"}
Tested, detailed examples are included on the ToResource and FromResource functions in the godocs.
- Fork the repository.
- Code your changes.
- If applicable, write tests and documentation for the new functionality (please ensure all tests pass, have 100% coverage and pass go vet and golint).
- Raise a new pull request with a short description.