tidwall / sjson

Set JSON values very quickly in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Setting possibly escaped keys

opened this issue · comments

Hi,
I am storing changes made to documents in key-value format(tidwall/gjson#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:

  • using map[string]interface{} for the changes which I would have to convert to json later on, avoiding sjson for changes completely(currently the least intrusive solution)
  • base64-encoding the keys
  • using different delimiter on insert and then replacing it with dot upon parsing

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

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

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.