Nils Nils - 6 months ago 10
Ruby Question

JSON object for just an integer

Silly question, but I'm unable to figure out..

I tried the following in Ruby:

irb(main):020:0> JSON.load('[1,2,3]').class
=> Array


This seems to work. While neither

JSON.load('1').class


nor this

JSON.load('{1}').class


works. Any ideas?

Answer

I'd ask the guys who programmed the library. AFAIK, 1 isn't a valid JSON object, and neither is {1} but 1 is what the library itself generates for the fixnum 1.

You'd need to do: {"number" : 1} to be valid json. The bug is that

a != JSON.parse(JSON.generate(a))