The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

JSON::Conditional - The great new JSON::Conditional!

VERSION

Version 0.02

SYNOPSIS

Quick summary of what the module does.

        use JSON::Conditional;

        my $c = JSON::Conditional->new();

        my $json = '{
                "for": {
                        "key": "countries",
                        "each": "countries",
                        "if": {
                                "m": "Thailand",
                                "key": "country",
                                "then": {
                                        "rank": 1
                                }
                        },
                        "elsif": {
                                "m": "Indonesia",
                                "key": "country",
                                "then": {
                                        "rank": 2
                                }
                        },
                        "else": {
                                "then": {
                                        "rank": null
                                }
                        },
                        "country": "{country}"
                }
        }';

        $json = $c->compile($json, {
                countries => [
                        { country => "Thailand" },
                        { country => "Indonesia" },
                        { country => "Japan" },
                        { country => "Cambodia" },
                ]
        }, 1);

        ...

        {
                countries => [
                        {
                                rank => 1,
                                country => "Thailand"
                        },
                        {
                                rank => 2,
                                country => 'Indonesia'
                        },
                        {
                                rank => undef
                                country => 'Japan',
                        },
                        {
                                rank => undef,
                                country => 'Cambodia'
                        }
                ]
        };

METHODS

new

Instantiate a new JSON::Conditional object. Currently this expects no arguments.

        my $c = JSON::Conditional->new;

encode

Encode a perl struct into JSON.

decode

Decode a JSON string into a perl struct.

compile

Compile a json string containing valid JSON::Conditional markup into either a json string or perl struct based upon the passed params.

        $c->compile($json, $params); # json string

        $c->compile($json, $params, 1); # perl struct

itterate

Itterate a perl struct that contains valid JSON::Conditional markup and return a perl struct based upon the passed params.

        $c->itterate($perl_struct, $params);

Markup or Markdown

keywords

if, elsif, else

If, elsif and else conditionals are logical blocks used within JSON::Conditional. They are comprised of a minimum of four parts, the keyword, the expression, 'key' and 'then'. The expression can be any that are defined in the expression section of this document. The 'key' is the value in the params that will be evaluated and the 'then' is the response that is returned if the expression is true.

        my $json = '{
                "if": {
                        "m": "Thailand",
                        "key": "country",
                        "then": {
                                "rank": 1
                        }
                },
                "elsif": {
                        "m": "Indonesia",
                        "key": "country",
                        "then": {
                                "rank": 2
                        }
                },
                "else": {
                        "then": {
                                "rank": null
                        }
                },
                "country": "{country}"
        }';

        $json = $c->compile($json, {
                country => "Thailand"
        }, 1);

        ...

        {
                country => 'Thailand',
                rank => 1
        }

You can also write this like the following:

        my $json = '{
                "if": {
                        "m": "Thailand",
                        "key": "country",
                        "then": {
                                "rank": 1
                        },
                        "elsif": {
                                "m": "Indonesia",
                                "key": "country",
                                "then": {
                                        "rank": 2
                                },
                                "else": {
                                        "then": {
                                                "rank": null
                                        }
                                }
                        }
                },
                "country": "{country}"
        }';

        $json = $c->compile($json, {
                country => "Indonesia"
        }, 1);

        ...

        {
                country => 'Indonesia',
                rank => 2
        }

given

Given conditionals are logical blocks used within JSON::Conditional. They are comprised of a minimum of three parts, the keyword, 'when' and'key'. The 'when' can either be an array or a hash of expression that are defined in the expression section of this document. The 'key' is the value in the params that will be evaluated. You can optionally provide a default which will be used when no 'when' expressions are matched.

        my $json = '{
                "given": {
                        "key": "country",
                        "default": {
                                "rank": null
                        },
                        "when": [
                                {
                                        "m": "Thailand",
                                        "then": {
                                                "rank": 1
                                        }
                                },
                                {
                                        "m": "Indonesia",
                                        "then": {
                                                "rank": 2
                                        }
                                }
                        ]
                },
                country => "{country}"
        }';

        my $compiled = $c->compile($json, {
                country => 'Thailand'
        }, 1);

        ...

        {
                country => 'Thailand'
                rank => 1
        }

You can also write this like the following:

        my $json = '{
                "given": {
                        "key": "country",
                        "when": {
                                "Thailand": {
                                        "rank": 1
                                },
                                "Indonesia": {
                                        "rank": 2
                                },
                                "default": {
                                        "rank": null
                                }
                        }
                },
                country => "{country}"
        }';

        my $compiled = $c->compile($json, {
                country => 'Indonesia'
        }, 1);

        ...

        {
                country => 'Indonesia'
                rank => 1
        }

or

The 'or' keyword allows you to chain expression checks, where only one expression has to match.

        my $json = '{
                "if": {
                        "m": "Thailand",
                        "key": "country",
                        "then": {
                                "rank": 1,
                                "country": "{country}"
                        },
                        "or": {
                                "key": "country",
                                "m": "Maldives",
                                "or": {
                                        "key": "country",
                                        "m": "Greece"
                                }
                        }
                },
        }';


        my $compiled = $c->compile($json, {
                country => 'Greece'
        }, 1);

        ...

        {
                country => 'Greece'
                rank => 1
        }

and

The 'and' keyword allows you to chain expression checks, where only all expression has to match.

        my $json = '{
                "if": {
                        "m": "Thailand",
                        "key": "country",
                        "then": {
                                "rank": 1,
                                "country": "{country}"
                        },
                        "and": {
                                "key": "season",
                                "m": "Summer",
                        }
                }
        }';


        my $compiled = $c->compile($json, {
                country => 'Thailand',
                season => 'Summer'
        }, 1);

        ...

        {
                country => 'Thailand'
                rank => 1
        }

expressions

m

Does the params key value match the provided regex value.

        {
                "key": $param_key
                "m": $regex,
                "then": \%then
        }

nm

Does the params key value not match the provided regex value.

        {
                "key": $param_key
                "nm": $regex,
                "then": \%then
        }

eq

Does the params key value equal the provided value.

        {
                "key": $param_key
                "eq": $equals,
                "then": \%then
        }

ne

Does the params key value not equal the provided value.

        {
                "key": $param_key
                "ne": $equals,
                "then": \%then
        }

gt

Is the params key value greater than the provided value.

        {
                "key": $param_key
                "gt": $greater_than,
                "then": \%then
        }

lt

Is the params key value less than the provided value.

        {
                "key": $param_key
                "lt": $greater_than,
                "then": \%then
        }

placeholders

All parameters that are passed into compile can be used as placeholders within the json. You can define a placeholder by enclosing a key in braces.

        {
                "placeholder": "{param_key}"
        }

loops

for

each

Expects key to reference a array in the passed params. It will then itterate each item in the array and build an array based upon which conditions/expressions are met.

        my $json = '{
                "for": {
                        "key": "countries",
                        "each": "countries",
                        "country": "{country}"
                }
        }';

        $json = $c->compile($json, {
                countries => [
                        { country => "Thailand" },
                        { country => "Indonesia" },
                        { country => "Japan" },
                        { country => "Cambodia" },
                ]
        }, 1);

        ...

        {
                countries => [
                        {
                                country => "Thailand"
                        },
                        {
                                country => 'Indonesia'
                        },
                        {
                                country => 'Japan',
                        },
                        {
                                country => 'Cambodia'
                        }
                ]
        };

keys

Expects key to reference a hash in the passed params. It will then itterate keys in the hash and build an hash based upon which conditions/expressions are met.

        my $json = '{
                "for": {
                        "key": "countries",
                        "keys": 1,
                        "country": "{country}"
                },
        }';

        $json = $c->compile($json, {
                countries => {
                        1 => { country => "Thailand" },
                        2 => { country => "Indonesia" },
                        3 => { country => "Japan" },
                        4 => { country => "Cambodia" },
                }
        }, 1);

        ...

        {
                1 => { country => "Thailand" },
                2 => { country => "Indonesia" },
                3 => { country => "Japan" },
                4 => { country => "Cambodia" },
        }

        ===================================================

        my $json = '{
                "for": {
                        "key": "countries",
                        "keys": "countries",
                        "country": "{country}"
                },
        }';

        $json = $c->compile($json, {
                countries => {
                        1 => { country => "Thailand" },
                        2 => { country => "Indonesia" },
                        3 => { country => "Japan" },
                        4 => { country => "Cambodia" },
                }
        }, 1);

        ...

        {
                countries => {
                        1 => { country => "Thailand" },
                        2 => { country => "Indonesia" },
                        3 => { country => "Japan" },
                        4 => { country => "Cambodia" },
                }
        }

AUTHOR

LNATION, <email at lnation.org>

BUGS

Please report any bugs or feature requests to bug-json-conditional at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=JSON-Conditional. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

You can find documentation for this module with the perldoc command.

        perldoc JSON::Conditional

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

This software is Copyright (c) 2020 by LNATION.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)