Skip to content

Commit 5e81aa9

Browse files
committed
Replace all of webwork2's direct JSON usage with Mojo::JSON.
PG still uses `JSON` for now, so this doesn't quite remove the dependency on the module. But PG can be switched also. Note that the module uses `Cpanel::JSON::XS` (if that module is installed) which is the recommended perl JSON encoding and decoding module. `Mojo::JSON` falls back to their own pure perl implementation if `Cpanel::JSON::XS` is not installed. Note that the `Mojo::JSON::encode_json` method UTF-8 encodes the output and is canonical (sorts object keys). The `Mojo::JSON::to_json` method does not UTF-8 encode the output, but is still canonical. The corresponding `Mojo::JSON::decode_json` decodes UTF-8 encoded JSON and `Mojo::JSON::from_json` method decodes non UTF-8 encoded json.
1 parent 3b9a306 commit 5e81aa9

File tree

9 files changed

+8
-14
lines changed

9 files changed

+8
-14
lines changed

bin/OPL-update-legacy

-2
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,6 @@ if($canopenfile) {
539539
}
540540
#### End of taxonomy/taxonomy2
541541

542-
use JSON;
543-
544542
#### Save the official taxonomy in json format
545543
my $webwork_htdocs = $ce->{webworkDirs}{htdocs};
546544
my $file = "$webwork_htdocs/DATA/tagging-taxonomy.json";

bin/OPLUtils.pm

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use warnings;
1717
use File::Find::Rule;
1818
use File::Basename;
1919
use open qw/:std :utf8/;
20-
use JSON;
20+
use Mojo::JSON qw(encode_json);
2121

2222
our @EXPORT = ();
2323
our @EXPORT_OK =
@@ -373,7 +373,7 @@ sub build_library_textbook_tree {
373373
sub writeJSONtoFile {
374374
my ($data, $filename) = @_;
375375

376-
my $json = JSON->new->utf8->encode($data);
376+
my $json = encode_json($data);
377377
open my $fh, ">", $filename or die "Cannot open $filename";
378378
print $fh $json;
379379
close $fh;

bin/check_modules.pl

-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ =head1 DESCRIPTION
108108
Iterator
109109
Iterator::Util
110110
JSON
111-
JSON::MaybeXS
112111
Locale::Maketext::Lexicon
113112
Locale::Maketext::Simple
114113
LWP::Protocol::https

bin/download-OPL-metadata-release.pl

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use File::Path;
1212
use Archive::Tar;
1313
use Mojo::File;
14-
use JSON;
14+
use Mojo::JSON qw(decode_json);
1515

1616
BEGIN {
1717
use Mojo::File qw(curfile);
@@ -36,7 +36,7 @@ BEGIN
3636
File::Fetch->new(uri => 'https://api.github.com/repos/openwebwork/webwork-open-problem-library/releases/latest');
3737
my $file = $releaseDataFF->fetch(to => $ce->{webworkDirs}{tmp}) or die $releaseDataFF->error;
3838
my $path = Mojo::File->new($file);
39-
my $releaseData = JSON->new->utf8->decode($path->slurp);
39+
my $releaseData = decode_json($path->slurp);
4040
$path->remove;
4141

4242
my $releaseTag = $releaseData->{tag_name};

lib/Caliper/Sensor.pm

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use WeBWorK::CourseEnvironment;
77
use WeBWorK::DB;
88
use WeBWorK::Debug;
99
use Data::Dumper;
10-
use JSON;
10+
use Mojo::JSON qw(encode_json);
1111
use Time::HiRes qw/gettimeofday/;
1212
use Date::Format;
1313

@@ -67,7 +67,7 @@ sub sendEvents {
6767
'data' => $event_chunk,
6868
};
6969

70-
my $json_payload = JSON->new->canonical->encode($envelope);
70+
my $json_payload = encode_json($envelope);
7171
# debug("Caliper event json_payload: " . $json_payload);
7272

7373
my $HTTPRequest = HTTP::Request->new(

lib/WeBWorK/ContentGenerator/InstructorRPCHandler.pm

-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@ error occurs, then the response will contain an "error" key.
3939
# was "instructor" only. Usage of all commands is based on permissions, and there have always been non-instructor users
4040
# that have some of these permissions. So this module and the corresponding route should really be renamed.
4141

42-
use JSON;
43-
4442
use WebworkWebservice;
4543

4644
sub initializeRoute ($c, $routeCaptures) {

lib/WeBWorK/Controller.pm

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ fields.
2525
=cut
2626

2727
use Encode;
28-
use JSON::MaybeXS;
2928

3029
use WeBWorK::Localize;
3130

lib/WeBWorK/Utils/Instructor.pm

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use strict;
2626
use warnings;
2727

2828
use File::Find;
29+
use Mojo::JSON qw(decode_json);
2930

3031
use WeBWorK::DB::Utils qw(initializeUserProblem);
3132
use WeBWorK::Debug;
@@ -642,7 +643,7 @@ sub loadSetDefListFile {
642643
$contents;
643644
};
644645

645-
return @{ JSON->new->decode($data) };
646+
return @{ decode_json($data) };
646647
}
647648

648649
return;

lib/WebworkWebservice/SetActions.pm

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use strict;
2020
use warnings;
2121

2222
use Carp;
23-
use JSON;
2423
use Data::Structure::Util qw(unbless);
2524

2625
use WeBWorK::Utils qw(max);

0 commit comments

Comments
 (0)