-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
451 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
@Time : 2019-03-27 18:27 | ||
@Author : seefan | ||
@File : jsonarr | ||
@Software: jsonreader | ||
*/ | ||
package jsonreader | ||
|
||
type JsonArray struct { | ||
reader | ||
arr []JsonValue | ||
} | ||
|
||
func (j *JsonArray) Get(i int) JsonValue { | ||
return j.arr[i] | ||
} | ||
func (j *JsonArray) Size() int { | ||
return len(j.arr) | ||
} | ||
func ParseJsonArray(bs []byte) *JsonArray { | ||
j := &JsonArray{ | ||
reader: *newReader(bs), | ||
} | ||
j.parse() | ||
return j | ||
} | ||
func (j *JsonArray) each(f func(int, JsonValue)) { | ||
if j.arr != nil { | ||
for i, v := range j.arr { | ||
f(i, v) | ||
} | ||
} | ||
} | ||
func (j *JsonArray) parse() { | ||
if !j.validArray() { | ||
j.end = -1 | ||
return | ||
} | ||
//remove [] | ||
j.start++ | ||
j.end-- | ||
start := j.start | ||
str := false | ||
depth := 0 | ||
for j.start <= j.end { | ||
switch j.data[j.start] { | ||
case '[', '{': | ||
depth++ | ||
case ']', '}': | ||
depth-- | ||
case '"': | ||
str = !str | ||
case ',': | ||
if depth == 0 { | ||
j.arr = append(j.arr, JsonValue(j.data[start:j.start])) | ||
start = j.start + 1 | ||
} | ||
} | ||
j.start++ | ||
} | ||
if start < j.start { | ||
j.arr = append(j.arr, JsonValue(j.data[start:j.start])) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
@Time : 2019-03-27 13:54 | ||
@Author : seefan | ||
@File : JsonObject | ||
@Software: jsonreader | ||
*/ | ||
package jsonreader | ||
|
||
type JsonObject struct { | ||
reader | ||
val map[string]JsonValue | ||
obj map[string]*JsonObject | ||
arr map[string]*JsonArray | ||
} | ||
|
||
func (j *JsonObject) parse() { | ||
if !j.validObject() { | ||
j.end = -1 | ||
return | ||
} | ||
|
||
//remove {} | ||
j.start++ | ||
j.end-- | ||
for j.start <= j.end { | ||
j.skip() | ||
if !j.IsValid() { | ||
break | ||
} | ||
key := j.parseString() | ||
j.skipSplit() | ||
if !j.IsValid() { | ||
break | ||
} | ||
value := j.parseValue() | ||
j.val[string(key)] = JsonValue(value) | ||
if !j.hasMore() { | ||
break | ||
} | ||
} | ||
} | ||
|
||
func ParseJsonObject(bs []byte) *JsonObject { | ||
j := &JsonObject{ | ||
reader: *newReader(bs), | ||
val: make(map[string]JsonValue), | ||
} | ||
j.parse() | ||
return j | ||
} | ||
func (j *JsonObject) GetJsonValue(name string) JsonValue { | ||
if v, ok := j.val[name]; ok { | ||
return v | ||
} | ||
return "name not exists" | ||
} | ||
func (j *JsonObject) GetJsonObject(name string) *JsonObject { | ||
if j.obj != nil { | ||
if v, ok := j.obj[name]; ok { | ||
return v | ||
} | ||
} | ||
return j.GetJsonObjectForce(name) | ||
} | ||
func (j *JsonObject) GetJsonObjectForce(name string) *JsonObject { | ||
if v, ok := j.val[name]; ok { | ||
if j.obj == nil { | ||
j.obj = make(map[string]*JsonObject) | ||
} | ||
j.obj[name] = ParseJsonObject(v.Bytes()) | ||
return j.obj[name] | ||
} | ||
return &JsonObject{ | ||
val: make(map[string]JsonValue), | ||
} | ||
} | ||
func (j *JsonObject) GetJsonArray(name string) *JsonArray { | ||
if j.arr != nil { | ||
if v, ok := j.arr[name]; ok { | ||
return v | ||
} | ||
} | ||
return j.GetJsonArrayForce(name) | ||
} | ||
func (j *JsonObject) GetJsonArrayForce(name string) *JsonArray { | ||
if v, ok := j.val[name]; ok { | ||
if j.arr == nil { | ||
j.arr = make(map[string]*JsonArray) | ||
} | ||
j.arr[name] = ParseJsonArray(v.Bytes()) | ||
return j.arr[name] | ||
} | ||
return &JsonArray{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/* | ||
@Time : 2019-03-26 16:36 | ||
@Author : seefan | ||
@File : jsonreader | ||
@Software: jsonreader | ||
*/ | ||
package jsonreader | ||
|
||
import ( | ||
"strconv" | ||
"time" | ||
) | ||
|
||
//扩展值,原始类型为 string | ||
type JsonValue string | ||
|
||
//返回 string 的值 | ||
func (v JsonValue) String() string { | ||
return string(v) | ||
} | ||
|
||
//返回 int64 的值 | ||
func (v JsonValue) Int64() int64 { | ||
if f, e := strconv.ParseInt(string(v), 10, 64); e == nil { | ||
return f | ||
} | ||
return 0 | ||
} | ||
|
||
//返回 int32 的值 | ||
func (v JsonValue) Int32() int32 { | ||
return int32(v.Int64()) | ||
} | ||
|
||
//返回 int16 的值 | ||
func (v JsonValue) Int16() int16 { | ||
return int16(v.Int64()) | ||
} | ||
|
||
//返回 int8 的值 | ||
func (v JsonValue) Int8() int8 { | ||
return int8(v.Int64()) | ||
} | ||
|
||
//返回 int 的值 | ||
func (v JsonValue) Int() int { | ||
return int(v.Int64()) | ||
} | ||
|
||
//返回 uint64 的值 | ||
func (v JsonValue) UInt64() uint64 { | ||
if f, e := strconv.ParseUint(string(v), 10, 64); e == nil { | ||
return f | ||
} | ||
return 0 | ||
} | ||
|
||
//返回 uint32 类型的值 | ||
func (v JsonValue) UInt32() uint32 { | ||
return uint32(v.UInt64()) | ||
} | ||
|
||
//返回 uint16 类型的值 | ||
func (v JsonValue) UInt16() uint16 { | ||
return uint16(v.UInt64()) | ||
} | ||
|
||
//返回 uint8 类型的值 | ||
func (v JsonValue) UInt8() uint8 { | ||
return uint8(v.UInt64()) | ||
} | ||
|
||
//返回 byte 类型的值 | ||
func (v JsonValue) Byte() byte { | ||
return v.UInt8() | ||
} | ||
|
||
//返回 uint 类型的值 | ||
func (v JsonValue) UInt() uint { | ||
return uint(v.UInt64()) | ||
} | ||
|
||
//返回 float64 类型的值 | ||
func (v JsonValue) Float64() float64 { | ||
if f, e := strconv.ParseFloat(string(v), 64); e == nil { | ||
return f | ||
} | ||
return 0 | ||
} | ||
|
||
//返回 float32 类型的值 | ||
func (v JsonValue) Float32() float32 { | ||
return float32(v.Float64()) | ||
} | ||
|
||
//返回 bool 类型的值 | ||
func (v JsonValue) Bool() bool { | ||
return v == "1" | ||
} | ||
|
||
//返回 time.Time 类型的值 | ||
func (v JsonValue) Time() time.Time { | ||
return time.Unix(v.Int64(), 0) | ||
} | ||
|
||
//返回 time.Duration 类型的值 | ||
func (v JsonValue) Duration() time.Duration { | ||
return time.Duration(v.Int64()) | ||
} | ||
|
||
//返回 []byte 类型的值 | ||
func (v JsonValue) Bytes() []byte { | ||
return []byte(v) | ||
} | ||
|
||
//判断是否为空 | ||
func (v JsonValue) IsEmpty() bool { | ||
return v == "" | ||
} | ||
func (v JsonValue) JsonObject() *JsonObject { | ||
return ParseJsonObject([]byte(v)) | ||
} | ||
func (v JsonValue) JsonArray() *JsonArray { | ||
return ParseJsonArray([]byte(v)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
@Time : 2019-03-26 17:20 | ||
@Author : seefan | ||
@File : jsonreader_test.go | ||
@Software: jsonreader | ||
*/ | ||
package jsonreader | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestJsonValue_GetValue(t *testing.T) { | ||
r := ParseJsonObject([]byte(" {\"data\" : {\"key\" : 123,\"abc\":-1021e5 } , \"value\":5 ,\"ars\":[1,2,3,4,{\"value\":5},\"6\",{\"value\":7}]}")) | ||
t.Log(r.GetJsonObject("data").GetJsonValue("abc").String()) | ||
|
||
r.GetJsonArray("ars").each(func(i int, value JsonValue) { | ||
t.Log(i, value) | ||
}) | ||
arr := ParseJsonArray([]byte("[{\"data\" : {\"key\" : 123,\"abc\":-1021e5 } , \"value\":5 ,\"ars\":[1,2,3,4,{\"value\":5},\"6\",{\"value\":7}]},0,1,\"sfdada\"]")) | ||
arr.each(func(i int, value JsonValue) { | ||
if i == 0 { | ||
t.Log(value.JsonObject().GetJsonObject("data").GetJsonValue("key")) | ||
} | ||
t.Log(i, value) | ||
}) | ||
} |
Oops, something went wrong.