tidwall / sjson

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

Setting possibly escaped keys #26

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi, I am storing changes made to documents in key-value format(https://github.com/tidwall/gjson/issues/103) and I have an issue with escaped keys.

If I call set foo.bar.baz 1 I will store this change as {"foo.bar.baz": 1} but I am not able to store changes if I call set foo\.bar\.baz 1 which should result into {"foo\.bar\.baz": 1} because keys do not have option to set raw value, like values do.

So far the only solutions that come to mind are:

... but I would prefer avoiding workarounds with something like SetOptions()

dipakw commented 5 years ago

Is this still the problem? Did you try SetRaw method?

ghost commented 5 years ago

Doesn't seem like it. For keys where I have no control over their format I use

func escape(str string) string {
    return strings.Replace(`.`, `\.`, str, -1)
}

I haven't done proper testing but I think this is no longer an issue.