Skip to content

Commit

Permalink
提升json生成性能
Browse files Browse the repository at this point in the history
  • Loading branch information
EndeavorEcho committed Feb 14, 2022
1 parent 92fb3b4 commit 07ccaa8
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 8 deletions.
51 changes: 51 additions & 0 deletions builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package json_spanner

import (
"encoding/json"
"reflect"
"strings"
)

type Builder struct {
jsonMap map[string]interface{}
}

func NewBuilder() *Builder {
return &Builder{jsonMap: make(map[string]interface{})}
}

func (b *Builder) Set(key string, value interface{}) {
b.jsonMap = b.setInternal(key, b.jsonMap, value)
}

func (b *Builder) setInternal(key string, p map[string]interface{}, value interface{}) map[string]interface{} {
keys := strings.Split(key, ".")
if p == nil {
p = make(map[string]interface{})
}
if len(keys) == 1 {
p[keys[0]] = value
return p
} else {
if _, ok := p[keys[0]]; ok {
if reflect.TypeOf(p[keys[0]]) == reflect.TypeOf(p) {
p[keys[0]] = b.setInternal(strings.Join(keys[1:], "."), p[keys[0]].(map[string]interface{}), value)
return p
} else {
p[keys[0]] = make(map[string]interface{})
p[keys[0]] = b.setInternal(strings.Join(keys[1:], "."), p[keys[0]].(map[string]interface{}), value)
return p
}

} else {
p[keys[0]] = make(map[string]interface{})
p[keys[0]] = b.setInternal(strings.Join(keys[1:], "."), p[keys[0]].(map[string]interface{}), value)
return p
}
}
}

func (b *Builder) ToJson() string {
jsonBytes, _ := json.Marshal(b.jsonMap)
return string(jsonBytes)
}
17 changes: 17 additions & 0 deletions builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package json_spanner

import (
"fmt"
"testing"
)

func TestBuilder(t *testing.T) {
b := NewBuilder()
b.Set("a", 6)
b.Set("a.b", 3)
b.Set("a.d", 3)

b.Set("c", "xxx")
fmt.Println(b.ToJson())

}
21 changes: 13 additions & 8 deletions engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"github.com/fuhongbo/json_spanner/parser"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)

type Engine struct {
Expand Down Expand Up @@ -63,26 +62,32 @@ func (e *Engine) Transform(json string) (match bool, result string, err error) {

if e.valid(&jsonPath) {

resultJson := `{}`
b := NewBuilder()
//resultJson := `{}`

for _, item := range e.selectStatement.Fields {
switch item.Type {
case "NAME":
if item.Alias != "" {
resultJson, _ = sjson.Set(resultJson, item.Alias, jsonPath.Get(item.Name).Value())
b.Set(item.Alias, jsonPath.Get(item.Name).Value())
//resultJson, _ = sjson.Set(resultJson, item.Alias, jsonPath.Get(item.Name).Value())
} else {
resultJson, _ = sjson.Set(resultJson, item.Name, jsonPath.Get(item.Name).Value())
b.Set(item.Name, jsonPath.Get(item.Name).Value())
//resultJson, _ = sjson.Set(resultJson, item.Name, jsonPath.Get(item.Name).Value())
}
case "STRING":
resultJson, _ = sjson.Set(resultJson, item.Alias, item.ValueString)
b.Set(item.Alias, item.ValueString)
//resultJson, _ = sjson.Set(resultJson, item.Alias, item.ValueString)
case "FLOAT":
resultJson, _ = sjson.Set(resultJson, item.Alias, item.ValueFloat)
b.Set(item.Alias, item.ValueFloat)
//resultJson, _ = sjson.Set(resultJson, item.Alias, item.ValueFloat)
case "INT":
resultJson, _ = sjson.Set(resultJson, item.Alias, item.ValueInt)
b.Set(item.Alias, item.ValueInt)
//resultJson, _ = sjson.Set(resultJson,)
}
}

return true, resultJson, nil
return true, b.ToJson(), nil

} else {
return false, "", errors.New("Data does not match filter criteria . ")
Expand Down

0 comments on commit 07ccaa8

Please sign in to comment.