forked from mirror/sjson
Append new members to objects
The Set operation now appends key values to objects when that key previously did not exist. Before the key value was prepended. Close #35
This commit is contained in:
parent
bac348a61c
commit
4f7b016772
6
sjson.go
6
sjson.go
|
@ -341,12 +341,12 @@ func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string,
|
||||||
default:
|
default:
|
||||||
return nil, &errorType{"json must be an object or array"}
|
return nil, &errorType{"json must be an object or array"}
|
||||||
case '{':
|
case '{':
|
||||||
buf = append(buf, '{')
|
buf = append(buf, jsres.Raw[:len(jsres.Raw)-1]...)
|
||||||
buf = appendBuild(buf, false, paths, raw, stringify)
|
|
||||||
if comma {
|
if comma {
|
||||||
buf = append(buf, ',')
|
buf = append(buf, ',')
|
||||||
}
|
}
|
||||||
buf = append(buf, jsres.Raw[1:]...)
|
buf = appendBuild(buf, false, paths, raw, stringify)
|
||||||
|
buf = append(buf, '}')
|
||||||
return buf, nil
|
return buf, nil
|
||||||
case '[':
|
case '[':
|
||||||
var appendit bool
|
var appendit bool
|
||||||
|
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/tidwall/pretty"
|
||||||
|
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -51,7 +53,14 @@ const (
|
||||||
setDelete = 6
|
setDelete = 6
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func sortJSON(json string) string {
|
||||||
|
opts := pretty.Options{SortKeys: true}
|
||||||
|
return string(pretty.Ugly(pretty.PrettyOptions([]byte(json), &opts)))
|
||||||
|
}
|
||||||
|
|
||||||
func testRaw(t *testing.T, kind int, expect, json, path string, value interface{}) {
|
func testRaw(t *testing.T, kind int, expect, json, path string, value interface{}) {
|
||||||
|
t.Helper()
|
||||||
|
expect = sortJSON(expect)
|
||||||
var json2 string
|
var json2 string
|
||||||
var err error
|
var err error
|
||||||
switch kind {
|
switch kind {
|
||||||
|
@ -62,12 +71,14 @@ func testRaw(t *testing.T, kind int, expect, json, path string, value interface{
|
||||||
case setDelete:
|
case setDelete:
|
||||||
json2, err = Delete(json, path)
|
json2, err = Delete(json, path)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
} else if json2 != expect {
|
}
|
||||||
|
json2 = sortJSON(json2)
|
||||||
|
if json2 != expect {
|
||||||
t.Fatalf("expected '%v', got '%v'", expect, json2)
|
t.Fatalf("expected '%v', got '%v'", expect, json2)
|
||||||
}
|
}
|
||||||
|
|
||||||
var json3 []byte
|
var json3 []byte
|
||||||
switch kind {
|
switch kind {
|
||||||
default:
|
default:
|
||||||
|
@ -77,6 +88,7 @@ func testRaw(t *testing.T, kind int, expect, json, path string, value interface{
|
||||||
case setDelete:
|
case setDelete:
|
||||||
json3, err = DeleteBytes([]byte(json), path)
|
json3, err = DeleteBytes([]byte(json), path)
|
||||||
}
|
}
|
||||||
|
json3 = []byte(sortJSON(string(json3)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
} else if string(json3) != expect {
|
} else if string(json3) != expect {
|
||||||
|
|
Loading…
Reference in New Issue