tidwall / sjson

Set JSON values very quickly in Go
MIT License
2.4k stars 165 forks source link

Can't set numbered keys #44

Closed clarkmcc closed 3 years ago

clarkmcc commented 3 years ago

In gjson, I can get the value of a numbered key, for example

gjson.Get("{\"foo\":{\"1\": 10}}", "foo.1")

However, in sjson, because of the way array values are set, I can't set the same key. Is there a way to get around this?

Here is the reproduction script

package main

import (
    "fmt"
    "github.com/tidwall/sjson"
)

func main() {
    str, _ := sjson.Set("{}", "foo.bar", 10)
    fmt.Println(str) // {"foo":{"bar":10}}

    str, _ = sjson.Set("{}", "foo.1", 10)
    fmt.Println(str) // {"foo":[null,10]}
}
tidwall commented 3 years ago

The only way I can think of is by first assigning an object using the SetRaw function.

str, _ := sjson.SetRaw("{}", "foo", "{}")
fmt.Println(str) // {"foo":{}}

str, _ = sjson.Set(str, "foo.1", 10)
fmt.Println(str) // {"foo":{"1":10}}