Cpanel-JSON-XS
Cpanel-JSON-XS copied to clipboard
JSON::XS->new->new fails badly
I know this is my fault, by I accidentally wrote code that did ->new on an object, and gave me a confusing error message:
Can't locate object method "encode" via package "JSON::XS=SCALAR(0x7fccca0070d0)" at -e line 1.
It would be helpful if JSON::XS->new could make sure that the class is not an object, just as pp_bless does.
Feel free simply to close this ticket if you do not think it is worth the extra check.
I'm not sure.
Both JSON::XS new and Cpanel::JSON::XS new accept any scalar and use the stash of it as classname. Objects are allowed.
$o=Cpanel::JSON::XS->new;print ref $o->new
returns an object of Cpanel::JSON::XS
.
$o=Cpanel::JSON::XS::new("JSON::XS");print ref $o->new
returns an object of JSON::XS
but with the fields of Cpanel::JSON::XS. It does no harm, it just uses the class of the existing object.
What pp_bless additionally does, but (Cpanel::)?JSON::XS not, are three checks:
- "Attempt to bless into a freed package"
- "Attempt to bless into a reference" I think you want this
- "Explicit blessing to '' (assuming package main)"
That would be overkill to add those checks also. No other XS constructor does this, just PP constructors.
Feel free to close it then, if you want.
Still thinking a bit over this...