How to safely upgrade from JSON 1 to JSON 2 wrt utf8?

We have a large code base that makes abundant use of the JSON v1 API:

use JSON;
my $json = objToJson($data);
my $data = jsonToObj($json);

We would like to switch to JSON v2, so we can start using it in the new code, and because we are faced with other modules that depend on the v2 API .

However, if I saved the utf8 string created objToJson(), it will no longer be decoded in the same way as JSON :: XS (this is what JSON v2 uses behind the scenes).

use JSON;
use JSON::XS;
use warnings;
use strict;

my $data    = ["\x{263a}b"];
my $encoded = JSON::objToJson($data);
print "different!\n"
  unless JSON::jsonToObj($encoded)->[0] eq JSON::XS::decode_json($encoded)->[0];
print "different!\n"
  unless JSON::jsonToObj($encoded)->[0] eq JSON::XS->new->decode($encoded)->[0];

Is there any way to upgrade to JSON v2, but still leave API v1 for backward compatibility with existing code?

+5
source share
1

, . , JSON 2.x JSON:: XS, , , .

:

  • JSON 1.x.
  • JSON:: XS.
  • JSON 1.x JSON:: XS.
  • JSON 2.x, .
    • JSON 2.x
    • s/JSON:: XS/JSON/
+1

All Articles