NAME
JSON::JOM::Plugins - plugin architecture
DESCRIPTION
Most of the interesting JOM functionality is designed to be implemented via plugins.
Here's a skeleton plugin:
package JSON::JOM::Plugins::ListUtils;
sub extensions
{
my ($class) = @_;
return (
## [ target, method, coderef ] ,
['ARRAY', 'count', sub { return scalar @{$_[0]}; }],
['ARRAY', 'values', sub { return @{$_[0]}; }],
['HASH', 'count', sub { return scalar keys %{$_[0]}; }],
['HASH', 'keys', sub { return keys %{$_[0]}; }],
['HASH', 'values', sub { return values %{$_[0]}; }],
);
}
1;
This adds methods count
and values
to JSON::JOM::Array objects and count
, keys
and values
to JSON::JOM::Object objects.
Valid targets for plugins are 'ARRAY', 'HASH', 'NULL', 'BOOLEAN', 'NUMBER' and 'STRING'. The target 'NODE' is effectively a wild card. Differentiating between numbers and strings may not always work as expected, so it's recommended that any plugin that targets numbers should also target strings, and vice versa.
e.g.
use JSON::JOM 'to_jom';
my $jom = to_jom([1,2,3,4,5,6,7,8,9]);
printf("Object has %d values:\n", $jom->count);
print "$_\n" foreach $jom->values;
JOM plugins must be named JSON::JOM::Plugins::pluginname or else JOM won't be able to load them.
Note that if you're developing code that uses JOM plugins, you don't need to do anything to load a plugin - JOM automatically loads all installed plugins. However, you can require
or use
a plugin if you want to double-check that it's installed before continuing:
my $jom = to_jom({...});
require JSON::JOM::Plugins::Dumper;
print $jom->dump;
BUGS
Please report any bugs to http://rt.cpan.org/.
SEE ALSO
AUTHOR
Toby Inkster <tobyink@cpan.org>.
COPYRIGHT
Copyright 2010 Toby Inkster
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.