JSON::Conditional - The great new JSON::Conditional!
Version 0.02
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' } ] };
Instantiate a new JSON::Conditional object. Currently this expects no arguments.
my $c = JSON::Conditional->new;
Encode a perl struct into JSON.
Decode a JSON string into a perl struct.
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 a perl struct that contains valid JSON::Conditional markup and return a perl struct based upon the passed params.
$c->itterate($perl_struct, $params);
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 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 }
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 }
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 }
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 }
Does the params key value match the provided regex value.
{ "key": $param_key "m": $regex, "then": \%then }
Does the params key value not match the provided regex value.
{ "key": $param_key "nm": $regex, "then": \%then }
Does the params key value equal the provided value.
{ "key": $param_key "eq": $equals, "then": \%then }
Does the params key value not equal the provided value.
{ "key": $param_key "ne": $equals, "then": \%then }
Is the params key value greater than the provided value.
{ "key": $param_key "gt": $greater_than, "then": \%then }
Is the params key value less than the provided value.
{ "key": $param_key "lt": $greater_than, "then": \%then }
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}" }
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' } ] };
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" }, } }
LNATION, <email at lnation.org>
<email at lnation.org>
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.
bug-json-conditional at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc JSON::Conditional
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
https://rt.cpan.org/NoAuth/Bugs.html?Dist=JSON-Conditional
CPAN Ratings
https://cpanratings.perl.org/d/JSON-Conditional
Search CPAN
https://metacpan.org/release/JSON-Conditional
This software is Copyright (c) 2020 by LNATION.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install JSON::Conditional, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::Conditional
CPAN shell
perl -MCPAN -e shell install JSON::Conditional
For more information on module installation, please visit the detailed CPAN module installation guide.