完善gjson和gparser

This commit is contained in:
John
2018-01-23 16:40:48 +08:00
parent 8a59aea6a6
commit a66542e8fd
4 changed files with 69 additions and 56 deletions

View File

@ -52,7 +52,7 @@ func DecodeToJson (b []byte) (*Json, error) {
if v, err := Decode(b); err != nil {
return nil, err
} else {
return NewJson(&v), nil
return NewJson(v), nil
}
}
@ -95,12 +95,12 @@ func LoadContent (data []byte, t string) (*Json, error) {
if err := json.Unmarshal(data, &result); err != nil {
return nil, err
}
return NewJson(&result), nil
return NewJson(result), nil
}
// 将变量转换为Json对象进行处理该变量至少应当是一个map或者array否者转换没有意义
func NewJson(v *interface{}) *Json {
return &Json{ p: v }
func NewJson(v interface{}) *Json {
return &Json{ p: &v }
}
// 将指定的json内容转换为指定结构返回查找失败或者转换失败目标对象转换为nil
@ -135,7 +135,7 @@ func (j *Json) GetMap(pattern string) map[string]interface{} {
func (j *Json) GetJson(pattern string) *Json {
result := j.Get(pattern)
if result != nil {
return NewJson(&result)
return NewJson(result)
}
return nil
}
@ -179,7 +179,9 @@ func (j *Json) GetFloat64(pattern string) float64 {
}
// 根据pattern查找并设置数据
// 注意:写入的时候"."符号只能表示层级,不能使用带"."符号的键名
// 注意:
// 1、写入的时候"."符号只能表示层级,不能使用带"."符号的键名
// 2、写入的value为nil时表示删除
func (j *Json) Set(pattern string, value interface{}) error {
value = j.convertValue(value)
array := strings.Split(pattern, ".")
@ -194,7 +196,11 @@ func (j *Json) Set(pattern string, value interface{}) error {
switch (*pointer).(type) {
case map[string]interface{}:
if i == length - 1 {
(*pointer).(map[string]interface{})[array[i]] = value
if value == nil {
delete((*pointer).(map[string]interface{}), array[i])
} else {
(*pointer).(map[string]interface{})[array[i]] = value
}
} else {
v, ok := (*pointer).(map[string]interface{})[array[i]]
if !ok {
@ -212,14 +218,21 @@ func (j *Json) Set(pattern string, value interface{}) error {
if n, err := strconv.Atoi(array[i]); err == nil {
if i == length - 1 {
if cap((*pointer).([]interface{})) >= n {
(*pointer).([]interface{})[n] = value
if value == nil {
j.mu.Unlock()
return j.Set(strings.Join(array[0 : i], "."), append((*pointer).([]interface{})[ : n], (*pointer).([]interface{})[n + 1 : ]...))
} else {
(*pointer).([]interface{})[n] = value
}
} else {
// 注意这里产生了临时变量和赋值拷贝
s := make([]interface{}, n + 1)
copy(s, (*pointer).([]interface{}))
s[n] = value
j.mu.Unlock()
return j.Set(strings.Join(array[0 : i], "."), s)
if value != nil {
// 注意这里产生了临时变量和赋值拷贝
s := make([]interface{}, n + 1)
copy(s, (*pointer).([]interface{}))
s[n] = value
j.mu.Unlock()
return j.Set(strings.Join(array[0 : i], "."), s)
}
}
} else {
pointer = &(*pointer).([]interface{})[n]
@ -256,21 +269,38 @@ func (j *Json) convertValue(value interface{}) interface{} {
// 修改根节点数据
func (j *Json) setRoot(pattern string, value interface{}) error {
if *j.p == nil {
if isNumeric(pattern) {
*j.p = make([]interface{}, 0)
} else {
*j.p = make(map[string]interface{})
}
}
switch (*j.p).(type) {
case map[string]interface{}:
(*j.p).(map[string]interface{})[pattern] = value
if value == nil {
delete((*j.p).(map[string]interface{}), pattern)
} else {
(*j.p).(map[string]interface{})[pattern] = value
}
case []interface{}:
if isNumeric(pattern) {
if n, err := strconv.Atoi(pattern); err != nil {
return err
} else {
if cap((*j.p).([]interface{})) >= n {
(*j.p).([]interface{})[n] = value
if value == nil {
(*j.p) = append((*j.p).([]interface{})[ : n], (*j.p).([]interface{})[n + 1 : ]...)
} else {
(*j.p).([]interface{})[n] = value
}
} else {
// 注意这里产生了临时变量和赋值拷贝
array := (*j.p).([]interface{})
array = append(array, value)
*j.p = array
if value != nil {
// 注意这里产生了临时变量和赋值拷贝
array := (*j.p).([]interface{})
array = append(array, value)
*j.p = array
}
}
}
}

View File

@ -15,6 +15,10 @@ type File struct {
json *gjson.Json
}
func New () *File {
return &File{gjson.NewJson(nil)}
}
func Load (path string) (*File, error) {
if j, e := gjson.Load(path); e == nil {
return &File{j}, nil
@ -126,25 +130,25 @@ func (f *File) ToToml() ([]byte, error) {
}
func VarToXml(value interface{}, rootTag...string) ([]byte, error) {
return gjson.NewJson(&value).ToXml(rootTag...)
return gjson.NewJson(value).ToXml(rootTag...)
}
func VarToXmlIndent(value interface{}, rootTag...string) ([]byte, error) {
return gjson.NewJson(&value).ToXmlIndent(rootTag...)
return gjson.NewJson(value).ToXmlIndent(rootTag...)
}
func VarToJson(value interface{}) ([]byte, error) {
return gjson.NewJson(&value).ToJson()
return gjson.NewJson(value).ToJson()
}
func VarToJsonIndent(value interface{}) ([]byte, error) {
return gjson.NewJson(&value).ToJsonIndent()
return gjson.NewJson(value).ToJsonIndent()
}
func VarToYaml(value interface{}) ([]byte, error) {
return gjson.NewJson(&value).ToYaml()
return gjson.NewJson(value).ToYaml()
}
func VarToToml(value interface{}) ([]byte, error) {
return gjson.NewJson(&value).ToToml()
return gjson.NewJson(value).ToToml()
}

View File

@ -54,7 +54,7 @@ func testSet() {
if err != nil {
glog.Error(err)
} else {
j.Set("users.count", 2)
j.Set("users.count", 1)
j.Set("users.list", []string{"John", "小明"})
c, _ := j.ToJson()
fmt.Println(string(c))
@ -96,5 +96,5 @@ func testConvert() {
}
func main() {
testConvert()
testSet()
}

View File

@ -2,35 +2,14 @@ package main
import (
"fmt"
"gitee.com/johng/gf/g/encoding/gjson"
"gitee.com/johng/gf/g/os/glog"
"gitee.com/johng/gf/g/encoding/gparser"
)
func main() {
j, _ := gjson.Load("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/config.yml")
//c, _ := j.ToToml()
//fmt.Println(j.Get("database.default").([]interface{})[0])
fmt.Println(j.Get("database.default.0"))
return
data :=
`{
"users" : {
"count" : 100
}
}`
j, err := gjson.DecodeToJson([]byte(data))
if err != nil {
glog.Error(err)
} else {
//j.Set("users.count", 1)
j.Set("users.list", []string{"John", "小明"})
fmt.Println(j.Set("users.list.10", []string{"John", "小明10"}))
fmt.Println(j.Set("users.list.9", []string{"John", "小明9"}))
j.Set("users", "a")
//fmt.Println(j.Get("users.count"))
//fmt.Println(j.Get("users.count"))
fmt.Println(j.Get("users.list.10"))
c, _ := j.ToJson()
fmt.Println(string(c))
}
f := gparser.New()
f.Set("name", "john")
f.Set("name", "john2")
c, e := f.ToJson()
fmt.Println(e)
fmt.Println(string(c))
}