oj icon indicating copy to clipboard operation
oj copied to clipboard

Migrating strict mode options to the new usual parser

Open semaperepelitsa opened this issue 1 year ago • 4 comments

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

semaperepelitsa avatar Nov 21 '23 09:11 semaperepelitsa

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.

ohler55 avatar Nov 21 '23 14:11 ohler55

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.

semaperepelitsa avatar Nov 21 '23 14:11 semaperepelitsa

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 avatar Nov 21 '23 15:11 ohler55

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

ohler55 avatar Dec 04 '23 00:12 ohler55