The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

UNIVERSAL::to_json - to_json() method for all objects.

VERSION

This document describes UNIVERSAL::to_json version 0.01

SYNOPSIS

  use UNIVERSAL::to_json;

  my $obj = Foo->new;
  print $obj->to_json;

  {
      use autobox; # activate autobox

      print 'scalar value'->to_json;   #=> "scalar value"
      print [qw(list items)]->to_json; #=> ["list","items"]
      print {key => 'value'}->to_json; #=> {"key":"value"}

      no autobox;  # inactivate autobox

      print {key => 'value'}->to_json;
      #=> 'Can't call method "to_json" on unblessed reference'
  }

DESCRIPTION

UNIVERSAL::to_json provides to_json() method to all objects.

Besides, it supports you to extend unblessed values like a scalar, a reference to an array and a reference to a hash to be able to be called to_json() method from them directly. This feature is optional, and owes to brilliant autobox.

NOTE: This distribution doesn't designate autobox module as pre-required. If you want the feature to be able to add to_json() method into unblessed values, you need to install it by your hand in advance.

METHODS

to_json()

      my $obj = Foo->new;
      print $obj->to_json;
    
      use autobox;
    
      print 'scalar value'->to_json;
      print [qw(list items)]->to_json;
      print {key => 'value'}->to_json;
    
      no autobox;

    to_json() method dumps the current object as JSON.

    If autobox is activated, to_json() method can be called magically from unblessed values.

which()

      my $loaded = UNIVERSAL::to_json::which();

    This method returns which of JSON or JSON::Syck is loaded actually.

    So you can recognize which module is loaded and assign some values to the package variables privided by the loaded module to set options and control outputs from to_json() method.

      my $loaded = UNIVERSAL::to_json::which;
    
      if ($loaded eq 'JSON') {
          $JSON::UTF8 = 1;
      }
      else {
          $JSON::Syck::ImplicitUnicode = 1;
      }

    For more details on the options, see the documentations of both JSON and JSON::Syck.

DEPENDENCIES

You must have JSON or JSON::Syck installed. UNIVERSAL::to_json prefers later over former if you have them both.

SEE ALSO

AUTHOR

Kentaro Kuribayashi <kentaro@cpan.org>

COPYRIGHT AND LICENSE (The MIT License)

Copyright (c) 2006, Kentaro Kuribayashi <kentaro@cpan.org>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.