Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dcoxall/jsonapi
Go library for JSONAPI serialization and deserialization
https://github.com/dcoxall/jsonapi
deserialization go golang json-api jsonapi serialization
Last synced: 13 days ago
JSON representation
Go library for JSONAPI serialization and deserialization
- Host: GitHub
- URL: https://github.com/dcoxall/jsonapi
- Owner: dcoxall
- License: mit
- Created: 2017-10-19T16:23:15.000Z (over 7 years ago)
- Default Branch: dev
- Last Pushed: 2017-10-23T19:01:52.000Z (over 7 years ago)
- Last Synced: 2024-11-14T23:13:38.410Z (2 months ago)
- Topics: deserialization, go, golang, json-api, jsonapi, serialization
- Language: Go
- Size: 29.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
JSON:API
========[![Build Status](https://travis-ci.org/dcoxall/jsonapi.svg?branch=dev)](https://travis-ci.org/dcoxall/jsonapi)
A Go JSON:API serialization and deserialization library.
Overview
--------This JSON API library was built to support circular references when serializing
and deserializing Go structures. Due to the dual phase approach to avoid stack
overflow errors then it is unlikely to be the most performant.Usage
-----```go
import (
"bytes"
"fmt"
"github.com/dcoxall/jsonapi"
)type Author struct {
ID string `jsonapi:"primary,author"`
FirstName string `jsonapi:"attr,first_name"`
LastName string `jsonapi:"attr,last_name"`
}type Book struct {
ID string `jsonapi:"primary,book"`
Name string `jsonapi:"attr,name"`
Author *Author `jsonapi:"relation,author"`
}func ExampleEncode() {
book := &Book{
ID: "book-123",
Name: "JSON Book",
Author: &Author{
ID: "author-123",
FirstName: "Jane",
LastName: "Doe",
},
}
buffer := new(bytes.Buffer)
encoder := jsonapi.NewEncoder(buffer)
if err := encoder.Encode(book); err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(buffer.String())
}
```The above would produce...
```json
{
"data": {
"id": "book-123",
"type": "book",
"attributes": {
"name": "JSON Book"
},
"relationships": {
"author": {
"data": {
"id": "author-123",
"type": "author"
}
}
}
},
"included": [
{
"id": "author-123",
"type": "author",
"attributes": {
"first_name": "Jane",
"last_name": "Doe"
}
}
]
}
```