Skip to content

Repository files navigation

jsonapi

GoDocBuild Statuscodecov

Package jsonapi converts Go data to and from the JSON API format.

go get -u github.com/smotes/jsonapi

Overview

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.

Avoid reflection

Implement multiple (mostly optional) interfaces to convert your data to and from JSON API documents and resources.

typePersonstruct {
IDintNamestringAgeint
}
func (p*Person) GetID() (string, error) {
returnstrconv.Itoa(p.ID), nil
}
func (p*Person) GetType() (string, error) {
return"people", nil
}

Use any JSON package

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.

varperson=&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 examples

Tested, detailed examples are included on the ToResource and FromResource functions in the godocs.

Contributing

  • 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.

About

Package jsonapi provides utilities for converting Go data to/from the JSON API format detailed at http://jsonapi.org

Resources

Stars

36 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages