When dumping a hash, the :symbol_keys option does not seem to have any effect when passed as false, example:
require 'oj'
# notice the ':' prefix of they key
puts Oj.dump({:abc => 123}, :symbol_keys => false)
#=> {":abc":123}
# same result
puts Oj.dump({:abc => 123}, :symbol_keys => true)
#=> {":abc":123}
Is this expected behavior?
That is expected behavior. The symbolize_keys option only applies to loading.
What is your recommendation if I have a hash with symbol keys, that I want dumped to JSON without the colon prefixes? I can recursively stringify-keys on the hash, but that does not seem like it would be very performant. Thoughts?
There are 4 modes OJ can use for dumping. This is to help people find the right level of conversion from Ruby to JSON. The :compat mode will convert symbols to strings. Try this
Oj.dump({a:1,b:2}, mode: :compat)
On Dec 12, 2012, at 3:21 AM, Trevor Rowe [email protected] wrote:
What is your recommendation if I have a hash with symbol keys, that I want dumped to JSON without the colon prefixes? I can recursively stringify-keys on the hash, but that does not seem like it would be very performant. Thoughts?
—
Reply to this email directly or view it on GitHub.
Most helpful comment
There are 4 modes OJ can use for dumping. This is to help people find the right level of conversion from Ruby to JSON. The :compat mode will convert symbols to strings. Try this
Oj.dump({a:1,b:2}, mode: :compat)
On Dec 12, 2012, at 3:21 AM, Trevor Rowe [email protected] wrote: