tidwall / sjson

Set JSON values very quickly in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't set numbered keys

clarkmcc opened this issue · comments

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]}
}

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}}