Skip to content

Commit

Permalink
[*] chore: try using jsoniter instead of jsonparser, however, the res…
Browse files Browse the repository at this point in the history
…ult is not good. This attempt will be abandoned
  • Loading branch information
Andrew-M-C committed Apr 20, 2021
1 parent 09c1589 commit bea1e74
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 206 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
language: go

go:
- 1.12
- 1.13
- 1.14
- 1.15
- 1.16

install:
- go get github.com/mattn/goveralls

before_script:
- go get github.com/buger/jsonparser
- go mod tidy

script:
- go test -v -failfast -covermode=count -coverprofile=coverage.out && $HOME/gopath/bin/goveralls -coverprofile=coverage.out -service=travis-ci -repotoken $COVERALLS_TOKEN
Expand Down
12 changes: 6 additions & 6 deletions append.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jsonvalue

import (
"github.com/buger/jsonparser"
jsoniter "github.com/json-iterator/go"
)

// Append type is for InTheEnd() or InTheBeginning() function. Please refer to related functions.
Expand Down Expand Up @@ -124,17 +124,17 @@ func (v *V) AppendArray() *Append {
func (apd *Append) InTheBeginning(params ...interface{}) (*V, error) {
v := apd.v
c := apd.c
if nil == v || v.valueType == jsonparser.NotExist {
if nil == v || v.valueType == jsoniter.InvalidValue {
return nil, ErrValueUninitialized
}
if nil == c || c.valueType == jsonparser.NotExist {
if nil == c || c.valueType == jsoniter.InvalidValue {
return nil, ErrValueUninitialized
}

// this is the last iteration
paramCount := len(params)
if paramCount == 0 {
if v.valueType != jsonparser.Array {
if v.valueType != jsoniter.ArrayValue {
return nil, ErrNotArrayValue
}

Expand All @@ -158,14 +158,14 @@ func (apd *Append) InTheBeginning(params ...interface{}) (*V, error) {
func (apd *Append) InTheEnd(params ...interface{}) (*V, error) {
v := apd.v
c := apd.c
if v.valueType == jsonparser.NotExist {
if v.valueType == jsoniter.InvalidValue {
return nil, ErrValueUninitialized
}

// this is the last iteration
paramCount := len(params)
if paramCount == 0 {
if v.valueType != jsonparser.Array {
if v.valueType != jsoniter.ArrayValue {
return nil, ErrNotArrayValue
}

Expand Down
18 changes: 0 additions & 18 deletions conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"unicode"
"unicode/utf16"
"unicode/utf8"
"unsafe"
)

func parseUint(b []byte) (uint64, error) {
Expand Down Expand Up @@ -175,23 +174,6 @@ func getu4(s []byte) rune {
return r
}

func parseStringNoQuote(b []byte) (string, error) {
if len(b) == 0 {
return "", nil
}
s := unsafe.Sizeof(b[0])
p := unsafe.Pointer(unsafe.Pointer(&b))
sh := (*reflect.SliceHeader)(p)
bh := reflect.SliceHeader{
Data: sh.Data - s,
Len: sh.Len + int(s+s),
Cap: sh.Len + int(s+s),
}
b = *(*[]byte)(unsafe.Pointer(&bh))
str, _, err := parseString(b)
return str, err
}

func formatBool(b bool) string {
if b {
return "true"
Expand Down
6 changes: 3 additions & 3 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/buger/jsonparser"
jsoniter "github.com/json-iterator/go"
)

func (v *V) delFromObjectChildren(key string) (exist bool) {
Expand Down Expand Up @@ -56,7 +56,7 @@ func (v *V) Delete(firstParam interface{}, otherParams ...interface{}) error {
}

func (v *V) deleteInCurrValue(param interface{}) error {
if v.valueType == jsonparser.Object {
if v.valueType == jsoniter.ObjectValue {
// string expected
key, err := intfToString(param)
if err != nil {
Expand All @@ -69,7 +69,7 @@ func (v *V) deleteInCurrValue(param interface{}) error {
return nil
}

if v.valueType == jsonparser.Array {
if v.valueType == jsoniter.ArrayValue {
// interger expected
pos, err := intfToInt(param)
if err != nil {
Expand Down
36 changes: 18 additions & 18 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"fmt"
"strings"

"github.com/buger/jsonparser"
jsoniter "github.com/json-iterator/go"
)

// Len returns length of an object or array type JSON value.
//
// Len 返回当前对象类型或数组类型的 JSON 的成员长度。如果不是这两种类型,那么会返回 0。
func (v *V) Len() int {
switch v.valueType {
case jsonparser.Array:
case jsoniter.ArrayValue:
return v.children.array.Len()
case jsonparser.Object:
case jsoniter.ObjectValue:
return len(v.children.object)
default:
return 0
Expand Down Expand Up @@ -59,7 +59,7 @@ func (v *V) getFromObjectChildren(key string) (child *V, exist bool) {
}

func (v *V) getInCurrValue(param interface{}) (*V, error) {
if v.valueType == jsonparser.Array {
if v.valueType == jsoniter.ArrayValue {
// integer expected
pos, err := intfToInt(param)
if err != nil {
Expand All @@ -71,7 +71,7 @@ func (v *V) getInCurrValue(param interface{}) (*V, error) {
}
return child, nil

} else if v.valueType == jsonparser.Object {
} else if v.valueType == jsoniter.ObjectValue {
// string expected
key, err := intfToString(param)
if err != nil {
Expand All @@ -96,7 +96,7 @@ func (v *V) GetString(firstParam interface{}, otherParams ...interface{}) (strin
if err != nil {
return "", err
}
if ret.valueType != jsonparser.String {
if ret.valueType != jsoniter.StringValue {
return "", ErrTypeNotMatch
}
return ret.String(), nil
Expand All @@ -110,7 +110,7 @@ func (v *V) GetInt(firstParam interface{}, otherParams ...interface{}) (int, err
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Int(), nil
Expand All @@ -124,7 +124,7 @@ func (v *V) GetUint(firstParam interface{}, otherParams ...interface{}) (uint, e
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Uint(), nil
Expand All @@ -138,7 +138,7 @@ func (v *V) GetInt64(firstParam interface{}, otherParams ...interface{}) (int64,
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Int64(), nil
Expand All @@ -152,7 +152,7 @@ func (v *V) GetUint64(firstParam interface{}, otherParams ...interface{}) (uint6
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Uint64(), nil
Expand All @@ -166,7 +166,7 @@ func (v *V) GetInt32(firstParam interface{}, otherParams ...interface{}) (int32,
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Int32(), nil
Expand All @@ -180,7 +180,7 @@ func (v *V) GetUint32(firstParam interface{}, otherParams ...interface{}) (uint3
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Uint32(), nil
Expand All @@ -194,7 +194,7 @@ func (v *V) GetFloat64(firstParam interface{}, otherParams ...interface{}) (floa
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Float64(), nil
Expand All @@ -208,7 +208,7 @@ func (v *V) GetFloat32(firstParam interface{}, otherParams ...interface{}) (floa
if err != nil {
return 0, err
}
if ret.valueType != jsonparser.Number {
if ret.valueType != jsoniter.NumberValue {
return 0, ErrTypeNotMatch
}
return ret.Float32(), nil
Expand All @@ -222,7 +222,7 @@ func (v *V) GetBool(firstParam interface{}, otherParams ...interface{}) (bool, e
if err != nil {
return false, err
}
if ret.valueType != jsonparser.Boolean {
if ret.valueType != jsoniter.BoolValue {
return false, ErrTypeNotMatch
}
return ret.Bool(), nil
Expand All @@ -236,7 +236,7 @@ func (v *V) GetNull(firstParam interface{}, otherParams ...interface{}) error {
if err != nil {
return err
}
if ret.valueType != jsonparser.Null {
if ret.valueType != jsoniter.NilValue {
return ErrTypeNotMatch
}
return nil
Expand All @@ -250,7 +250,7 @@ func (v *V) GetObject(firstParam interface{}, otherParams ...interface{}) (*V, e
if err != nil {
return nil, err
}
if ret.valueType != jsonparser.Object {
if ret.valueType != jsoniter.ObjectValue {
return nil, ErrTypeNotMatch
}
return ret, nil
Expand All @@ -264,7 +264,7 @@ func (v *V) GetArray(firstParam interface{}, otherParams ...interface{}) (*V, er
if err != nil {
return nil, err
}
if ret.valueType != jsonparser.Array {
if ret.valueType != jsoniter.ArrayValue {
return nil, ErrTypeNotMatch
}
return ret, nil
Expand Down
7 changes: 2 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module github.com/Andrew-M-C/go.jsonvalue

require (
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23
github.com/json-iterator/go v1.1.10
)
go 1.13

go 1.11
require github.com/json-iterator/go v1.1.10
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23 h1:D21IyuvjDCshj1/qq+pCNd3VZOAEI9jy6Bi131YlXgI=
github.com/buger/jsonparser v0.0.0-20181115193947-bf1c66bbce23/go.mod h1:bbYlZJ7hK1yFx9hf58LP0zeX7UjIGs20ufpu3evjr+s=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
12 changes: 6 additions & 6 deletions insert.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package jsonvalue

import (
"github.com/buger/jsonparser"
jsoniter "github.com/json-iterator/go"
)

// Insert type is for After() and Before() function. Please refer for realated function.
Expand Down Expand Up @@ -134,14 +134,14 @@ func (v *V) InsertArray() *Insert {
func (ins *Insert) Before(firstParam interface{}, otherParams ...interface{}) (*V, error) {
v := ins.v
c := ins.c
if v.valueType == jsonparser.NotExist {
if v.valueType == jsoniter.InvalidValue {
return nil, ErrValueUninitialized
}

// this is the last iteration
paramCount := len(otherParams)
if paramCount == 0 {
if v.valueType != jsonparser.Array {
if v.valueType != jsoniter.ArrayValue {
return nil, ErrNotArrayValue
}

Expand Down Expand Up @@ -187,17 +187,17 @@ func (ins *Insert) Before(firstParam interface{}, otherParams ...interface{}) (*
func (ins *Insert) After(firstParam interface{}, otherParams ...interface{}) (*V, error) {
v := ins.v
c := ins.c
if nil == v || v.valueType == jsonparser.NotExist {
if nil == v || v.valueType == jsoniter.InvalidValue {
return nil, ErrValueUninitialized
}
if nil == c || c.valueType == jsonparser.NotExist {
if nil == c || c.valueType == jsoniter.InvalidValue {
return nil, ErrValueUninitialized
}

// this is the last iteration
paramCount := len(otherParams)
if paramCount == 0 {
if v.valueType != jsonparser.Array {
if v.valueType != jsoniter.ArrayValue {
return nil, ErrNotArrayValue
}

Expand Down

0 comments on commit bea1e74

Please sign in to comment.