hjson/hjson-php

Incorrect stringification of falsy strings

Closed this issue · 1 comments

HJSONStringifier->quote has a check at the beginning which returns an empty quoted string if the string is falsy:

private function quote($string = null, $gap = null, $hasComment = null, $isRootObject = null)
    {
        if (!$string) {
            return '""';
        }
...}

This means that if the string is falsy, such as if it contains just "0", it won't be serialized correctly:

php > echo (new HJSON\HJSONStringifier)->stringify(["A", "0"]);
[
  A
  ""
]

The fix is simply changing the !$string test to $string === ''. As HJSONStringifier->quote is a private function and, as far as I can see, there is no way for it to be called with anything other than a string, this should not affect anything else.

Created a PR #30 to fix the issue (first time doing so, so please let me know if I have messed it up :-))

I haven't been able to wrap my head around how the test structure works and I don't have much time now to investigate, so I'll refrain from adding a test case. It is probably simple, though.