forked from mirror/gjson
Added GopherJS support
This commit is contained in:
parent
081192fa2e
commit
5a96cfda70
|
@ -1,4 +1,4 @@
|
||||||
//+build appengine
|
//+build appengine js
|
||||||
|
|
||||||
package gjson
|
package gjson
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
//+build !appengine
|
//+build !appengine
|
||||||
|
//+build !js
|
||||||
|
|
||||||
package gjson
|
package gjson
|
||||||
|
|
||||||
|
@ -15,45 +16,40 @@ func getBytes(json []byte, path string) Result {
|
||||||
if json != nil {
|
if json != nil {
|
||||||
// unsafe cast to string
|
// unsafe cast to string
|
||||||
result = Get(*(*string)(unsafe.Pointer(&json)), path)
|
result = Get(*(*string)(unsafe.Pointer(&json)), path)
|
||||||
result = fromBytesGet(result)
|
// safely get the string headers
|
||||||
}
|
rawhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Raw))
|
||||||
return result
|
strhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Str))
|
||||||
}
|
// create byte slice headers
|
||||||
|
rawh := reflect.SliceHeader{Data: rawhi.Data, Len: rawhi.Len}
|
||||||
func fromBytesGet(result Result) Result {
|
strh := reflect.SliceHeader{Data: strhi.Data, Len: strhi.Len}
|
||||||
// safely get the string headers
|
if strh.Data == 0 {
|
||||||
rawhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Raw))
|
// str is nil
|
||||||
strhi := *(*reflect.StringHeader)(unsafe.Pointer(&result.Str))
|
if rawh.Data == 0 {
|
||||||
// create byte slice headers
|
// raw is nil
|
||||||
rawh := reflect.SliceHeader{Data: rawhi.Data, Len: rawhi.Len}
|
result.Raw = ""
|
||||||
strh := reflect.SliceHeader{Data: strhi.Data, Len: strhi.Len}
|
} else {
|
||||||
if strh.Data == 0 {
|
// raw has data, safely copy the slice header to a string
|
||||||
// str is nil
|
result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
|
||||||
if rawh.Data == 0 {
|
}
|
||||||
|
result.Str = ""
|
||||||
|
} else if rawh.Data == 0 {
|
||||||
// raw is nil
|
// raw is nil
|
||||||
result.Raw = ""
|
result.Raw = ""
|
||||||
} else {
|
// str has data, safely copy the slice header to a string
|
||||||
// raw has data, safely copy the slice header to a string
|
result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
|
||||||
|
} else if strh.Data >= rawh.Data &&
|
||||||
|
int(strh.Data)+strh.Len <= int(rawh.Data)+rawh.Len {
|
||||||
|
// Str is a substring of Raw.
|
||||||
|
start := int(strh.Data - rawh.Data)
|
||||||
|
// safely copy the raw slice header
|
||||||
result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
|
result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
|
||||||
|
// substring the raw
|
||||||
|
result.Str = result.Raw[start : start+strh.Len]
|
||||||
|
} else {
|
||||||
|
// safely copy both the raw and str slice headers to strings
|
||||||
|
result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
|
||||||
|
result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
|
||||||
}
|
}
|
||||||
result.Str = ""
|
|
||||||
} else if rawh.Data == 0 {
|
|
||||||
// raw is nil
|
|
||||||
result.Raw = ""
|
|
||||||
// str has data, safely copy the slice header to a string
|
|
||||||
result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
|
|
||||||
} else if strh.Data >= rawh.Data &&
|
|
||||||
int(strh.Data)+strh.Len <= int(rawh.Data)+rawh.Len {
|
|
||||||
// Str is a substring of Raw.
|
|
||||||
start := int(strh.Data - rawh.Data)
|
|
||||||
// safely copy the raw slice header
|
|
||||||
result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
|
|
||||||
// substring the raw
|
|
||||||
result.Str = result.Raw[start : start+strh.Len]
|
|
||||||
} else {
|
|
||||||
// safely copy both the raw and str slice headers to strings
|
|
||||||
result.Raw = string(*(*[]byte)(unsafe.Pointer(&rawh)))
|
|
||||||
result.Str = string(*(*[]byte)(unsafe.Pointer(&strh)))
|
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
|
@ -1361,7 +1361,7 @@ null
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNumUint64String(t *testing.T) {
|
func TestNumUint64String(t *testing.T) {
|
||||||
i := 9007199254740993 //2^53 + 1
|
var i int64 = 9007199254740993 //2^53 + 1
|
||||||
j := fmt.Sprintf(`{"data": [ %d, "hello" ] }`, i)
|
j := fmt.Sprintf(`{"data": [ %d, "hello" ] }`, i)
|
||||||
res := Get(j, "data.0")
|
res := Get(j, "data.0")
|
||||||
if res.String() != "9007199254740993" {
|
if res.String() != "9007199254740993" {
|
||||||
|
@ -1370,7 +1370,7 @@ func TestNumUint64String(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNumInt64String(t *testing.T) {
|
func TestNumInt64String(t *testing.T) {
|
||||||
i := -9007199254740993
|
var i int64 = -9007199254740993
|
||||||
j := fmt.Sprintf(`{"data":[ "hello", %d ]}`, i)
|
j := fmt.Sprintf(`{"data":[ "hello", %d ]}`, i)
|
||||||
res := Get(j, "data.1")
|
res := Get(j, "data.1")
|
||||||
if res.String() != "-9007199254740993" {
|
if res.String() != "-9007199254740993" {
|
||||||
|
@ -1388,7 +1388,7 @@ func TestNumBigString(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNumFloatString(t *testing.T) {
|
func TestNumFloatString(t *testing.T) {
|
||||||
i := -9007199254740993
|
var i int64 = -9007199254740993
|
||||||
j := fmt.Sprintf(`{"data":[ "hello", %d ]}`, i) //No quotes around value!!
|
j := fmt.Sprintf(`{"data":[ "hello", %d ]}`, i) //No quotes around value!!
|
||||||
res := Get(j, "data.1")
|
res := Get(j, "data.1")
|
||||||
if res.String() != "-9007199254740993" {
|
if res.String() != "-9007199254740993" {
|
||||||
|
|
Loading…
Reference in New Issue