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

DateTimeX::TO_JSON - Adds a TO_JSON method to DateTime

VERSION

version 0.0.2

SYNOPSIS

  use DateTime;
  use JSON;
  use DateTimeX::TO_JSON formatter => 'DateTime::Format::RFC3339';

  my $dt = DateTime->now;
  my $out = JSON->new->convert_blessed(1)->encode([$dt]);

DESCRIPTION

Adds a TO_JSON method to DateTime so that JSON and other JSON serializers can serialize it when it encounters it a data structure.

Can be given an optional DateTime formatter on import such as DateTime::Format::RFC3339. Any formatter that supports new and format_datetime will work. Defaults to turning DateTime into a string by calling "datetime" in DateTime

If you want to format the date in your own way, then just define the following function in your code instead of using this module:

    sub DateTime::TO_JSON {
        my $dt = shift;
        # do something with $dt, such as:
        return $dt->ymd;
    }

AUTHOR

Steven Humphrey <shumphrey@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Steven Humphrey.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.