What kind of number would you return them as? Let's consider Math::BigInt:
use Mojo::JSON 'encode_json';
use Math::BigInt;
my $int = Math::BigInt->new(50000000000000000000000');
print encode_json {int => $int}; # {"int":"50000000000000000000000"}
5.0E+22 is too large of an integer to be supported by the underlying type
systems, and it seems unlikely that one would prefer that it be converted
to a floating point (with associated loss of precision), since in floating
point math, any value that cannot be represented by exactly n/2^m loses
precision.
However, you are welcome to subclass the modules to your taste, if you are
ok with the lossy behavior that occurs when a BigInt that exceeds the size
of an integer "Plain Old Data" type, thereby being converted to a floating
point representation:
package Math::MyBigInt;
use Math::BigInt;
our @ISA = 'Math::BigInt';
sub TO_JSON {
return shift()->numify;
}
package main;
use Mojo::JSON 'encode_json';
my $int = Math::MyBigInt->new('50000000000000000000000');
print encode_json {int => $int}; #{"int":5e+22}
Just remember:
printf "%f", 5e+22; # 49999999999999995805696.000000
On Mon, Jun 30, 2014 at 12:12 PM, Tekki <[email protected]> wrote:
> Mojo::JSON encodes Math::BigFloat and Math::BigInt as strings. Example:
>
> use Mojo::JSON 'encode_json';
> use Math::BigFloat;
> use Math::BigInt;
>
> my $float = Math::BigFloat->new(12.34);
> my $int = Math::BigInt->new(123);
>
> print encode_json {float => $float, int => $int}; #
> {"int":"123","float":"12.34"}
>
> Wouldn't it make sense to return them as numbers instead?
>
> --
> 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.