It seems to me that adding a TO_JSON method that calls ->bstr() will get
you the existing behavior; that is, the modules already overload
stringification such that JSON encoding will trigger stringification. In
short, "sub TO_JSON { shift()->bstr() }" should just get you existing
behavior.
I think what you were wanting is numification, so your TO_JSON methods
would call ->numify(). But then you will *not* be guaranteed a loss-less
round trip.
Consider:
package Math::MyBigInt;
use Math::BigInt;
our @ISA = 'Math::BigInt';
sub TO_JSON {
return shift()->numify;
}
package main;
use Mojo::JSON qw( encode_json decode_json );
use Math::BigInt;
my $int = Math::MyBigInt->new('50000000000000000000000');
my $j = encode_json {int => $int};
my $d = decode_json $j;
print "$j\n"; # {"int":5e+22}
printf "%f\n", $d->{int}; # 49999999999999995805696.000000
On Mon, Jun 30, 2014 at 2:43 PM, Tekki <[email protected]> wrote:
>
> Am Montag, 30. Juni 2014 22:16:32 UTC+2 schrieb sri:
>
>> use Math::BigFloat;
>>> use Math::BigInt;
>>>
>>
>> Send patches for those modules and add TO_JSON methods. ;)
>>
>
> The most elegant solution would be to update Mojo::JSON, the easiest to
> modify the modules of my own application. So I'll probably subclass
> Math::BigFloat, add a TO_JSON method that calls ->bstr (thanks to Gabriel
> for the tip) and use this instead of Math::BigFloat.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Mojolicious" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/mojolicious.
> For more options, visit https://groups.google.com/d/optout.
>
--
David Oswald
[email protected]
--
You received this message because you are subscribed to the Google Groups
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.