jsonld

package module
v0.0.0-...-401cba7 Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2017 License: MIT Imports: 8 Imported by: 0

README

go-jsonld

GoDoc Build Status

A Go library for JSON-LD.

Usage

type Person struct {
	ID string `jsonld:"@id"`
	Name string `jsonld:"http://schema.org/name"`
}

const jsonString = `{
  "@id": "http://me.markus-lanthaler.com/",
  "http://schema.org/name": "Manu Sporny"
}`

var p Person
if err := jsonld.Unmarshal([]byte(jsonString), &p); err != nil {
	log.Fatal(err)
}

log.Printf(&p)

License

MIT

Documentation

Overview

Package jsonld implements JSON-LD, as defined in https://www.w3.org/TR/json-ld/.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) ([]byte, error)

Marshal returns the JSON-LD encoding of v.

Marshal uses the same rules as the encoding/json package, except for Resource values.

func MarshalWithContext

func MarshalWithContext(v interface{}, ctx *Context) ([]byte, error)

MarshalWithContext returns the JSON-LD encoding of v with the context ctx.

func Unmarshal

func Unmarshal(b []byte, v interface{}) error

Unmarshal parses the JSON-LD-encoded data and stores the result in the value pointed to by v.

Unmarshal uses the inverse of the encodings that Marshal uses, allocating maps, slices, and pointers as necessary, with the following additional rules:

To unmarshal JSON-LD into a struct:

  • If the struct has a field named JSONLDType of type Type, Unmarshal records the resource type in that field.
  • If the JSONLDType field has an associated tag of the form "type-URI", the resource must have the given type or else Unmarshal returns an error.
  • If the struct has a field whose tag is "@id", Unmarshal records the resource URI in that field.
  • If the resource has a property whose URI matches a tag formatted as "property-URI", the property value is recorded in that field.

To unmarshal JSON-LD into an interface value, Unmarshal uses the same rules as the encoding/json package, except for resources which are stored as *Resource.

func UnmarshalWithContext

func UnmarshalWithContext(b []byte, v interface{}, ctx *Context) error

UnmarshalWithContext parses the JSON-LD-encoded data with the context ctx and stores the result in the value pointed to by v.

Types

type Context

type Context struct {
	URL   string
	Lang  string
	Base  string // Base URI to resolve relative URIs.
	Vocab string // Base vocabulary.
	Terms map[string]*Resource
}

func FetchContext

func FetchContext(url string) (*Context, error)

FetchContext fetches remote contexts with http.DefaultClient.

type Decoder

type Decoder struct {
	// Context, if non-nil, will be used when decoding values.
	Context *Context
	// FetchContext, if non-nil, will be called to fetch remote contexts. By
	// default, remote contexts are not fetched.
	FetchContext FetchContextFunc
	// contains filtered or unexported fields
}

Decoder decodes JSON-LD values.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder creates a new JSON-LD decoder.

func (*Decoder) Decode

func (d *Decoder) Decode(v interface{}) error

Decode decodes a JSON-LD value.

type Encoder

type Encoder struct {
	// If specified, this context will be used when encoding values.
	Context *Context
	// contains filtered or unexported fields
}

Encoder encodes JSON-LD values.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder creates a new JSON-LD encoder.

func (*Encoder) Encode

func (e *Encoder) Encode(v interface{}) error

Encode encodes a JSON-LD value.

type FetchContextFunc

type FetchContextFunc func(url string) (*Context, error)

FetchContextFunc fetches remote contexts.

type Props

type Props map[string][]interface{}

func (Props) Add

func (p Props) Add(k string, v interface{})

func (Props) Get

func (p Props) Get(k string) interface{}

func (Props) Set

func (p Props) Set(k string, v interface{})

func (Props) Type

func (p Props) Type() string

type Resource

type Resource struct {
	ID    string
	Props Props
}

type Type

type Type struct {
	URI string
}

Jump to

Keyboard shortcuts

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