tidwall / sjson

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

v1.2.0 breaks updating keys beginning with @ #53

Closed arigon closed 2 years ago

arigon commented 2 years ago

Hello

version 1.2.0+ which added "updates of complex paths" breaks updating keys beginning with the @ symbol.

Example:

data, err := sjson.SetBytes([]byte(`{"Test":123}`), "@timestamp", time.Now().Unix())
if err != nil {
    log.Fatal(err)
}

Current Result: {"Test":123}

Expected: {"Test":123,"@timestamp":1639152890}

Is there a workaround?

tidwall commented 2 years ago

Hi,

The @ symbol is one of the reserved character. The workaround is to escape it with a \ character.

// Using either one of these styles
sjson.SetBytes([]byte(`{"Test":123}`), "\\@timestamp", time.Now().Unix())
sjson.SetBytes([]byte(`{"Test":123}`), `\@timestamp`, time.Now().Unix())
arigon commented 2 years ago

Thank you!