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

Handling invalid JSON #169

Open
exAspArk opened this issue Jan 20, 2017 · 3 comments
Open

Handling invalid JSON #169

exAspArk opened this issue Jan 20, 2017 · 3 comments

Comments

@exAspArk
Copy link

exAspArk commented Jan 20, 2017

Hi,

I have the following case:

require 'yajl'

valid_json = '{"1":{"11":11,"12":12}}'

invalid_json1 = valid_json.sub('":12}}', '')
# => {"1":{"11":11,"12
Yajl::Parser.parse(invalid_json1)
# => {"11"=>11}

invalid_json2 = valid_json.sub(':12}}', '')
# => {"1":{"11":11,"12"
Yajl::Parser.parse(invalid_json2)
# => "12"

require 'yajl/version'
Yajl::VERSION
# => "1.3.0"

In my opinion, this is really a strange behavior.

Can we check whether JSON is valid and if not, then raise an exception?

@exAspArk exAspArk changed the title Raise error if JSON is invalid Handling invalid JSON Jan 20, 2017
@kapcod
Copy link

kapcod commented Dec 7, 2017

Hi, any news here? It happens since version 1.1, i.e. 6 years already and it still behaves the same in 1.3.1.

@pienkowskip
Copy link

And it still behaves the same in 1.4.3 in 2022 :(

@pienkowskip
Copy link

pienkowskip commented Sep 9, 2022

I'm not sure but it seems that the problem is related to underlying C extension. yajl-ruby is based on version 1.x of yajl library and I think this bug was fixed in release of version 2.

If you use Yajl FFI binding with libyajl2 (2.1.0) parsing such data results in Yajl::FFI::ParserError (parse error: premature EOF). But... Yajl FFI is like 4-5 times slower :/

One can find this in yajl_parser.c, there is a function yajl_do_finish which does the work. But I'm not so good in C to implement this fix safely in yajl-ruby native C code.

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

3 participants