jsonparser
jsonparser copied to clipboard
Deleting and Adding a Key corrupts the JSON
Hi,
If we delete a name & add it back, the JSON is corrupted. Its Partially formed and values at the end of the JSON are truncated.
Code:
package main
import (
"fmt"
"github.com/buger/jsonparser"
)
var jsonByteGlobal []byte = []byte(
`{
"person": {
"fullName": "Leonid Bugaev"
},
"company": {
"name": "Acme"
}
}`)
func main() {
jsonByteArray := jsonByteGlobal
jsonByteArray = jsonparser.Delete(jsonByteArray, "person", "fullName")
fmt.Printf("After Deletion : %s\n", string(jsonByteArray))
jsonparser.Set(jsonByteArray, []byte(`"Kishore Bandi"`), "person", "fullName")
fmt.Printf("After Addition : %s\n", string(jsonByteArray))
}
Output:
After Deletion : {
"person": {
},
"company": {
"name": "Acme"
}
}
After Addition : {
"person": {"fullName":"Kishore Bandi"},
"company": {
In above output, the company part of the JSON got truncated when we tried to add "person"."fullName"
Playground : https://play.golang.org/p/y4cE3UaGoR7
Thanks for reporting. I'll take a look