jsonx

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 11, 2021 License: MIT Imports: 8 Imported by: 1

README

jsonx - json pkg supports comments,extraComma,unquotedKey

example 1: WithComment,WithExtraComma

{
	/* c-style comment supported if contains option WithComment
	 * extra comma allowed if contains option WithExtraComma
	 */
	// a is an object
	"a": {
		// id is an integer
		"id": 1,
		// name is a string
		"name": "xxx", // name of A
	},
	"b": {
		"key": "key",
		"value": 222, // NOTE: here is an extra comma, but no problem if contains option WithExtraComma
	},
	"x": 2,
	"y": 1.5,
	"z": true,
	"s": [
		// array element 0
		{"key":"k1", "value": 1},
		// array element 1
		{
			"key":"k2",
			"value":2
		},{} // array element 2
	]
}

example 2: WithComment,WithExtraComma,WithUnquotedKey

{
	/* c-style comment supported if contains option WithComment
	 * extra comma allowed if contains option WithExtraComma
	 * key not quoted with " allowed if contains option WithUnquotedKey
	 */
	// a is an object
	a: {
		// id is an integer
		id: 1,
		// name is a string
		name: "xxx", // name of A
	},
	b: {
		key: "key",
		value: 222, // NOTE: here is an extra comma, but no problem if contains option WithExtraComma
	},
	x: 2,
	y: 1.5,
	z: true,
	s: [
		// array element 0
		{key:"k1", value: 1},
		// array element 1
		{
			key:"k2",
			value:2
		},{} // array element 2
	]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

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

Marshal marshal value v to json with options

func NewEncoder

func NewEncoder(w io.Writer) *json.Encoder

NewEncoder wraps json.NewEncoder

func Unmarshal

func Unmarshal(data []byte, v interface{}, opts ...Option) error

Unmarshal wraps json.Unmarshal with options

func Write

func Write(w io.Writer, node Node, opts ...Option) error

Write writes a json node to writer w

func WriteFile

func WriteFile(filename string, node Node, perm os.FileMode, opts ...Option) error

WriteFile writer a json node to file

Types

type Decoder

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

Decoder wraps json.Decoder with options

func NewDecoder

func NewDecoder(r io.Reader, opts ...Option) *Decoder

NewDecoder creates a decoder with reader and options

func (*Decoder) Decode

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

Decode decodes data and stores it in the valyala pointed to by v

type Node

type Node interface {
	// embed encoding.Node
	encoding.Node
	// Doc returns lead comments
	Doc() *encoding.CommentGroup
	// Comment returns line comments
	Comment() *encoding.CommentGroup
	// NumChild returns number of child nodes
	NumChild() int
	// ByIndex gets ith child node, key is empty if current node is not an object node
	// Panic if i out of range [0,NumChild)
	ByIndex(i int) (key string, node Node)
	// ByKey gets child node by key, nil returned if key not found
	ByKey(key string) Node
	// Value returns value of node as an interface
	Value() interface{}
	// IsEmpty indicates whther node is empty
	IsEmpty() bool
	// contains filtered or unexported methods
}

Node represents top-level json object

func Read

func Read(r io.Reader, opts ...Option) (Node, error)

Read reads a json node from reader r

func ReadBytes

func ReadBytes(data []byte, opts ...Option) (Node, error)

ReadBytes reads a json node from bytes

func ReadFile

func ReadFile(filename string, opts ...Option) (Node, error)

ReadFile reads a json node from file

type Option

type Option func(*options)

Option represents a function for setting options

func WithComment

func WithComment() Option

WithComment returns an option which sets supportComment true

func WithExtraComma

func WithExtraComma() Option

WithExtraComma returns an option which sets extraComma true

func WithIndent

func WithIndent(indent string) Option

WithIndent returns an option which with indent while outputing

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix returns an option which with prefix while outputing

func WithUnquotedKey

func WithUnquotedKey() Option

WithUnquotedKey returns an option which sets unquotedKey true

Jump to

Keyboard shortcuts

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