Convert symbols to string when serializing with Oj.dump

While I was writing the question, I was able to find the answer. Since I cannot find any other answers on StackOverflow relating to this issue (specifically with regards to the Oj gem), I will keep this post in the hopes it will help others in my situation.

According to this previously discussed issue on GitHub, setting the option mode to :compat will indeed convert the symbols to strings. So my render line now looks like this:

render json: Oj.dump(example_hash, mode: :compat)

According to the Oj documentation for default_options, :compat mode is defined as follows:

...compatible with other systems. It will serialize any Object but will check to see if the Object implements a to_hash() or to_json() method. If either exists that method is used for serializing the Object. The to_hash() is more flexible and produces more consistent output so it has a preference over the to_json() method. If neither the to_json() or to_hash() methods exist then the Oj internal Object variable encoding is used.

So if I am interpreting that correctly, it seems this solution works because it is ultimately using the to_json method of the Hash class.

I am uncertain whether I have affected performance (either positively or negatively), but at least it saves me from having to manually invoke with_indifferent_access or to_json in the case of an array.

Update

In regards to performance, cmwright did some benchmarking, and came up with these results:

Rehearsal ----------------------------------------------
json        13.990000   0.250000  14.240000 ( 14.257051)
oj default   3.260000   0.230000   3.490000 (  3.491028)
oj compat    3.360000   0.240000   3.600000 (  3.593109)
------------------------------------ total: 21.330000sec

                 user     system      total        real
json        13.740000   0.240000  13.980000 ( 13.992641)
oj default   3.020000   0.230000   3.250000 (  3.248077)
oj compat    3.060000   0.230000   3.290000 (  3.286443)

Seems like the compat option is at least on par with the default Oj options, and significantly more efficient than plain 'ol to_json.

This is the gist containing the benchmark code.


Using the generate method gives you the same output.

Oj.generate({a: `test`})