benkasminbullock / json-create

https://metacpan.org/release/JSON-Create
1 stars 4 forks source link

Broken JSON when undefs are encoded #1

Closed zoffixznet closed 9 years ago

zoffixznet commented 9 years ago

The docs advertise undefs to be encoded as nulls, but currently broken JSON is generated instead:

$ perl -MJSON::Create=create_json -wlE 'say create_json $_ for undef, [undef], {foo => undef}'
Use of uninitialized value in say at -e line 1.

[]
{"foo":}
benkasminbullock commented 9 years ago

Thank you for your report. This is fixed via

commit 4a6384e6da0968f93160d4b3a54c6efcf12453ea

If you are interested, details of what happened follow.

Thank you again for your report.


The error was here:

https://github.com/benkasminbullock/json-create/commit/4a6384e6da0968f93160d4b3a54c6efcf12453ea#diff-c83f00e3dc9c5499b21409dd7e8ffb5aL114

It was testing the value of the character ("c") before, rather than after, reading it from the master string ("s"). In other words it was making a meaningless test of an uninitialized value to find where the string ended.

This had not been detected earlier because everything except "null" production was going via another routine, add_str_len, here:

https://github.com/benkasminbullock/json-create/blob/4a6384e6da0968f93160d4b3a54c6efcf12453ea/json-create-perl.c#L120

Also, there were no tests for the documented behaviour, which claimed to produce null with undefined values. (In fact I had not even confirmed this statement at the time of writing it, let alone writtten module tests.)


Thanks once more.