Skip to content

jsignanini/jsonapi-serializer-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

66 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsonapi-serializer-go

GoDoc Build Status Go Report Card Coverage Status

Installation

Install jsonapi-serializer-go with:

go get -u github.com/jsignanini/jsonapi-serializer-go

Then, import it using:

import "github.com/jsignanini/jsonapi-serializer-go"

Usage

package main

import (
	"fmt"

	"github.com/jsignanini/jsonapi-serializer-go"
)

func main() {
	// sample data
	type (
		BookBinding string
		BookSubject string
		Author      struct {
			ID        string `jsonapi:"primary,authors"`
			FirstName string `jsonapi:"attribute,first_name"`
			LastName  string `jsonapi:"attribute,last_name"`
		}
		Book struct {
			ISBN            string        `jsonapi:"primary,books"`
			Bindings        []BookBinding `jsonapi:"attribute,bindings"`
			PublicationYear int           `jsonapi:"attribute,publication_date"`
			Subject         BookSubject   `jsonapi:"attribute,subject"`
			Title           string        `jsonapi:"attribute,title"`
			Author          *Author       `jsonapi:"attribute,author"`
		}
	)
	const (
		BookBindingHardcover BookBinding = "Hardcover"
		BookBindingPaperback BookBinding = "Paperback"
	)
	cosmos := Book{
		ISBN:            "0-394-50294-9",
		Bindings:        []BookBinding{BookBindingHardcover, BookBindingPaperback},
		PublicationYear: 1980,
		Subject:         "Cosmology",
		Title:           "Cosmos",
		Author: &Author{
			ID:        "c3a6ddb6-7e5e-4264-bd03-ef6e41d76365",
			FirstName: "Carl",
			LastName:  "Sagan",
		},
	}

	// marshal
	jsonBytes, err := jsonapi.Marshal(&cosmos, nil)
	if err != nil {
		panic(err)
	}

	// print output
	fmt.Println(string(jsonBytes))
}

Outputs:

{
	"data": {
		"id": "0-394-50294-9",
		"type": "books",
		"attributes": {
			"author": {
				"ID": "c3a6ddb6-7e5e-4264-bd03-ef6e41d76365",
				"FirstName": "Carl",
				"LastName": "Sagan"
			},
			"bindings": [
				"Hardcover",
				"Paperback"
			],
			"publication_date": 1980,
			"subject": "Cosmology",
			"title": "Cosmos"
		}
	},
	"jsonapi": {
		"version": "1.0"
	}
}

TODOs

  • Optionally validate jsonapi spec
  • Optionally set jsonapi settings (e.g.: spec version, error/warning on document validation, etc.)
  • Support omitempty tag jsonapi:"attribute,name,omitempty"
  • Standardize internal errors
  • Show error or warning when parsing an unsupported builtin type (e.g.: complex128)
  • Handle top-level Links and resource-level links separatedly

About

JSON:API serializer and deserializer.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages