insaneJSON

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2019 License: BSD-3-Clause Imports: 11 Imported by: 0

README

Insane JSON

Lighting fast and simple JSON decode/encode library for GO

Key features

To be filled

Usage

    // ==== DECODE API ====
    root, err = insaneJSON.DecodeString(jsonString)        // from string
    root, err = insaneJSON.DecodeBytes(jsonBytes)          // from byte slice
    defer insaneJSON.Release(root)                         // place root back to pool 

    // ==== GET API ====
    code = root.Dig("response", "code").AsInt()            // int from objects
    body = root.Dig("response", "body").AsString()         // string from objects

    keys = []string{"items", "3", "name"} 
    thirdItemName = root.Dig(keys...).AsString()           // string from objects and array

    // ==== CHECK API ====
    isObject = root.Dig("response").IsObject()             // is value object?
    isInt = root.Dig("response", "code").IsInt()           // is value null?
    isArray = root.Dig("items").IsArray()                  // is value array?

    // ==== DELETE API ====
    root.Dig("response", "code").Suicide()                 // delete object field
    root.Dig("items", "3").Suicide()                       // delete array element
    anyDugNode.Suicide()                                   // delete any previously dug node

    // ==== MODIFY API ====
    root.Dig("response", "code").MutateToString("OK")      // convert to string
    root.Dig("items", "3").MutateToObject()                // convert to empty object

    item = `{"name":"book","weight":1000}`
    err = root.Dig("items", "3").MutateToJSON(item)        // convert to parsed JSON  

    // ==== OBJECT API ====
    response = root.Dig("response")                        // get object
    fields = response.AsFields()                           // get object fields

    for _, field = range(fields) {                         
        fmt.Println(field.AsField())                       // print all object fields 
    }

    for _, field = range(fields) {                         
        response.Dig(field.AsField()).Suicide()            // remove all fields            
    }

    for _, field = range(fields) {                         
        field.Suicide()                                    // simpler way to remove all fields
    }
    
    header="Content-Encoding: gzip"
    response.AddField("header").MutateToString(header)     // add new field and set value 

    // ==== ARRAY API ====
    items = root.Dig("items")                              // get array
    elements = items.AsArray()                             // get array elements

    for _, element = range(elements) {                     
        fmt.Println(element.AsString())                    // print all array elements    
    }

    for _, element = range(elements) {                     
        element.Suicide()                                  // remove all elements
    }

    item = `{"name":"book","weight":1000}`
    err = items.AddElement().MutateToJSON(item)            // add new element and set value 

    // ==== ENCODE API ====
    To be filled

    // ==== STRICT API ====
    items = root.Dig("items").InStrictMode()               // convert value to strict mode
    items, err = root.DigStrict("items")                   // or get strict value directly
         
    o, err = items.AsObject()                              // now value has api with error handling  
    name, err = items.Dig("5").Dig("name").AsInt           // err won't be nil since name is a string 

    // ==== POOL API ====
    root, err = insaneJSON.DecodeString(json)              // get a root from the pool and place decoded json into it 
    emptyRoot = insaneJSON.Spawn()                         // get an empty root from the pool            

    root.DecodeString(emptyRoot, anotherJson)              // reuse a root to decode another JSONs

    insaneJSON.Release(root)                               // place roots back to the pool
    insaneJSON.Release(emptyRoot)                           

Benchmarks

To be filled

Documentation

Index

Constants

View Source
const (
	FlagFieldMap = 1 << 0
)

Variables

View Source
var (
	StartNodePoolSize = 16

	// decode errors
	ErrEmptyJSON                    = errors.New("json is empty")
	ErrUnexpectedJSONEnding         = errors.New("unexpected ending of json")
	ErrUnexpectedEndOfString        = errors.New("unexpected end of string")
	ErrUnexpectedEndOfTrue          = errors.New("unexpected end of true")
	ErrUnexpectedEndOfFalse         = errors.New("unexpected end of false")
	ErrUnexpectedEndOfNull          = errors.New("unexpected end of null")
	ErrUnexpectedEndOfObjectField   = errors.New("unexpected end of object field")
	ErrExpectedObjectField          = errors.New("expected object field")
	ErrExpectedObjectFieldSeparator = errors.New("expected object field separator")
	ErrExpectedValue                = errors.New("expected value")
	ErrExpectedComma                = errors.New("expected comma")

	// api errors
	ErrRootIsNil = errors.New("root is nil")
	ErrNotFound  = errors.New("node isn't found")
	ErrNotObject = errors.New("node isn't an object")
	ErrNotArray  = errors.New("node isn't an array")
	ErrNotBool   = errors.New("node isn't a bool")
	ErrNotString = errors.New("node isn't a string")
	ErrNotNumber = errors.New("node isn't a number")
	ErrNotField  = errors.New("node isn't an object field")
)

Functions

func Fuzz

func Fuzz(data []byte) int

func Release

func Release(root *Root)

Types

type Last

type Last struct {
	*Node
}

type Node

type Node struct {
	Type Type
	// contains filtered or unexported fields
}

func (*Node) AddField

func (n *Node) AddField(name string) *Node

func (*Node) AppendElement

func (n *Node) AppendElement() *Node

func (*Node) AsArray

func (n *Node) AsArray() []*Node

func (*Node) AsBool

func (n *Node) AsBool() bool

func (*Node) AsBytes

func (n *Node) AsBytes() []byte

func (*Node) AsEscapedString

func (n *Node) AsEscapedString() string

func (*Node) AsField

func (n *Node) AsField(field string) *Node

func (*Node) AsFieldValue

func (n *Node) AsFieldValue() *Node

func (*Node) AsFields

func (n *Node) AsFields() []*Node

func (*Node) AsFloat

func (n *Node) AsFloat() float64

func (*Node) AsInt

func (n *Node) AsInt() int

func (*Node) AsString

func (n *Node) AsString() string

func (*Node) Dig

func (n *Node) Dig(path ...string) *Node

Dig legendary insane dig function

func (*Node) DigStrict

func (n *Node) DigStrict(path ...string) (*StrictNode, error)

func (*Node) Encode

func (n *Node) Encode(out []byte) []byte

Encode legendary insane encode function uses already created byte buffer to place json data so mem allocations may occur only if buffer isn't long enough use it for performance

func (*Node) EncodeToByte

func (n *Node) EncodeToByte() []byte

EncodeToByte legendary insane encode function slow because it allocates new byte buffer on every call use Encode to reuse already created buffer and gain more performance

func (*Node) EncodeToString

func (n *Node) EncodeToString() string

EncodeToString legendary insane encode function slow because it allocates new string on every call use Encode to reuse already created buffer and gain more performance

func (*Node) InStrictMode

func (n *Node) InStrictMode() *StrictNode

func (*Node) InsertElement

func (n *Node) InsertElement(pos int) *Node

func (*Node) IsArray

func (n *Node) IsArray() bool

func (*Node) IsFalse

func (n *Node) IsFalse() bool

func (*Node) IsField

func (n *Node) IsField() bool

func (*Node) IsNil

func (n *Node) IsNil() bool

func (*Node) IsNull

func (n *Node) IsNull() bool

func (*Node) IsNumber

func (n *Node) IsNumber() bool

func (*Node) IsObject

func (n *Node) IsObject() bool

func (*Node) IsString

func (n *Node) IsString() bool

func (*Node) IsTrue

func (n *Node) IsTrue() bool

func (*Node) MergeWith

func (n *Node) MergeWith(node *Node) *Node

func (*Node) MutateToBool

func (n *Node) MutateToBool(value bool) *Node

func (*Node) MutateToEscapedString

func (n *Node) MutateToEscapedString(value string) *Node

func (*Node) MutateToField

func (n *Node) MutateToField(value string) *Node

func (*Node) MutateToFloat

func (n *Node) MutateToFloat(value float64) *Node

func (*Node) MutateToInt

func (n *Node) MutateToInt(value int) *Node

func (*Node) MutateToJSON

func (n *Node) MutateToJSON(json string) *Node

func (*Node) MutateToNode

func (n *Node) MutateToNode(node *Node) *Node

MutateToNode it isn't safe function, if you create node cycle, encode() may freeze

func (*Node) MutateToNull

func (n *Node) MutateToNull(value bool) *Node

func (*Node) MutateToObject

func (n *Node) MutateToObject() *Node

func (*Node) MutateToString

func (n *Node) MutateToString(value string) *Node

func (*Node) Suicide

func (n *Node) Suicide()

Suicide legendary insane suicide function

type Root

type Root struct {
	*Node
}

func DecodeBytes

func DecodeBytes(jsonBytes []byte) (*Root, error)

func DecodeString

func DecodeString(json string) (*Root, error)

func Spawn

func Spawn() *Root

func (*Root) BuffCap

func (r *Root) BuffCap() int

BuffCap returns current size of internal buffer

func (*Root) DecodeBytes

func (r *Root) DecodeBytes(jsonBytes []byte) error

DecodeBytes clear root and decode new JSON useful for reusing root to decode multiple times and reduce allocations

func (*Root) DecodeBytesAdditional

func (r *Root) DecodeBytesAdditional(jsonBytes []byte) (*Node, error)

DecodeBytesAdditional doesn't clean root, uses root's node pool to decode JSON

func (*Root) DecodeString

func (r *Root) DecodeString(json string) error

DecodeString clear root and decode new JSON useful for reusing root to decode multiple times and reduce allocations

func (*Root) DecodeStringAdditional

func (r *Root) DecodeStringAdditional(json string) (*Node, error)

DecodeStringAdditional doesn't clean root, uses root's node pool to decode JSON

func (*Root) PullSize

func (r *Root) PullSize() int

PullSize returns current size of node pool

func (*Root) ReleaseBufMem

func (r *Root) ReleaseBufMem()

ReleasePoolMem sends internal buffer to GC

func (*Root) ReleaseMem

func (r *Root) ReleaseMem()

ReleaseMem sends node pool and internal buffer to GC useful to reduce memory usage after decoding big JSON

func (*Root) ReleasePoolMem

func (r *Root) ReleasePoolMem()

ReleasePoolMem sends node pool to GC

type StrictNode

type StrictNode struct {
	*Node
}

func (*StrictNode) AsArray

func (n *StrictNode) AsArray() ([]*Node, error)

func (*StrictNode) AsBool

func (n *StrictNode) AsBool() (bool, error)

func (*StrictNode) AsBytes

func (n *StrictNode) AsBytes() ([]byte, error)

func (*StrictNode) AsEscapedString

func (n *StrictNode) AsEscapedString() (string, error)

func (*StrictNode) AsField

func (n *StrictNode) AsField(field string) (*Node, error)

func (*StrictNode) AsFieldValue

func (n *StrictNode) AsFieldValue() (*Node, error)

func (*StrictNode) AsFields

func (n *StrictNode) AsFields() ([]*Node, error)

func (*StrictNode) AsFloat

func (n *StrictNode) AsFloat() (float64, error)

func (*StrictNode) AsInt

func (n *StrictNode) AsInt() (int, error)

func (*StrictNode) AsString

func (n *StrictNode) AsString() (string, error)

type Type

type Type int
const (
	Object Type = 0
	Array  Type = 1
	String Type = 2
	Number Type = 3
	True   Type = 4
	False  Type = 5
	Null   Type = 6
	Field  Type = 7
)

Jump to

Keyboard shortcuts

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