variable type utility for JSON encoding
Perl
Switch branches/tags
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib/JSON
t
xt
.shipit
.travis.yml
Changes
LICENSE
MANIFEST
MANIFEST.SKIP
Makefile.PL
README

README

NAME
    JSON::Types - variable type utility for JSON encoding

SYNOPSIS
        # Export type functions by default
        use JSON;
        use JSON::Types;
    
        print encode_json({
            number => number "123",
            string => string 123,
            bool   => bool "True value",
        });
        # => {"number":123,"string":"123","bool":true}
    
    
        # Non export interface
        use JSON::Types ();
    
        print encode_json({
            number => JSON::Types::number "123",
            string => JSON::Types::string 123,
            bool   => JSON::Types::bool "True value",
        });

DESCRIPTION
    The type mappings between JSON and Perl is annoying things. For example,

        use JSON;
    
        my $number = 123;
    
        warn "[DEBUG] number:$number\n" if $ENV{DEBUG};
    
        print encode_json([ $number ]);

    Output of this code depends on whether DEBUG environment is set or not.
    If set, result is "[123]". If not to set, result is "["123"]". This is
    normal behaviour on Perl though, it sometimes causes unexpected JSON
    results.

    There is a solution about this:

        print encode_json([ $number + 0 ]);

    This code always outputs "[123]". But the code is a bit ugly and not
    readable at all.

    This module provides some functions to fix this variable types issue:

        number $foo;  # is always number
        string $foo;  # is always string
        bool   $foo;  # is always bool

    You can fix above code by using this module like this:

        use JSON;
        use JSON::Types;
    
        my $number = 123;
    
        warn "[DEBUG] number:$number\n" if $ENV{DEBUG};
    
        print encode_json([ number $number ]);

FUNCTIONS
    There is three functions and all functions is exported by default.

    If you don't want this exported functions, pass empty list to use line:

        use JSON::Types ();

    You should specify full function name when this case, like
    "JSON::Types::number $foo" or etc.

  string
  number
  bool
BEHAVIOURS ON UNEXPECTED ARGS
  string(undef), number(undef) returns undef, bool(undef) returns false.
    Passing undefined variable to string and number function is returns
    undef. If you doesn't prefer this, have to treat this like following:

        number $undef_possible_value // 0

    This code returns 0 if variable is undef.

  number($string)
    Passing not numeric variable to number function is returns 0, but a
    warning will be occurred.

AUTHOR
    Daisuke Murase <typester@cpan.org>

COPYRIGHT AND LICENSE
    Copyright (c) 2012 Daisuke Murase. All rights reserved.

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

    The full text of the license can be found in the LICENSE file included
    with this module.