Oj: Oj.dump ignores :symbol_keys option

Created on 11 Dec 2012  Â·  3Comments  Â·  Source: ohler55/oj

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?

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:

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.

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kmasuda-aiming picture kmasuda-aiming  Â·  8Comments

dpaluy picture dpaluy  Â·  55Comments

werleo picture werleo  Â·  7Comments

skliew picture skliew  Â·  6Comments

ericmwalsh picture ericmwalsh  Â·  25Comments