Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiJson.load('') behavior doesn't match JSON specification #206

Open
joshcooper opened this issue Sep 30, 2022 · 2 comments
Open

MultiJson.load('') behavior doesn't match JSON specification #206

joshcooper opened this issue Sep 30, 2022 · 2 comments

Comments

@joshcooper
Copy link

MultiJson.load('') raises, but JSON.load('') returns nil:

require 'json'
puts JSON.load('').inspect

require 'multi_json'
MultiJson.use(:json_gem)
begin
  puts MultiJson.load('')
rescue => e
  puts e.class
end
$ bx ruby --version
ruby 2.7.6p219 (2022-04-12 revision c9c2245c0a) [x86_64-linux]
$ bx ruby test.rb 
nil
MultiJson::ParseError

MultiJson behavior was changed in 49aa2d8 to raise an error. But JSON has accepted empty string since 2011 ruby/json@0eb6ed5 Also the spec says:

A [JSON] string is a sequence of zero or more Unicode characters

@joshcooper
Copy link
Author

It appears JSON.load accepts an empty string by default, but JSON.parse and MultiJson.load do not. Also JSON.parse can be configured to reject empty strings:

irb(main):001:0> require 'json'
=> true
irb(main):002:0> JSON.load("")
=> nil
irb(main):003:0> JSON.load("", nil, allow_blank: true)
=> nil
irb(main):004:0> JSON.load("", nil, allow_blank: false)
Traceback (most recent call last):
        ....
        3: from /home/josh/work/json/lib/json/common.rb:569:in `load'
        2: from /home/josh/work/json/lib/json/common.rb:216:in `parse'
        1: from /home/josh/work/json/lib/json/common.rb:216:in `parse'
JSON::ParserError (783: unexpected token at '')
irb(main):005:0> JSON.parse("")
Traceback (most recent call last):
        ...
        2: from /home/josh/work/json/lib/json/common.rb:216:in `parse'
        1: from /home/josh/work/json/lib/json/common.rb:216:in `parse'
JSON::ParserError (783: unexpected token at '')

I think it would be least surprising if MultiJson.load allowed an empty string like the similarly named JSON.load, but I don't know if that behavior is universal among different backends.

@sferik
Copy link
Member

sferik commented Mar 12, 2024

I would accept a patch to make MultiJson.load behave like JSON.load, however this would require a major version bump.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants