jsonweb

package module
v0.0.0-...-baf40d5 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2018 License: Apache-2.0 Imports: 8 Imported by: 0

README

Work in progress...

Documentation

Overview

Example
package main

import (
	"encoding/json"
	"fmt"
	"os"

	"github.com/dolmen-go/jsonptr"
	"github.com/dolmen-go/jsonweb"
)

func main() {
	r, err := os.Open("testdata/wikipedia-0.web.json")
	if err != nil {
		panic(err)
	}
	dec := json.NewDecoder(r)
	var browser jsonweb.Browser
	if err = dec.Decode(&browser); err != nil {
		panic(err)
	}
	r.Close()

	r, err = os.Open("testdata/wikipedia-0.json")
	if err != nil {
		panic(err)
	}

	err = browser.Parse(
		`https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions|info&rvprop=user&rvlimit=10&format=json&formatversion=2`,
		json.NewDecoder(r),
		func(ptr jsonptr.Pointer, ctx *jsonweb.Context) error {
			values, _ := json.Marshal(ctx.Values())
			fmt.Printf("%s: %s\n", ptr, values)
			return nil
		},
	)
	if err != nil {
		panic(err)
	}

}
Output:
/query/pages/0: {"title":"Main Page"}
/query/pages/0/revisions/0: {"title":"Main Page","user":"The Blade of the Northern Lights"}
/query/pages/0/revisions/1: {"title":"Main Page","user":"Bearcat"}
/query/pages/0/revisions/2: {"title":"Main Page","user":"Bearcat"}
/query/pages/0/revisions/3: {"title":"Main Page","user":"Optimist on the run"}
/query/pages/0/revisions/4: {"title":"Main Page","user":"Ian.thomson"}
/query/pages/0/revisions/5: {"title":"Main Page","user":"Ian.thomson"}
/query/pages/0/revisions/6: {"title":"Main Page","user":"Alex Shih"}
/query/pages/0/revisions/7: {"title":"Main Page","user":"TheDJ"}
/query/pages/0/revisions/8: {"title":"Main Page","user":"TheDJ"}
/query/pages/0/revisions/9: {"title":"Main Page","user":"Doc James"}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Browser

type Browser struct {
	// contains filtered or unexported fields
}

func NewBrowser

func NewBrowser(m Map) (*Browser, error)

func (*Browser) MarshalJSON

func (browser *Browser) MarshalJSON() ([]byte, error)

func (*Browser) Parse

func (browser *Browser) Parse(url string, doc interface{}, visit ContextVisitor) error

func (*Browser) Roots

func (browser *Browser) Roots() []string

func (*Browser) UnmarshalJSON

func (browser *Browser) UnmarshalJSON(b []byte) error

func (*Browser) WithVars

func (browser *Browser) WithVars(names []string) []*uritemplates.UriTemplate

type BuildError

type BuildError struct {
	Ptr string
	Err error
}

func (*BuildError) Error

func (e *BuildError) Error() string

type Context

type Context struct {
	// contains filtered or unexported fields
}

func (*Context) Parent

func (ctx *Context) Parent() *Context

Parent returns the parent context.

The root context is nil.

func (*Context) Pointer

func (ctx *Context) Pointer() jsonptr.Pointer

func (*Context) Values

func (ctx *Context) Values() map[string]interface{}

type ContextVisitor

type ContextVisitor func(ptr jsonptr.Pointer, ctx *Context) error

ContextVisitor is a callback that visits all contexts of a JSON-style document. The pointer is reused between calls, so it must be .Copy()ed if you need to keep it beyond the duraction of the visit call.

type ContextVisitorError

type ContextVisitorError struct {
	Err error
}

func (ContextVisitorError) Error

func (e ContextVisitorError) Error() string

type Extractor

type Extractor struct {
	// contains filtered or unexported fields
}

Extractor allows to extract hierarchical points of interest in a JSON document.

func (*Extractor) UnmarshalJSON

func (ex *Extractor) UnmarshalJSON(b []byte) error

type ExtractorError

type ExtractorError struct {
	Ptr string
	Err error
}

func (*ExtractorError) Error

func (e *ExtractorError) Error() string

type Map

type Map map[string]*Extractor

Map stores a group of Extractor by names.

func (Map) Parse

func (m Map) Parse(key string, doc interface{}, visit ContextVisitor) error

func (*Map) UnmarshalJSON

func (m *Map) UnmarshalJSON(b []byte) error

Jump to

Keyboard shortcuts

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