kazeburo / Kossy

sinatra-ish simple waf
Other
35 stars 14 forks source link

json escape only for string #3

Closed sonots closed 11 years ago

sonots commented 11 years ago

I got a JSON parse error for outputs coming when I posted a number to GrowthForecast (--enable-float-number). The returned JSON is as belows (indented):

{"llimit":-1000000000,"number":0,"stype":"AREA","mode":"gauge","adjustval":"1000000",
"meta":"{\"number\":0,\"mode\":\"gauge\",\"adjustval\":\"1000000\",\"meta\":\"\",\"data\":[],
\"unit\":\"sec\",\"adjust\":\"/\",\"md5\":\"8613985ec49eb8f757ae6439e879bb2a\"}",
"service_name":"0","gmode":"gauge","color":"#000000","created_at":"2013/06/2717:43:16",
"section_name":"0","ulimit":1e\u002b15,"id":90,"graph_name":"9"}

"ulimit":1e\u002b15 is bad. It must be like "ulimit":1e+15, + must not be escaped because it is a float, not a string.

irb(main):009:0> require 'json'
irb(main):009:0> JSON.parse('{"ulimit":1e\u002b15}')
JSON::ParserError: 757: unexpected token at '{"ulimit":1e\u002b15}'

http://www.atmarkit.co.jp/ait/articles/0908/10/news087_2.html

文字列を生成する場合には、半角英数字(a-zA-Z0-9)以外の文字はすべて\uXXXXの形式でエスケープしておくとよいでしょう。

I changed the code to apply the escape conversion only for strings.