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

Erroneous boolean parsing. #1

Closed
efrecon opened this issue Jan 20, 2015 · 1 comment
Closed

Erroneous boolean parsing. #1

efrecon opened this issue Jan 20, 2015 · 1 comment

Comments

@efrecon
Copy link
Contributor

efrecon commented Jan 20, 2015

I have failed parsing JSON that looks like this:

{"action":"set","node":{"key":"/dir4","dir":true,"modifiedIndex":17,"createdIndex":17}}

This all fails on the parsing of true. To fix this, I propose the following implementation:

proc ::json::decode-value {str {numberDictArrays 0}} {
    set str [string trimleft $str]
    switch -regexp -- $str {
        {^\".*} {
            return [::json::decode-string $str]
        }
        {^[0-9-].*} {
            return [::json::decode-number $str]
        }
        {^\{.*} {
            return [::json::decode-object $str $numberDictArrays]
        }
        {^\[.*} {
            return [::json::decode-array $str $numberDictArrays]
        }
        {^(true|false|null)} {
            regexp {^(true|false|null)} $str val
            return [list $val [regsub {^(true|false|null)} $str ""]]
        }
        default {
            error "cannot decode value as JSON: \"$str\""
        }
    }
}

The only difference is in the implementation of the {^(true|false|null)} branch of the switch.

I haven't made extensive testing of this, but my solution seems to solve the problem. You might want to introduce a json::decode-boolean for completion?

@dbohdan
Copy link
Owner

dbohdan commented Jan 20, 2015

Thanks for a great bug report! I've put your implementation in ::json::decode-boolean-or-null.

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

2 participants