mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 03:07:45 +08:00
修正gjson解析bug,初步完成框架手册
This commit is contained in:
parent
a9a1a0e750
commit
48b4d25362
20
README.MD
20
README.MD
@ -35,14 +35,14 @@ func main() {
|
||||
* [TCPServer](https://www.kancloud.cn/johng/gf/494382)
|
||||
* [UDPServer](https://www.kancloud.cn/johng/gf/494383)
|
||||
* [功能模块设计](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [缓存模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [日志模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [时间模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [JSON模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [命令行模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [缓存模块](https://www.kancloud.cn/johng/gf/494385)
|
||||
* [日志模块](https://www.kancloud.cn/johng/gf/494386)
|
||||
* [时间模块](https://www.kancloud.cn/johng/gf/494387)
|
||||
* [JSON模块](https://www.kancloud.cn/johng/gf/494388)
|
||||
* [命令行模块](https://www.kancloud.cn/johng/gf/494389)
|
||||
* [HTTP客户端](https://www.kancloud.cn/johng/gf/499674)
|
||||
* [环境变量模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [文件管理模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [并发安全容器](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [通用编码模块](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [其他模块介绍](https://www.kancloud.cn/johng/gf/494384)
|
||||
* [环境变量模块](https://www.kancloud.cn/johng/gf/494390)
|
||||
* [文件管理模块](https://www.kancloud.cn/johng/gf/494391)
|
||||
* [并发安全容器](https://www.kancloud.cn/johng/gf/494392)
|
||||
* [通用编码模块](https://www.kancloud.cn/johng/gf/494393)
|
||||
* [其他模块介绍](https://www.kancloud.cn/johng/gf/494394)
|
||||
|
@ -1,115 +0,0 @@
|
||||
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://gitee.com/johng/gf.
|
||||
//
|
||||
|
||||
package gset
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Int64Set struct {
|
||||
sync.RWMutex
|
||||
M map[int64]struct{}
|
||||
}
|
||||
|
||||
func NewInt64Set() *Int64Set {
|
||||
return &Int64Set{M: make(map[int64]struct{})}
|
||||
}
|
||||
|
||||
// 设置键
|
||||
func (this *Int64Set) Add(item int64) *Int64Set {
|
||||
if this.Contains(item) {
|
||||
return this
|
||||
}
|
||||
this.Lock()
|
||||
this.M[item] = struct{}{}
|
||||
this.Unlock()
|
||||
return this
|
||||
}
|
||||
|
||||
// 批量添加设置键
|
||||
func (this *Int64Set) BatchAdd(items []int64) *Int64Set {
|
||||
count := len(items)
|
||||
if count == 0 {
|
||||
return this
|
||||
}
|
||||
|
||||
todo := make([]int64, 0, count)
|
||||
this.RLock()
|
||||
for i := 0; i < count; i++ {
|
||||
_, exists := this.M[items[i]]
|
||||
if exists {
|
||||
continue
|
||||
}
|
||||
|
||||
todo = append(todo, items[i])
|
||||
}
|
||||
this.RUnlock()
|
||||
|
||||
count = len(todo)
|
||||
if count == 0 {
|
||||
return this
|
||||
}
|
||||
|
||||
this.Lock()
|
||||
for i := 0; i < count; i++ {
|
||||
this.M[todo[i]] = struct{}{}
|
||||
}
|
||||
this.Unlock()
|
||||
return this
|
||||
}
|
||||
|
||||
// 键是否存在
|
||||
func (this *Int64Set) Contains(item int64) bool {
|
||||
this.RLock()
|
||||
_, exists := this.M[item]
|
||||
this.RUnlock()
|
||||
return exists
|
||||
}
|
||||
|
||||
// 删除键值对
|
||||
func (this *Int64Set) Remove(key int64) {
|
||||
this.Lock()
|
||||
delete(this.M, key)
|
||||
this.Unlock()
|
||||
}
|
||||
|
||||
// 大小
|
||||
func (this *Int64Set) Size() int {
|
||||
this.RLock()
|
||||
l := len(this.M)
|
||||
this.RUnlock()
|
||||
return l
|
||||
}
|
||||
|
||||
// 清空set
|
||||
func (this *Int64Set) Clear() {
|
||||
this.Lock()
|
||||
this.M = make(map[int64]struct{})
|
||||
this.Unlock()
|
||||
}
|
||||
|
||||
// 转换为数组
|
||||
func (this *Int64Set) Slice() []int64 {
|
||||
this.RLock()
|
||||
ret := make([]int64, len(this.M))
|
||||
i := 0
|
||||
for item := range this.M {
|
||||
ret[i] = item
|
||||
i++
|
||||
}
|
||||
|
||||
this.RUnlock()
|
||||
return ret
|
||||
}
|
||||
|
||||
// 转换为字符串
|
||||
func (this *Int64Set) String() string {
|
||||
s := this.Slice()
|
||||
return fmt.Sprint(s)
|
||||
}
|
@ -32,7 +32,7 @@ func Encode (v interface{}) ([]byte, error) {
|
||||
// 解码字符串为interface{}变量
|
||||
func Decode (b []byte) (interface{}, error) {
|
||||
var v interface{}
|
||||
if err := DecodeTo(b, &v); err == nil {
|
||||
if err := DecodeTo(b, &v); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return v, nil
|
||||
@ -47,9 +47,9 @@ func DecodeTo (b []byte, v interface{}) error {
|
||||
// 解析json字符串为gjson.Json对象,并返回操作对象指针
|
||||
func DecodeToJson (b []byte) (*Json, error) {
|
||||
if v, err := Decode(b); err != nil {
|
||||
return &Json{&v}, nil
|
||||
} else {
|
||||
return nil, err
|
||||
} else {
|
||||
return &Json{&v}, nil
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,111 +2,24 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
//"encoding/json"
|
||||
"gitee.com/johng/gf/g/os/glog"
|
||||
"gitee.com/johng/gf/g/encoding/gjson"
|
||||
)
|
||||
|
||||
type City struct {
|
||||
Age string
|
||||
CityId int
|
||||
CityName string
|
||||
ProvinceId int
|
||||
//CityOrder int
|
||||
}
|
||||
|
||||
func main() {
|
||||
//data := `[{"CityId":1, "CityName":"北京", "ProvinceId":1, "CityOrder":1}, {"CityId":5, "CityName":"成都", "ProvinceId":27, "CityOrder":1}]`
|
||||
data := `{"name":"中国","age":31,"list":[["a","b","c"],["d","e","f"]],"items":{"title":"make\"he moon","name":"make'he moon","content":"'[}]{[}he moon"}}`
|
||||
//data := `[{"CityId":18,"CityName":"西安","ProvinceId":27,"CityOrder":1},{"CityId":53,"CityName":"广州","ProvinceId":27,"CityOrder":1}]`
|
||||
//data := `{"name" : "中国", "age" : 31, "items":[1,2,3]}`
|
||||
//data := `[["a","b","c"],["d","e","f"]]`
|
||||
//data := `["a","b","c"]`
|
||||
//json := `
|
||||
//[1,{"a":2},
|
||||
//{"a":{}},
|
||||
//{"a":[]},
|
||||
//{"a":[{}]},
|
||||
//{"{[a" : "\"2,:3," a ":33}]"}]` // 错误的json
|
||||
//data := `["a","b","c"` // 错误的json
|
||||
//data := `,{ "name" : "中国", "age" : 31, "items":[1,2]:}` //错误的json
|
||||
|
||||
v := gjson.DecodeToJson(&data)
|
||||
fmt.Println(v.GetNumber("list"))
|
||||
|
||||
//v := map[string]interface{} {
|
||||
//
|
||||
// "name" : "中国",
|
||||
// "age" : 11,
|
||||
// "list" : []interface{} {
|
||||
// 1,2,3,4,
|
||||
// },
|
||||
//}
|
||||
//r, _ := json.MarshalIndent(v, "", "\t")
|
||||
//fmt.Println(string(r))
|
||||
//s, _ := gjson.Encode(v)
|
||||
//fmt.Println(*s)
|
||||
|
||||
|
||||
//p, err := gjson.Decode(&data)
|
||||
//if err == nil {
|
||||
// //p.Print()
|
||||
// //fmt.Println(p.Get("0"))
|
||||
// fmt.Println(p.GetMap("0"))
|
||||
//} else {
|
||||
// fmt.Println(err)
|
||||
//}
|
||||
//fmt.Println()
|
||||
//fmt.Println()
|
||||
////v := make(map[string]interface{})
|
||||
////i := 31
|
||||
////j := "john"
|
||||
////v["age"] = i
|
||||
////v["name"] = make(map[string]interface{})
|
||||
////t := v["name"]
|
||||
////t.(map[string]interface{})["n"] = j
|
||||
////
|
||||
////fmt.Println(v)
|
||||
//var s struct{
|
||||
// v interface{}
|
||||
// p interface{}
|
||||
//}
|
||||
//v := make(map[string]interface{})
|
||||
//s.v = v
|
||||
//s.p = &v
|
||||
//c := (*s.p.(*map[string]interface{}))
|
||||
//c["name1"] = "john1"
|
||||
//
|
||||
//t := make(map[string]interface{})
|
||||
//c["/"] = t
|
||||
//s.p = &t
|
||||
//t["name2"] = "john2"
|
||||
//
|
||||
//c2 := (*s.p.(*map[string]interface{}))
|
||||
//c2["name3"] = "john3"
|
||||
//
|
||||
////t2[2] = 100
|
||||
//fmt.Println(s.v)
|
||||
|
||||
|
||||
//a := map[string]interface{} {
|
||||
// "name" : "john",
|
||||
// "list" : []interface{}{
|
||||
// 1,2,3, "fuck",
|
||||
// },
|
||||
// "item" : map[string]string {
|
||||
// "n1" : "v1",
|
||||
// "n2" : "v2",
|
||||
// "n3" : "v3",
|
||||
// },
|
||||
//}
|
||||
//fmt.Println(json.M)
|
||||
|
||||
//
|
||||
//var a = []int{1,2,3}
|
||||
//var b = []int{4,5,6, 7,8}
|
||||
//cc := make([]int, len(a) + 12)
|
||||
//a = cc
|
||||
//copy(a, b)
|
||||
//fmt.Println(a)
|
||||
//fmt.Println(b)
|
||||
data := `{
|
||||
"users" : {
|
||||
"count" : 100,
|
||||
"list" : [
|
||||
{"name" : "小明", "score" : 60},
|
||||
{"name" : "John", "score" : 99.5}
|
||||
]
|
||||
}
|
||||
}`
|
||||
j, err := gjson.DecodeToJson([]byte(data))
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
} else {
|
||||
fmt.Println("John Score:", j.GetFloat32("users.list.1.score"))
|
||||
}
|
||||
}
|
@ -1,19 +1,9 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/encoding/gurl"
|
||||
"gitee.com/johng/gf/g/os/glog"
|
||||
)
|
||||
|
||||
|
||||
type T struct {
|
||||
name string
|
||||
}
|
||||
|
||||
func (t *T)Test() {
|
||||
fmt.Println(t.name)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(gurl.Encode("@123"))
|
||||
glog.Error("发生错误!")
|
||||
}
|
Loading…
Reference in New Issue
Block a user