JsonStruct

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

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

Go to latest
Published: Aug 12, 2022 License: MIT Imports: 9 Imported by: 0

README

JsonStruct

Json / JS dynamic structure. Play with Go.

Docs

Standard

Parsing
Byte array core
Marshallers
Data access speed
  • Primitive values
    • Bool implemented in same way on both sides so no difference in speed - 0.5 - 0.6 ns (instantly)
    • Value implementation
      • Write: 0.5 - 1.2 ns
      • Read set type: 1.7 - 2.7 ns
      • Check: 1.2 - 2.3 ns
    • Pointer implementation
      • Write: 15 - 36 ns
      • Read set type: 1.7 - 3.0 ns
      • Check: 1.2 - 2.3 ns
  • Object operations
    • Value implementation
      • Set key: 392 - 497 ns (22% - 48% slower than go map)
      • Get key: 67 - 77 ns (43% - 57% slower than go map)
      • Check: 70 - 72 ns (47% - 49% slower than go map)
    • Pointer implementation
      • Set key: 409 - 541 ns
        • 27% - 61% slower than go map
        • 4% - 8% slower than value implementation
      • Get key: 71 - 79 ns
        • 51% - 61% slower than go map
        • 3% - 6% slower than value implementation
      • Check: 71 - 74 ns
        • 51% slower than go map
        • 1% - 3% slower than value implementation
    • Go Map implementation
      • Set key: 321 - 336 ns
      • Get key: 47 - 49 ns
      • Check: 47 - 49 ns
  • Array operations
    • Value implementation
      • Push: 70 - 124 ns (84% - 143% slower than go slice)
      • Pop: 2.1 - 2.5 ns (similar to go slice)
    • Pointer implementation
      • Push: 91 - 132 ns (139% - 159% slower than go slice)
      • Pop: 29 - 31 ns (1100% - 1200% slower than value implementation)
    • Go slice implementation
      • Push: 38 - 51 ns
      • Pop: 2.3 ns (single measurement)
Memory
  • Primitive values:

    • Value implementation: 112 bytes
    • Pointer implementation: 16 - 40 bytes depend on the value

    From 2.8 to 7 times larger value then pointer implementation.

  • Object values (7 elements with all primitive types):

    • Value implementation: 160 bytes empty object and 1232 bytes with set of fields
    • Pointer implementation: 72 bytes empty object and 536 bytes with set of fields

    From 2.2 to 2.3 times larger value then pointer implementation.

  • Array values:

    • Value implementation: 112 bytes empty array and 1136 bytes with set of elements
    • Pointer implementation: 40 bytes empty array and 624 bytes with set of elements

    From 1.8 to 2.8 times larger value then pointer implementation.

Summary

The value implementation is faster than pointer implementation. The difference is due to the fact that value implementation is less memory efficient. Value implementation should expect to consume approximately 2 time more memory than pointer implementation (depends on stored values). The performance of pointer implementation significantly slow in array implementation the rest operations in same range or similar to value implementation. For performance please handle maps and slices.

How to increase version

  • commit all required changes
  • git tag <version - v0.0.2>
  • git push origin --tags
  • done - check docs on pkg.go.dev
  • install by go get -u github.com/Pencroff/JsonStruct

ToDo

  • Parse json to struct
  • ToJson from test to suite
  • Make diff
  • Evaluate circular dependencies and return error
  • MsgPack

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Buffers
	JStructScannerBufferSize       = 4 * 1024
	JSStructScannerBufferThreshold = JStructScannerBufferSize >> 3
)
View Source
var (
	// UnmarshalJSON Func is injection point of json.Unmarshaller for JsonStruct
	UnmarshalJSON = UnmarshalJSONFn
	// MarshalJSON Func is injection point of json.Marshaller for JsonStruct
	MarshalJSON = MarshalJSONFn
	// JStructParse Func provides io.Reader based parsing of JSON data
	JStructParse = JStructParseFn
	// JStructSerialize Func provides io.Writer based serialization of JSON data
	JStructSerialize = JStructSerializeFn
)

Injection points for testing purpose and custom implementation. If you would like to override JSON marshal/unmarshal implementation, please follow bellow approach.

js.UnmarshalJSON = func (bytes []byte, v js.JStructOps) error { ... }
js.MarshalJSON = func (v js.JStructOps) ([]byte, error) { ... }
View Source
var IndexOutOfRangeError = errors.New("JsonStruct: index out of range")
View Source
var InvalidCharacterError = errors.New("JsonStruct: invalid character")
View Source
var InvalidEscapeCharacterError = errors.New("JsonStruct: invalid escape character")
View Source
var InvalidHexNumberError = errors.New("JsonStruct: invalid hex number")
View Source
var NotArrayError = errors.New("JsonStruct: not an array, set explicitly")
View Source
var NotObjectError = errors.New("JsonStruct: not an object, set explicitly")
View Source
var OffsetOutOfRangeError = errors.New("JStructReader: offset out of range")
View Source
var UnsupportedTypeError = errors.New("JsonStruct: unsupported value type, resolved as null")

Functions

func JStructParseFn

func JStructParseFn(rd io.Reader, v JStructOps) (e error)

func JStructSerializeFn

func JStructSerializeFn(v JStructOps, wr io.Writer) (e error)

func MarshalJSONFn

func MarshalJSONFn(v JStructOps) ([]byte, error)

func ParsePrimitiveValue

func ParsePrimitiveValue(bt byte, rd *bufio.Reader, v JStructOps) (e error)

func UnmarshalJSONFn

func UnmarshalJSONFn(b []byte, v JStructOps) error

Types

type ArrayOps

type ArrayOps interface {
	Push(interface{}) error
	Pop() JStructOps
	Shift() JStructOps
	SetIndex(int, interface{}) error
	GetIndex(int) JStructOps
}

type GeneralOps

type GeneralOps interface {
	Type() Type
	Value() interface{}

	IsNull() bool
	SetNull()

	IsObject() bool
	AsObject()

	IsArray() bool
	AsArray()

	Size() int
}

type InvalidJsonError

type InvalidJsonError struct {
	Err error
}

func (InvalidJsonError) Error

func (i InvalidJsonError) Error() string

type InvalidJsonPtrError

type InvalidJsonPtrError struct {
	Err error
	Pos int
}

func (InvalidJsonPtrError) Error

func (i InvalidJsonPtrError) Error() string

type InvalidJsonTokenPtrError

type InvalidJsonTokenPtrError struct {
	Err error
	Pos int
}

func (InvalidJsonTokenPtrError) Error

func (i InvalidJsonTokenPtrError) Error() string

type JStructOps

type JStructOps interface {
	GeneralOps
	PrimitiveOps
	ObjectOps
	ArrayOps
}

type JStructScanner

type JStructScanner interface {
	Current() byte    // show from buffer
	Bytes() []byte    // copy to data and release
	Next() error      // scan 1 byte
	Scan(n int) error // move pointer, if required read data

	Index() int                        // position in file
	Buffer() []byte                    // current buffer
	Window() (int, int)                // window of current buffer (start, end)
	FillBuffFrom(idx int) (int, error) // fill buffer from idx, should be in interval [start, size)
}

func NewJStructScanner

func NewJStructScanner(rd io.Reader) JStructScanner

func NewJStructScannerWithParam

func NewJStructScannerWithParam(rd io.Reader, size, threshold int) JStructScanner

type JStructScannerImpl

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

func (*JStructScannerImpl) Buffer

func (j *JStructScannerImpl) Buffer() []byte

Buffer returns current buffer

func (*JStructScannerImpl) Bytes

func (j *JStructScannerImpl) Bytes() []byte

Bytes returns data in window (start, ptr)

func (*JStructScannerImpl) Current

func (j *JStructScannerImpl) Current() byte

Current returns pointed byte

func (*JStructScannerImpl) FillBuffFrom

func (j *JStructScannerImpl) FillBuffFrom(idx int) (int, error)

func (*JStructScannerImpl) Index

func (j *JStructScannerImpl) Index() int

Index returns current position in file

func (*JStructScannerImpl) Next

func (j *JStructScannerImpl) Next() error

func (*JStructScannerImpl) Scan

func (j *JStructScannerImpl) Scan(n int) error

func (*JStructScannerImpl) Window

func (j *JStructScannerImpl) Window() (int, int)

Window returns window of current buffer (start, ptr)

type JStructTokenizer

type JStructTokenizer interface {
	Next() error
	Value() []byte
	Kind() TokenizerKind
	Level() TokenizerLevel
}

func NewJStructTokenizer

func NewJStructTokenizer(sc JStructScanner) JStructTokenizer

type JStructTokenizerImpl

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

func (*JStructTokenizerImpl) Kind

func (*JStructTokenizerImpl) Level

func (*JStructTokenizerImpl) Next

func (t *JStructTokenizerImpl) Next() error

func (*JStructTokenizerImpl) PopLevel

func (t *JStructTokenizerImpl) PopLevel()

PopLevel pops the current level from the stack and sets the new level to the popped value

func (*JStructTokenizerImpl) PushLevel

func (t *JStructTokenizerImpl) PushLevel(l TokenizerLevel)

PushLevel pushes the current level to the stack and sets the new level to the given value

func (*JStructTokenizerImpl) ReadExponentPart

func (t *JStructTokenizerImpl) ReadExponentPart(firstIdx int) error

func (*JStructTokenizerImpl) ReadFalse

func (t *JStructTokenizerImpl) ReadFalse() error

func (*JStructTokenizerImpl) ReadFractionPart

func (t *JStructTokenizerImpl) ReadFractionPart(firstIdx int) error

func (*JStructTokenizerImpl) ReadNull

func (t *JStructTokenizerImpl) ReadNull() error

func (*JStructTokenizerImpl) ReadNumber

func (t *JStructTokenizerImpl) ReadNumber(hasMinus bool) error

func (*JStructTokenizerImpl) ReadString

func (t *JStructTokenizerImpl) ReadString() error

func (*JStructTokenizerImpl) ReadStringTime

func (t *JStructTokenizerImpl) ReadStringTime() error

Read json string or time in RFC3339 format

func (*JStructTokenizerImpl) ReadTrue

func (t *JStructTokenizerImpl) ReadTrue() error

func (*JStructTokenizerImpl) Value

func (t *JStructTokenizerImpl) Value() []byte

type JsonStruct

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

JsonStruct is a struct that can be converted to JSON. It implements the json.Marshaler and json.Unmarshaler interfaces. // It also implements the sql.Scanner and sql.Valuer interfaces. It supports JSON types like:

  • string
  • int / int64
  • float64
  • bool
  • [extra] DateTime (ISO 8601, rfc3339)
  • Object
  • Array

func (*JsonStruct) AsArray

func (s *JsonStruct) AsArray()

func (*JsonStruct) AsObject

func (s *JsonStruct) AsObject()

func (*JsonStruct) Bool

func (s *JsonStruct) Bool() bool

func (*JsonStruct) Float

func (s *JsonStruct) Float() float64

func (*JsonStruct) GetIndex

func (s *JsonStruct) GetIndex(i int) JStructOps

func (*JsonStruct) GetKey

func (s *JsonStruct) GetKey(key string) JStructOps

func (*JsonStruct) HasKey

func (s *JsonStruct) HasKey(key string) bool

func (*JsonStruct) Int

func (s *JsonStruct) Int() int64

func (*JsonStruct) IsArray

func (s *JsonStruct) IsArray() bool

func (*JsonStruct) IsBool

func (s *JsonStruct) IsBool() bool

func (*JsonStruct) IsFloat

func (s *JsonStruct) IsFloat() bool

func (*JsonStruct) IsInt

func (s *JsonStruct) IsInt() bool

func (*JsonStruct) IsNull

func (s *JsonStruct) IsNull() bool

func (*JsonStruct) IsNumber

func (s *JsonStruct) IsNumber() bool

func (*JsonStruct) IsObject

func (s *JsonStruct) IsObject() bool

func (*JsonStruct) IsString

func (s *JsonStruct) IsString() bool

func (*JsonStruct) IsTime

func (s *JsonStruct) IsTime() bool

func (*JsonStruct) IsUint

func (s *JsonStruct) IsUint() bool

func (*JsonStruct) Keys

func (s *JsonStruct) Keys() []string

func (*JsonStruct) MarshalJSON

func (s *JsonStruct) MarshalJSON() ([]byte, error)

func (*JsonStruct) Pop

func (s *JsonStruct) Pop() JStructOps

func (*JsonStruct) Push

func (s *JsonStruct) Push(v interface{}) error

func (*JsonStruct) RemoveKey

func (s *JsonStruct) RemoveKey(key string) JStructOps

func (*JsonStruct) SetBool

func (s *JsonStruct) SetBool(v bool)

func (*JsonStruct) SetFloat

func (s *JsonStruct) SetFloat(v float64)

func (*JsonStruct) SetIndex

func (s *JsonStruct) SetIndex(i int, v interface{}) error

func (*JsonStruct) SetInt

func (s *JsonStruct) SetInt(v int64)

func (*JsonStruct) SetKey

func (s *JsonStruct) SetKey(key string, v interface{}) error

func (*JsonStruct) SetNull

func (s *JsonStruct) SetNull()

func (*JsonStruct) SetString

func (s *JsonStruct) SetString(v string)

func (*JsonStruct) SetTime

func (s *JsonStruct) SetTime(v time.Time)

func (*JsonStruct) SetUint

func (s *JsonStruct) SetUint(v uint64)

func (*JsonStruct) Shift

func (s *JsonStruct) Shift() JStructOps

func (*JsonStruct) Size

func (s *JsonStruct) Size() int

func (*JsonStruct) String

func (s *JsonStruct) String() string

func (*JsonStruct) Time

func (s *JsonStruct) Time() time.Time

func (*JsonStruct) Type

func (s *JsonStruct) Type() Type

func (*JsonStruct) Uint

func (s *JsonStruct) Uint() uint64

func (*JsonStruct) UnmarshalJSON

func (s *JsonStruct) UnmarshalJSON(bytes []byte) error

func (*JsonStruct) Value

func (s *JsonStruct) Value() interface{}

type ObjectOps

type ObjectOps interface {
	SetKey(string, interface{}) error
	GetKey(string) JStructOps
	RemoveKey(string) JStructOps
	HasKey(string) bool
	Keys() []string
}

type PrimitiveOps

type PrimitiveOps interface {
	IsBool() bool
	SetBool(bool)
	Bool() bool

	IsNumber() bool

	IsInt() bool
	SetInt(int64)
	Int() int64

	IsUint() bool
	SetUint(uint64)
	Uint() uint64

	IsFloat() bool
	SetFloat(float64)
	Float() float64

	IsString() bool
	SetString(string)
	String() string

	IsTime() bool
	SetTime(time.Time)
	Time() time.Time
}

type TokenizerKind

type TokenizerKind byte
const (
	KindUnknown TokenizerKind = iota
	KindNull
	KindFalse
	KindTrue
	KindNumber
	KindFloatNumber
	KindTime
	KindString
	KindLiteral
)

func (TokenizerKind) String

func (k TokenizerKind) String() string

type TokenizerLevel

type TokenizerLevel byte
const (
	LevelUnknown TokenizerLevel = iota
	LevelRoot
	LevelObject
	LevelObjectEnd
	LevelArray
	LevelArrayEnd
	LevelKey
	LevelValue
	LevelValueLast
)

func (TokenizerLevel) String

func (l TokenizerLevel) String() string

type Type

type Type byte
const (
	Null Type = iota
	False
	True
	Int
	Uint
	Float
	String
	Time
	Object
	Array
)

func (Type) String

func (t Type) String() string

Directories

Path Synopsis
benchmark module

Jump to

Keyboard shortcuts

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