为什么`JSON.parse`没有抛出异常?

时间:2016-07-30 02:20:45

标签: ruby json rubygems

我们一直在使用JSON.parse一段时间,我们刚刚更新到最新版本。

我们收到以下错误。

功能是否已更新为返回nil而不是抛出ParserError?

function login(action, callback) {

    Meteor.loginWithPassword(action.email, action.password, err => {
        if ( err ) {
            //...
        } else {
          callback('login success');
        }
    });

 }
2.3.0 :001 > gem 'json', '2.0.2'
 => true
2.3.0 :002 > require 'json'
 => true
2.3.0 :003 > JSON.parse("null")
 => nil

1 个答案:

答案 0 :(得分:5)

该功能已更新为符合JSON specification according to RFC 7159,其中包含:

  

JSON值必须是对象,数组,数字或字符串,或者是其中之一   以下三个字面名称:

false null true
     

文字名称必须小写。不允许使用其他文字名称。

因此,JSON.parse("null")返回nil是预期的功能,您可以通过查看tests for parsing single values来确认。

相关问题