ohler55 / oj

Optimized JSON
http://www.ohler.com/oj
MIT License
3.14k stars 251 forks source link

Migrating strict mode options to the new usual parser #904

Open semaperepelitsa opened 10 months ago

semaperepelitsa commented 10 months ago

We are currently using :quirks_mode=>false, :empty_string=>false options. But they are not available in the new parser. Is there a recommended alternative?

> Oj.load("", :mode=>:strict, :quirks_mode=>false, :empty_string=>false)
JSON::ParserError: Empty input (after ) at line 1, column 1 [parse.c:1062]

> Oj.load("123", :mode=>:strict, :quirks_mode=>false, :empty_string=>false)
Oj::ParseError: unexpected non-document value

> Oj::Parser.new(:usual).parse("")
=> nil

> Oj::Parser.new(:usual).parse("123")
=> 123
ohler55 commented 10 months ago

I need to look into it. In general the new parser does not have all the options the old one does. That is intentional as the number of detailed options caused confusion and were often the cause of issues where they were not working exactly the way different developers wanted them to.

semaperepelitsa commented 10 months ago

These options are easy enough to implement on client-side. If they are not offered by other implementations, dropping them seems fine. But :empty_string=>false behavior is how it works by default in Ruby's built-in JSON and in Javascript, so it seems wrong not to have it here.

ohler55 commented 10 months ago

I'll look at it tonight. I'm trying to avoid falling into the trap of being just like the JSON gem but I could see the reason it might be reasonable to raise on anything other than a non-empty string.

ohler55 commented 10 months ago

Please try the raise_on_empty branch. A test was added in test/test_parser_usual.rb:150.