json

package module
v0.0.0-...-899f80d Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2014 License: BSD-2-Clause Imports: 10 Imported by: 0

README

NOTE

This was forked from github.com/garyburd/json to provide an old view of the world. For some reason, a push -f on the original repo rewrote history and removed this particular version. This has been recreated from cached copies of the code since the API changed significantly after the push.

Original README

This project is a work in progress.

Documentation

Documentation

Overview

Package json is a JSON parser.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Unmarshal

func Unmarshal(s *Scanner, v interface{}) error

Unmarshal deserializes data from the scanner to value v. In the case of struct values, only exported fields will be decoded. The lowercase field name is used as the key for each exported field, but this behavior may be changed using the respective field tag. The tag may also contain flags to tweak the decoding behavior for the field.

Types

type ArrayScanner

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

ArrayScanner is a helper for scanning arrays.

func (*ArrayScanner) Scan

func (as *ArrayScanner) Scan() bool

Scan advances to the next element at the current nesting level, possibly skipping over nested elements. Scan returns false at the End element for the current level or if an error is encountered.

type DecodeTypeError

type DecodeTypeError struct {
	Kind Kind         // description of JSON value
	Type reflect.Type // type of Go value it could not be assigned to
}

A DecodeTypeError describes a JSON value that was not appropriate for a value of a specific Go type.

func (*DecodeTypeError) Error

func (e *DecodeTypeError) Error() string

type Kind

type Kind int

Kind represents the kind a JSON document element.

const (
	// Null represents a JSON null element.
	Null Kind = iota

	// Bool represents a JSON bool element.
	Bool

	// String represents a JSON string element.
	String

	// Number represents a JSON number element.
	Number

	// Array represents the start of a JSON array element.
	Array

	// Object represents the start of a JSON object element.
	Object

	// End represents the end of an object or array element.
	End
)

func (Kind) String

func (k Kind) String() string

type ObjectScanner

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

ObjectScanner is a helper for scanning objects.

func (*ObjectScanner) Name

func (os *ObjectScanner) Name() string

Name returns the last member named scanned at the current nesting level.

func (*ObjectScanner) Scan

func (os *ObjectScanner) Scan() bool

Scan advances to the next element at the current nesting level, possibly skipping over nested elements. Scan returns false at the End element for the current level or if an error is encountered.

type Scanner

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

Scanner reads a JSON document from an io.Reader. Successive calls to the Scan method step through the elements of the document as follows:

element = Null | Bool | Number | String | object | array
array = Array element* End
object = Object element* End

Scanning stops unrecoverably at EOF, the first I/O error, or a syntax error. When a scan stops, the reader may have advanced arbitrarily far past the last token.

When scanning strings, invalid UTF-8 or invalid UTF-16 surrogate pairs are not treated as an error. Instead, they are replaced by the Unicode replacement character U+FFFD.

Example

This example shows how to decode a JSON value to a tree of maps and slices.

package main

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/garyburd/json"
)

const jsonText = `
[
    {
        "name": "redigo",
        "keywords": ["database", "redis"],
        "imports": 10
    },
    {
        "name": "mgo",
        "keywords": ["database", "mongodb"],
        "imports": 22
    }
]
`

var emptySlice = make([]interface{}, 0, 0)

func decodeValue(s *json.Scanner) (interface{}, error) {
	switch s.Kind() {
	case json.Number:
		return strconv.ParseFloat(string(s.Value()), 64)
	case json.String:
		return string(s.Value()), nil
	case json.Array:
		v := emptySlice
		as := s.ArrayScanner()
		for as.Scan() {
			subv, err := decodeValue(s)
			if err != nil {
				return v, err
			}
			v = append(v, subv)
		}
		return v, s.Err()
	case json.Object:
		v := make(map[string]interface{})
		os := s.ObjectScanner()
		for os.Scan() {
			subv, err := decodeValue(s)
			if err != nil {
				return v, err
			}
			v[os.Name()] = subv
		}
		return v, s.Err()
	case json.Bool:
		return s.BoolValue(), nil
	case json.Null:
		return nil, nil
	default:
		return nil, fmt.Errorf("unexpected %v", s.Kind())
	}
}

// This example shows how to decode a JSON value to a tree of maps and slices.
func main() {

	s := json.NewScanner(strings.NewReader(jsonText))

	if !s.Scan() {
		fmt.Printf("error %v\n", s.Err())
		return
	}

	v, err := decodeValue(s)
	if err != nil {
		fmt.Printf("error %v\n", err)
		return
	}

	s.Scan()
	if s.Err() != nil {
		fmt.Printf("error %v\n", s.Err())
		return
	}

	fmt.Println(v)

}
Output:

[map[name:redigo keywords:[database redis] imports:10] map[name:mgo keywords:[database mongodb] imports:22]]

func NewScanner

func NewScanner(rd io.Reader) *Scanner

NewScanner allocates and initializes a new scanner.

func (*Scanner) ArrayScanner

func (s *Scanner) ArrayScanner() ArrayScanner

func (*Scanner) BoolValue

func (s *Scanner) BoolValue() bool

BoolValue returns the value of the current boolean value.

func (*Scanner) Err

func (s *Scanner) Err() error

Err returns the first non-EOF error that was encountered by the Scanner.

func (*Scanner) Kind

func (s *Scanner) Kind() Kind

Kind returns the kind of the current value.

func (*Scanner) Name

func (s *Scanner) Name() []byte

Name returns the object member name of the current value. The underlying array may point to data that will be overwritten by a subsequent call to Scan.

func (*Scanner) ObjectScanner

func (s *Scanner) ObjectScanner() ObjectScanner

func (*Scanner) Scan

func (s *Scanner) Scan() bool

Scan advances the Scanner to the next element, which will then be available through the Kind, Value, and BoolValue methods. Scan returns false if there are no more elements in the input or an error is encountered. The Err method returns the error if any.

func (*Scanner) Value

func (s *Scanner) Value() []byte

Value returns the bytes of the current string or number value. The underlying array may point to data that will be overwritten by a subsequent call to Scan.

type SyntaxError

type SyntaxError struct {
	Pos int
	// contains filtered or unexported fields
}

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

Jump to

Keyboard shortcuts

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