Skip to content

Commit

Permalink
trim space
Browse files Browse the repository at this point in the history
  • Loading branch information
seefan committed Apr 18, 2019
1 parent 5abd5e6 commit 7c054a1
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 34 deletions.
19 changes: 11 additions & 8 deletions jsonarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@
*/
package jsonreader

type jsonArray struct {
import "bytes"

type JsonArray struct {
reader
arr []JsonValue
}

func (j *jsonArray) Get(i int) JsonValue {
func (j *JsonArray) Get(i int) JsonValue {
return j.arr[i]
}
func (j *jsonArray) Size() int {
func (j *JsonArray) Size() int {
return len(j.arr)
}
func ParseJsonArray(bs []byte) *jsonArray {
j := &jsonArray{
func ParseJsonArray(bs []byte) *JsonArray {
j := &JsonArray{
reader: *newReader(bs),
}
j.parse()
return j
}
func (j *jsonArray) Each(f func(int, JsonValue)) {
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() {
func (j *JsonArray) parse() {
if !j.validArray() {
j.LastError = "JsonArray format error"
j.end = -1
Expand All @@ -40,6 +42,7 @@ func (j *jsonArray) parse() {
//remove []
j.index++
j.end--
j.skip()
start := j.index
str := 0
depth := 0
Expand All @@ -60,7 +63,7 @@ func (j *jsonArray) parse() {
if j.data[start] == '"' {
j.arr = append(j.arr, JsonValue(j.data[start+1:j.index-1]))
} else {
j.arr = append(j.arr, JsonValue(j.data[start:j.index]))
j.arr = append(j.arr, JsonValue(bytes.TrimSpace(j.data[start:j.index])))
}
start = j.index + 1
}
Expand Down
42 changes: 21 additions & 21 deletions jsonobject.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
/*
@Time : 2019-03-27 13:54
@Author : seefan
@File : jsonObject
@File : JsonObject
@Software: jsonreader
*/
package jsonreader

type jsonObject struct {
type JsonObject struct {
reader
val map[string]JsonValue
obj map[string]*jsonObject
arr map[string]*jsonArray
obj map[string]*JsonObject
arr map[string]*JsonArray
}

func (j *jsonObject) parse() {
func (j *JsonObject) parse() {
if !j.validObject() {
j.end = -1
return
Expand All @@ -40,70 +40,70 @@ func (j *jsonObject) parse() {
}
}

func ParseJsonObject(bs []byte) *jsonObject {
j := &jsonObject{
func ParseJsonObject(bs []byte) *JsonObject {
j := &JsonObject{
reader: *newReader(bs),
val: make(map[string]JsonValue),
}
j.parse()
return j
}
func (j *jsonObject) C(name string) bool {
func (j *JsonObject) C(name string) bool {
return j.Contains(name)
}
func (j *jsonObject) Contains(name string) bool {
func (j *JsonObject) Contains(name string) bool {
if _, ok := j.val[name]; ok {
return true
}
return false
}
func (j *jsonObject) V(name string) JsonValue {
func (j *JsonObject) V(name string) JsonValue {
return j.GetValue(name)
}
func (j *jsonObject) GetValue(name string) JsonValue {
func (j *JsonObject) GetValue(name string) JsonValue {
return j.val[name]
}
func (j *jsonObject) O(name string) *jsonObject {
func (j *JsonObject) O(name string) *JsonObject {
return j.GetObject(name)
}
func (j *jsonObject) GetObject(name string) *jsonObject {
func (j *JsonObject) GetObject(name string) *JsonObject {
if j.obj != nil {
if v, ok := j.obj[name]; ok {
return v
}
}
return j.GetObjectForce(name)
}
func (j *jsonObject) GetObjectForce(name string) *jsonObject {
func (j *JsonObject) GetObjectForce(name string) *JsonObject {
if v, ok := j.val[name]; ok {
if j.obj == nil {
j.obj = make(map[string]*jsonObject)
j.obj = make(map[string]*JsonObject)
}
j.obj[name] = ParseJsonObject(v.Bytes())
return j.obj[name]
}
return &jsonObject{
return &JsonObject{
val: make(map[string]JsonValue),
}
}
func (j *jsonObject) A(name string) *jsonArray {
func (j *JsonObject) A(name string) *JsonArray {
return j.GetArray(name)
}
func (j *jsonObject) GetArray(name string) *jsonArray {
func (j *JsonObject) GetArray(name string) *JsonArray {
if j.arr != nil {
if v, ok := j.arr[name]; ok {
return v
}
}
return j.GetArrayForce(name)
}
func (j *jsonObject) GetArrayForce(name string) *jsonArray {
func (j *JsonObject) GetArrayForce(name string) *JsonArray {
if v, ok := j.val[name]; ok {
if j.arr == nil {
j.arr = make(map[string]*jsonArray)
j.arr = make(map[string]*JsonArray)
}
j.arr[name] = ParseJsonArray(v.Bytes())
return j.arr[name]
}
return &jsonArray{}
return &JsonArray{}
}
4 changes: 2 additions & 2 deletions jsonvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ func (v JsonValue) Bytes() []byte {
func (v JsonValue) IsEmpty() bool {
return v == "" || v.IsNull()
}
func (v JsonValue) ParseJsonObject() *jsonObject {
func (v JsonValue) ParseJsonObject() *JsonObject {
return ParseJsonObject([]byte(v))
}
func (v JsonValue) ParseJsonArray() *jsonArray {
func (v JsonValue) ParseJsonArray() *JsonArray {
return ParseJsonArray([]byte(v))
}
8 changes: 5 additions & 3 deletions jsonvalue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ func TestJsonValue_Null(t *testing.T) {
}

func TestJsonValue_C(t *testing.T) {
obj := ParseJsonObject([]byte("{\"contacts\":{ \"company\":{ \"address\":null, \"state_code\":null }, \"employees\":[ ] }}"))
v := obj.GetValue("employees")
if v.IsNull() {
obj := ParseJsonObject([]byte("{\"contacts\":{ \"company\":{ \"address\":null, \"state_code\":null }, \"employees\":[ null] }}"))
v := obj.V("contacts").ParseJsonObject().A("employees")
if v.Size() == 0 {
t.Error(" parse error")
} else {
t.Log(v.Get(0).String(), v.Get(0).IsNull())
}
}

0 comments on commit 7c054a1

Please sign in to comment.