From JSON::XS (emphasis added)
$json = $json->allow_nonref ([$enable])
$enabled = $json->get_allow_nonref
If $enable is true (or missing), then the encode method can convert a non-reference into its corresponding string, number or null JSON value, which is an extension to RFC4627. likewise, decode will accept those JSON
values instead of croaking.
If $enable is false, then the encode method will croak if it isn't passed an arrayref or hashref, as JSON texts must either be an object or array . Likewise, decode will croak if given something that is not a JSON object or array.
Example, encode a Perl scalar as JSON value with enabled allow_nonref, resulting in an invalid JSON text:
JSON::XS->new->allow_nonref->encode ("Hello, World!")
=> "Hello, World!"
In short, according to RFC4627 a JSON-string must start with an array (i.e. [...] ) or hash (i.e. {...} ) on top level.
Since many applications accept more, you have the option allow_nonref to allow simple values and strings.
As a side note: How do I post a question effectively?
| [reply] [d/l] [select] |
Please try to pass the same data structure to both, if you want the same result.
The first one is a string, the second a (malformed) hash.
I'd say { Hello => 1} does the same.
| [reply] [d/l] |
With that I get hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) returned
| [reply] [d/l] |
| [reply] [d/l] |
my mistake, I get the following {"Hello":1}
| [reply] [d/l] |