Cpanel-JSON-XS icon indicating copy to clipboard operation
Cpanel-JSON-XS copied to clipboard

allow_blessed/convert_blessed and type_all_string compatibility

Open alpha6 opened this issue 4 years ago • 5 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

alpha6 avatar Feb 18 '21 19:02 alpha6

Please discuss that with @pali

rurban avatar May 04 '22 13:05 rurban

CC @choroba

pali avatar May 04 '22 13:05 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".

bschmalhofer avatar Oct 25 '23 13:10 bschmalhofer

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.

choroba avatar Oct 25 '23 14:10 choroba

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.

bschmalhofer avatar Oct 25 '23 15:10 bschmalhofer