jsonapi

package
v0.0.0-...-edb82c2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2015 License: MIT Imports: 8 Imported by: 0

README

api2go JSONAPI package

This package contains JSON API compatible marshal und unmarshal functionality.

  go get github.com/univedo/api2go/jsonapi

Usage

For information on how to use this package, please refer to the documentation on the api2go main project, the integration_test.go or the godoc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dejsonify

func Dejsonify(s string) string

Dejsonify returns a go struct key name from a JSON key name

func Jsonify

func Jsonify(s string) string

Jsonify returns a JSON formatted key name from a go struct field name

func Marshal

func Marshal(data interface{}) (map[string]interface{}, error)

Marshal thats the input from `data` which can be a struct, a slice, or a pointer of it. Any struct in `data`or data itself, must at least implement the `MarshalIdentifier` interface. If so, it will generate a map[string]interface{} matching the jsonapi specification.

func MarshalToJSON

func MarshalToJSON(val interface{}) ([]byte, error)

MarshalToJSON marshals a struct to json it works like `Marshal` but returns json instead

func MarshalToJSONWithURLs

func MarshalToJSONWithURLs(val interface{}, information ServerInformation) ([]byte, error)

MarshalToJSONWithURLs marshals a struct to json with URLs in `links`

func MarshalWithURLs

func MarshalWithURLs(data interface{}, information ServerInformation) (map[string]interface{}, error)

MarshalWithURLs can be used to include the generation of `related` and `self` links

func Pluralize

func Pluralize(word string) string

Pluralize a noun

func Singularize

func Singularize(word string) string

Singularize a noun

func Unmarshal

func Unmarshal(input map[string]interface{}, target interface{}) error

Unmarshal reads a JSONAPI map to a model struct target must at least implement the `UnmarshalIdentifier` interface.

func UnmarshalFromJSON

func UnmarshalFromJSON(data []byte, target interface{}) error

UnmarshalFromJSON reads a JSONAPI compatible JSON document to a model struct

func UnmarshalInto

func UnmarshalInto(input map[string]interface{}, targetStructType reflect.Type, targetSliceVal *reflect.Value) error

UnmarshalInto reads input params for one struct from `input` and marshals it into `targetSliceVal`

Types

type MarshalIdentifier

type MarshalIdentifier interface {
	GetID() string
}

MarshalIdentifier interface is necessary to give an element a unique ID. This interface must be implemented for marshal and unmarshal in order to let them store elements

type MarshalIncludedRelations

type MarshalIncludedRelations interface {
	MarshalReferences
	MarshalIdentifier
	GetReferencedStructs() []MarshalIdentifier
}

MarshalIncludedRelations must be implemented if referenced structs should be included

type MarshalLinkedRelations

type MarshalLinkedRelations interface {
	MarshalReferences
	MarshalIdentifier
	GetReferencedIDs() []ReferenceID
}

MarshalLinkedRelations must be implemented if there are references and the reference IDs should be included

type MarshalReferences

type MarshalReferences interface {
	GetReferences() []Reference
}

MarshalReferences must be implemented if the struct to be serialized has relations. This must be done because jsonapi needs information about relations even if many to many relations or many to one relations are empty

type Reference

type Reference struct {
	Type string
	Name string
}

Reference information about possible references of a struct

type ReferenceID

type ReferenceID struct {
	ID   string
	Type string
	Name string
}

ReferenceID contains all necessary information in order to reference another struct in jsonapi

type ServerInformation

type ServerInformation interface {
	GetBaseURL() string
	GetPrefix() string
}

ServerInformation can be passed to MarshalWithURLs to generate the `self` and `related` urls inside `links`

type UnmarshalIdentifier

type UnmarshalIdentifier interface {
	SetID(string) error
}

UnmarshalIdentifier interface to set ID when unmarshalling

type UnmarshalLinkedRelations

type UnmarshalLinkedRelations interface {
	SetReferencedIDs([]ReferenceID) error
}

UnmarshalLinkedRelations same as MarshalLinkedRelations for unmarshaling

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL