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:
tidwall 2020-03-31 03:52:06 -07:00
parent bac348a61c
commit 4f7b016772
2 changed files with 17 additions and 5 deletions

View File

@ -341,12 +341,12 @@ func appendRawPaths(buf []byte, jstr string, paths []pathResult, raw string,
default:
return nil, &errorType{"json must be an object or array"}
case '{':
buf = append(buf, '{')
buf = appendBuild(buf, false, paths, raw, stringify)
buf = append(buf, jsres.Raw[:len(jsres.Raw)-1]...)
if comma {
buf = append(buf, ',')
}
buf = append(buf, jsres.Raw[1:]...)
buf = appendBuild(buf, false, paths, raw, stringify)
buf = append(buf, '}')
return buf, nil
case '[':
var appendit bool

View File

@ -7,6 +7,8 @@ import (
"testing"
"time"
"github.com/tidwall/pretty"
"github.com/tidwall/gjson"
)
@ -51,7 +53,14 @@ const (
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{}) {
t.Helper()
expect = sortJSON(expect)
var json2 string
var err error
switch kind {
@ -62,12 +71,14 @@ func testRaw(t *testing.T, kind int, expect, json, path string, value interface{
case setDelete:
json2, err = Delete(json, path)
}
if err != nil {
t.Fatal(err)
} else if json2 != expect {
}
json2 = sortJSON(json2)
if json2 != expect {
t.Fatalf("expected '%v', got '%v'", expect, json2)
}
var json3 []byte
switch kind {
default:
@ -77,6 +88,7 @@ func testRaw(t *testing.T, kind int, expect, json, path string, value interface{
case setDelete:
json3, err = DeleteBytes([]byte(json), path)
}
json3 = []byte(sortJSON(string(json3)))
if err != nil {
t.Fatal(err)
} else if string(json3) != expect {