Why does the JSON module quote some numbers but not others?

后端 未结 2 1385
一个人的身影
一个人的身影 2020-12-11 20:14

We recently switched to the new JSON2 perl module. I thought all and everything gets returned quoted now. But i encountered some cases in which a number (250) g

2条回答
  •  时光取名叫无心
    2020-12-11 21:08

    You can force it into a string by doing something like this:

    $number_str = '' . $number;
    

    For example:

    perl -MJSON -le 'print encode_json({foo=>123, bar=>"".123})'
    {"bar":"123","foo":123}
    

    It looks like older versions of JSON has autoconvert functionality that can be set. Did you not have $JSON::AUTOCONVERT set to a true value?

提交回复
热议问题