Skip to content

JSON Integer/Double conversion problem. #22

@jezwhite

Description

@jezwhite

Hello all,

I'm having an odd problem, and I'm not sure what the solution should be...

I'm using JavaScript-Duktape-XS to generate some dynamic config files which is then JSON posted to a 3rd party (I am usingMojo::UserAgent to post) . During the conversion of objects from Duktape->Perl->JSON integers are converted to doubles which causes the the json to look like:

{
   "an_id":1.0,
   "another_id":252.0
}

rather than:

{
   "an_id":1,
   "another_id":252
}

the 3rd party is throwing an exception as it's expecting an integer for these fields. I can't get the 3rd party to change their parsing rules. The actual objects passed to the 3rd party are much more complex/dynamic than this example.

One solution that solves this for me is to explicitly check of the value returned via duk_get_number to see if it looks like an integer (in pl_duk.c, pl_duk_to_perl_impl) , ie, something like:

        case DUK_TYPE_NUMBER: {
            duk_double_t val = duk_get_number(ctx, pos);
            if (IsAnInteger(val)) {
              ret = newSViv(val);
            }
            else {
              ret = newSVnv(val);  /* JS numbers are always doubles */
            }
          break;

But I am not sure what the broader implicates would be?

Any thoughts/comments?

Regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions