Closed mperham closed 7 months ago
The round trip isn't symmetric. How can I get symmetric dump/parse?
# current
> JSON.parse JSON.generate(:foo)
=> {"json_class"=>"Symbol", "s"=>"foo"}
# ideal
> JSON.parse JSON.generate(:foo)
=> :foo
Ah, ok:
> JSON.parse JSON.generate(:foo), create_additions: true
=> :foo
Still need support for Hash and recursive structures.
Looks like it's only an issue with Hash keys. Works with Arrays and Hash values.
> JSON.parse(JSON.generate([1, [:foo, "mike"]]), create_additions: true)
=> [1, [:foo, "mike"]]
> JSON.parse(JSON.generate({:foo => "mike", 123 => :bar}), create_additions: true)
=> {"foo"=>"mike", "123"=>:bar}
But unfortunately Hash keys are the main usecase for Symbols in method arguments.
I think I understand now. "JSON keys are strings, always on the left of the colon, and must be wrapped in double quotes". Converting a Symbol to a Hash is ok but it can't be used as a key in a larger Hash. The code would need to be modified to perform something similar to ActiveJob here:
This seems inconsistent and not useful. I want Symbols to serialize the same everywhere. Am I missing something?