tidwall / sjson

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

Feature request: Set a value with an object/array #37

Closed christoth closed 4 years ago

christoth commented 4 years ago

Would it be possible to set a value to an object/array? So don't escape the string value if it starts with "{" | "[" and ends with "}" | "]".

value, _ := sjson.Set(`{"name":"Andy"}`, "friends", `["Carol","Sara"]')
println(value)

// Output:
// {"name":"Andy","friends":["Carol","Sara"]}
value, _ := sjson.Set(`{"name":"Andy"}`, "meta", `{"gender":"M","age":42}`)
println(value)

// Output:
// {"name":"Andy","meta":{"gender":"M","age":42}}

Or append an object/array to an existing array?

value, _ := sjson.Set(`{"friends":[{"name":"Andy","age": 8},{"name":"Carol","age":7}]}`, "friends.-1", `{"name":"Sara","age":8}`)
println(value)

// Output:
// {"friends":[{"name":"Andy","age": 8},{"name":"Carol","age":7},{"name":"Sara","age":8}]}

Thanks in advance!

tidwall commented 4 years ago

I think the SetRaw function might be what you are looking for.

christoth commented 4 years ago

That worked perfectly, thank you!

Would you like me to create a PR to add SetRaw to the readme doc?