title: How to make Rails 3 JSON parse doubly quoted strings and single number
body: <h1>Background</h1>
  
  <p>On the <a href="http://www.json.org/" rel="noreferrer">json.org</a> website, a string is defined as <code>"char+"</code> where <code>char+</code> is one or more <code>char</code>. A <code>char</code> is any unicode character except <code>"</code> or <code>\</code>. A subset of control characters are allowed, just escape them: </p>
  
  <p><code>"foo"</code> <code>"2"</code> <code>"\\"</code></p>
  
  <p>In Javascript, if you want to parse a string, it needs to be enclosed:</p>
  
  <p><code>"\"foo\""</code> or <code>'"foo"'</code>, but not <code>"'foo'"</code></p>
  
  <p>In Rails 3, the <a href="http://flori.github.io/json/" rel="noreferrer">JSON gem</a> that runs C or pure Ruby code is default.</p>
  
  <hr>
  
  <p>As per the accepted answer, the gem parses JSON documents rather than elements. A document is either a collection in the form of key, value (object/hash) or values (array).</p>
  
  <p><br></p>
  
  <h1>The problem</h1>
  
  <h2>Strings</h2>
  
  <p>Let's say we want to parse the string <code>foo</code>, We would need to enclose it as <code>"\"foo\""</code> or <code>'"foo"'</code></p>
  
  <pre><code>JSON.parse("\"foo\"")
  JSON.parse('"foo"')
  </code></pre>
  
  <p>yield</p>
  
  <pre><code>JSON::ParserError unexpected token at '"foo"' 
  </code></pre>
  
  <p>meaning it can't parse <code>"foo"</code></p>
  
  <h2>Numbers</h2>
  
  <p>The same goes for numbers: <code>'3'</code> or <code>"3"</code> will yield <code>Needs at least two octets</code>. 
  Larger numbers ( an octet is a byte, so two utf8 characters are two bytes ): <code>'42'</code> or <code>"42"</code> simply yield the same <code>JSON::ParserError unexpected token at '42'</code></p>
  
  <p><br></p>
  
  <h1>The workaround</h1>
  
  <p>The gem correctly parses these things if they are in an array: <code>'["foo"]'</code> or <code>'[3]'</code></p>
  
  <pre><code>jsonstring = '"foo"'
  JSON.parse( "[#{jsonstring}]" )[0]
  </code></pre>
  
  <p>yields</p>
  
  <pre><code>"foo"
  </code></pre>
  
  <p><br></p>
  
  <p>This is ridiculous. Am I not understanding something correctly? Or is this gem bugged?</p>