Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON object for just an integer

Tags:

json

ruby

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?

like image 220
Nils Avatar asked Dec 17 '22 09:12

Nils


1 Answers

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))
like image 133
a2800276 Avatar answered Jan 02 '23 06:01

a2800276