rurban / Cpanel-JSON-XS

Improved fork of JSON-XS

Home Page:http://search.cpan.org/dist/Cpanel-JSON-XS/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

allow_blessed/convert_blessed and type_all_string compatibility

alpha6 opened this issue · comments

Hello.

I'm trying to use the allow_blessed/convert_blessed and type_all_string simultaneously. But the result looks like as thetype_all_string has a higher priority than the allow_blessed/convert_blessed .

Here's a sample code:

#!/usr/bin/env perl

use v5.20;
use strict;
use Cpanel::JSON::XS;

my $json = Cpanel::JSON::XS->new->allow_blessed(1)->convert_blessed(1)->type_all_string(1);

my $obj = bless {}, 'foo';

my $ref = { num => 123, str => 'val', obj => $obj };

say  $json->encode($ref);

Result:

{"obj":"foo=HASH(0x562c5c8ff7b0)","str":"val","num":"123"}

The expected result with allow/convert_blessed and type_all_string is:

{"obj":null,"str":"val","num":"123"}

Did I forget some options or is this a bug or some feature?

Thank you in advance!

Denis

Please discuss that with @pali

I have a bit related use case. I'd like to set type_all_string()in order to avoid the mess of how numbers are more or less randomly encoded. I don't need to serialize blessed objects. But the booleans should still be encoded as trueand false and not as "true"and "false".

I'm not sure it's clear what the result of combining type_all_string and convert_blessed should be, i.e. obj:"" seems reasonable, too. The current situation, i.e. the HASH address, makes no sense at all.

One approach could be to take the converted blessed objects and then stringify them. For my specific use case I was hoping for a stringify_numbers option. But even in this more specific case, the order in which the events should happen is not really obvious.