mongoextjson

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2020 License: BSD-2-Clause Imports: 18 Imported by: 0

README

mongoextjson

A package to encode/decode MongoDB extended JSON. Compatible with the new official go driver (https://github.com/mongodb/mongo-go-driver)

credits

This package is based on the code from the package go-mgo/mgo/internal/json written by Gustavo Niemeyer (@niemeyer), witch is itself based on an old version of the json package of the go standard library.

Documentation

Overview

Package mongoextjson allows to encode/decode MongoDB extended JSON

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Compact

func Compact(dst *bytes.Buffer, src []byte) error

Compact appends to dst the JSON-encoded src with insignificant space characters elided.

func Indent

func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error

Indent appends to dst an indented form of the JSON-encoded src. Each element in a JSON object or array begins on a new, indented line beginning with prefix followed by one or more copies of indent according to the indentation nesting. The data appended to dst does not begin with the prefix nor any indentation, to make it easier to embed inside other formatted JSON data. Although leading space characters (space, tab, carriage return, newline) at the beginning of src are dropped, trailing space characters at the end of src are preserved and copied to dst. For example, if src has no trailing spaces, neither will dst; if src ends in a trailing newline, so will dst.

func Marshal

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

Marshal return the BSON extended JSON encoding of value. The output is not a valid JSON and will look like

{ "_id": ObjectId("5a934e000102030405000000")}

func MarshalCanonical

func MarshalCanonical(value interface{}) ([]byte, error)

MarshalCanonical return the BSON extended JSON encoding of value. The output is a valid JSON and will look like

{ "_id": {"$oid": "5a934e000102030405000000"}}

func Unmarshal

func Unmarshal(data []byte, value interface{}) error

Unmarshal unmarshals a JSON value that may hold non-standard syntax as defined in BSON's extended JSON specification.

Types

type Decoder

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

A Decoder reads and decodes JSON values from an input stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder that reads from r.

The decoder introduces its own buffering and may read data from r beyond the JSON values requested.

func (*Decoder) Buffered

func (dec *Decoder) Buffered() io.Reader

Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to Decode.

func (*Decoder) Decode

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

Decode reads the next JSON-encoded value from its input and stores it in the value pointed to by v.

See the documentation for Unmarshal for details about the conversion of JSON into a Go value.

func (*Decoder) Extend

func (dec *Decoder) Extend(ext *Extension)

Extend changes the decoder behavior to consider the provided extension.

func (*Decoder) More

func (dec *Decoder) More() bool

More reports whether there is another element in the current array or object being parsed.

func (*Decoder) Token

func (dec *Decoder) Token() (Token, error)

Token returns the next JSON token in the input stream. At the end of the input stream, Token returns nil, io.EOF.

Token guarantees that the delimiters [ ] { } it returns are properly nested and matched: if Token encounters an unexpected delimiter in the input, it will return an error.

The input stream consists of basic JSON values—bool, string, number, and null—along with delimiters [ ] { } of type Delim to mark the start and end of arrays and objects. Commas and colons are elided.

func (*Decoder) UseNumber

func (dec *Decoder) UseNumber()

UseNumber causes the Decoder to unmarshal a number into an interface{} as a Number instead of as a float64.

type Delim

type Delim rune

A Delim is a JSON array or object delimiter, one of [ ] { or }.

func (Delim) String

func (d Delim) String() string

type Encoder

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

An Encoder writes JSON values to an output stream.

func NewEncoder

func NewEncoder(w io.Writer) *Encoder

NewEncoder returns a new encoder that writes to w.

func (*Encoder) DisableHTMLEscaping

func (enc *Encoder) DisableHTMLEscaping()

DisableHTMLEscaping causes the encoder not to escape angle brackets ("<" and ">") or ampersands ("&") in JSON strings.

func (*Encoder) Encode

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

Encode writes the JSON encoding of v to the stream, followed by a newline character.

See the documentation for Marshal for details about the conversion of Go values to JSON.

func (*Encoder) Extend

func (enc *Encoder) Extend(ext *Extension)

Extend changes the encoder behavior to consider the provided extension.

func (*Encoder) Indent

func (enc *Encoder) Indent(prefix, indent string)

Indent sets the encoder to format each encoded value with Indent.

type Extension

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

Extension holds a set of additional rules to be used when unmarshaling strict JSON or JSON-like content.

func (*Extension) DecodeConst

func (e *Extension) DecodeConst(name string, value interface{})

DecodeConst defines a constant name that may be observed inside JSON content and will be decoded with the provided value.

func (*Extension) DecodeFunc

func (e *Extension) DecodeFunc(name string, key string, args ...string)

DecodeFunc defines a function call that may be observed inside JSON content. A function with the provided name will be unmarshaled as the document {key: {args[0]: ..., args[N]: ...}}.

func (*Extension) DecodeKeyed

func (e *Extension) DecodeKeyed(key string, decode func(data []byte) (interface{}, error))

DecodeKeyed defines a key that when observed as the first element inside a JSON document triggers the decoding of that document via the provided decode function.

func (*Extension) DecodeTrailingCommas

func (e *Extension) DecodeTrailingCommas(accept bool)

DecodeTrailingCommas defines whether to accept trailing commas in maps and arrays.

func (*Extension) DecodeUnquotedKeys

func (e *Extension) DecodeUnquotedKeys(accept bool)

DecodeUnquotedKeys defines whether to accept map keys that are unquoted strings.

func (*Extension) EncodeType

func (e *Extension) EncodeType(sample interface{}, encode func(v interface{}) ([]byte, error))

EncodeType registers a function to encode values with the same type of the provided sample.

func (*Extension) Extend

func (e *Extension) Extend(ext *Extension)

Extend includes in e the extensions defined in ext.

type InvalidUTF8Error

type InvalidUTF8Error struct {
	S string // the whole string value that caused the error
}

InvalidUTF8Error before Go 1.2, an InvalidUTF8Error was returned by Marshal when attempting to encode a string value with invalid UTF-8 sequences. As of Go 1.2, Marshal instead coerces the string to valid UTF-8 by replacing invalid bytes with the Unicode replacement rune U+FFFD. This error is no longer generated but is kept for backwards compatibility with programs that might mention it.

func (*InvalidUTF8Error) Error

func (e *InvalidUTF8Error) Error() string

type InvalidUnmarshalError

type InvalidUnmarshalError struct {
	Type reflect.Type
}

An InvalidUnmarshalError describes an invalid argument passed to Unmarshal. (The argument to Unmarshal must be a non-nil pointer.)

func (*InvalidUnmarshalError) Error

func (e *InvalidUnmarshalError) Error() string

type Marshaler

type Marshaler interface {
	MarshalJSON() ([]byte, error)
}

Marshaler is the interface implemented by types that can marshal themselves into valid JSON.

type MarshalerError

type MarshalerError struct {
	Type reflect.Type
	Err  error
}

A MarshalerError is returned by Marshal when attempting to marshal an invalid JSON

func (*MarshalerError) Error

func (e *MarshalerError) Error() string

type Number

type Number string

A Number represents a JSON number literal.

func (Number) Float64

func (n Number) Float64() (float64, error)

Float64 returns the number as a float64.

func (Number) Int64

func (n Number) Int64() (int64, error)

Int64 returns the number as an int64.

func (Number) String

func (n Number) String() string

String returns the literal text of the number.

type SyntaxError

type SyntaxError struct {
	Offset int64 // error occurred after reading Offset bytes
	// contains filtered or unexported fields
}

A SyntaxError is a description of a JSON syntax error.

func (*SyntaxError) Error

func (e *SyntaxError) Error() string

type Token

type Token interface{}

A Token holds a value of one of these types:

Delim, for the four JSON delimiters [ ] { }
bool, for JSON booleans
float64, for JSON numbers
Number, for JSON numbers
string, for JSON string literals
nil, for JSON null

type UnmarshalFieldError

type UnmarshalFieldError struct {
	Key   string
	Type  reflect.Type
	Field reflect.StructField
}

An UnmarshalFieldError describes a JSON object key that led to an unexported (and therefore unwritable) struct field. (No longer used; kept for compatibility.)

func (*UnmarshalFieldError) Error

func (e *UnmarshalFieldError) Error() string

type UnmarshalTypeError

type UnmarshalTypeError struct {
	Value  string       // description of JSON value - "bool", "array", "number -5"
	Type   reflect.Type // type of Go value it could not be assigned to
	Offset int64        // error occurred after reading Offset bytes
}

An UnmarshalTypeError describes a JSON value that was not appropriate for a value of a specific Go type.

func (*UnmarshalTypeError) Error

func (e *UnmarshalTypeError) Error() string

type Unmarshaler

type Unmarshaler interface {
	UnmarshalJSON([]byte) error
}

Unmarshaler is the interface implemented by types that can unmarshal a JSON description of themselves. The input can be assumed to be a valid encoding of a JSON value. UnmarshalJSON must copy the JSON data if it wishes to retain the data after returning.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	Type reflect.Type
}

An UnsupportedTypeError is returned by Marshal when attempting to encode an unsupported value type.

func (*UnsupportedTypeError) Error

func (e *UnsupportedTypeError) Error() string

type UnsupportedValueError

type UnsupportedValueError struct {
	Value reflect.Value
	Str   string
}

An UnsupportedValueError is returned by Marshal when attempting to encode an unsupported value.

func (*UnsupportedValueError) Error

func (e *UnsupportedValueError) Error() string

Jump to

Keyboard shortcuts

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