Mojo::JSON_XS - Faster JSON processing for Mojolicious


  use Mojo::JSON_XS;  # Must be earlier than Mojo::JSON
  use Mojo::JSON qw(to_json from_json ...);


Using Mojo::JSON_XS overrides Mojo::JSON, so your JSON processing will be done by compiled C code rather than pure perl. Cpanel::JSON::XS is a hard dependency, so is required both at installation time and run time.


You absolutely must use Mojo::JSON_XS before anything uses Mojo::JSON.

I suggest that in your top-level file ( for a lite app and script/my_app for a full app) you use this module very early in the file (even if you do not mention any other JSON in that file).


The underlying module Cpanel::JSON::XS generates slightly different results (since it is maintaining compatibility with JSON::XS) from the results you would get from Mojo::JSON. Be sure to check each of the differences noted below and consider the impact on your application. Clearly it is no use generating the wrong output quickly when you could have the correct output (less quickly).

The examples below show to_json because it is slightly shorter, but usually it is encode_json that you will want. Remember too that j is available ("FUNCTIONS" in Mojo::JSON) particularly for commandline testing.


Mojo::JSON escapes slashes when encoding (to mitigate script-injection attacks).

  perl -MMojo::JSON=to_json -E'say to_json(q{/})'
  # produces "\/"

  perl -MMojo::JSON_XS -MMojo::JSON=to_json -E'say to_json(q{/})'
  # produces "/"

and similar for encode_json.


Mojo::JSON uses uppercase for hex values when encoding

  perl -MMojo::JSON=to_json -E'say to_json(qq{\x1f})'
  # produces "\u001F"

  perl -MMojo::JSON_XS -MMojo::JSON=to_json -E'say to_json(qq{\x1f})'
  # produces "\u001f"

and similar for encode_json. Cf

Mojo::JSON makes special cases for security, so u2028 and u2029 are rendered in their codepoint form.

  perl -MMojo::JSON=to_json -E'say to_json(qq{\x{2028}})'
  # produces "\u2028"

  perl -MMojo::JSON_XS -MMojo::JSON=to_json -E'say to_json(qq{\x{2028}})'
  # produces the unicode character


Mojo::JSON can encode references (as Boolean).

  perl -MMojo::JSON=to_json -E'$a = q{string}; say to_json(\$a)'
  # produces "true"

  perl -MMojo::JSON_XS -MMojo::JSON=to_json -E'$a = q{string}; say to_json(\$a)'
  # produces error
  # "cannot encode reference to scalar unless the scalar is 0 or 1"


Mojo::JSON detects numbers much better.

  perl -MMojo::JSON=to_json -E'$a = 2; say to_json(["$a", $a])'
  # produces "["2",2]"

  perl -MMojo::JSON_XS -MMojo::JSON=to_json -E'$a = 2; say to_json(["$a", $a])'
  # produces "["2","2"]"

Mojo::JSON encodes inf and nan as strings.

  perl -MMojo::JSON=to_json -E'say to_json(9**9**9)'
  # produces "inf"

  perl -MMojo::JSON_XS -MMojo::JSON=to_json -E'say to_json(9**9**9)'
  # produces inf

Error Messages

The handling and format of error messages is different between the two modules, as you would expect.


Although the code is gifted by Sebastian Riedel, this is not part of the Mojolicious distribution. Saying that, it is likely you can find someone on the IRC channel happy to discuss this module. Any bugs or issues should be logged in the specific Github account.


#mojo on

Github Issue Tracker


Copyright (C) 2014--17, Sebastian Riedel, Nic Sandfield.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.


Mojo::JSON, Cpanel::JSON::XS.