Data::Phrasebook::Loader::JSON::Syck - A Data::Phrasebook loader for JSON files


Data-Phrasebook-Loader-JSON-Syck documentation Contained in the Data-Phrasebook-Loader-JSON-Syck distribution.

Index


Code Index:

NAME

Top

Data::Phrasebook::Loader::JSON::Syck - A Data::Phrasebook loader for JSON files

SYNOPSIS

Top

  my $p = Data::Phrasebook->new(
      class  => 'Plain',
      loader => 'JSON::Syck',
      file   => 'errors.json',
  );

  # now use the phrasebook like any other ...
  warn $p->fetch('FAILED_LOGIN', { message => 'Could not find user'});

DESCRIPTION

Top

This is a Data::Phrasebook loader which will load phasebooks stored in JSON. It uses the very nice and very fast JSON::Syck parser to load the JSON data.

You should refer to the Data::Phrasebook documentation for more information on how to use this module.

EXPECTED JSON FORMAT

Top

This module expects that the JSON returned from JSON::Syck::Load($json) will be a HASH reference. This is fairly simple as long as your top level data structure is a JSON hash. Here is an example from the test suite.

  {
      foo: "Welcome to [% my %] world. It is a nice [%place %].",
      bar: "Welcome to :my world. It is a nice :place."
  }

CAVEATS

Top

This phrasebook loader does not yet support multiple phrasebook dictionaries. Future plans do include supporting these, and we will retain backwards compatability with the JSON file format.

WHY JSON?

Top

Because it is Plain Ole Javascript. If you need to share your phrasebook data (error messages and such) with Javascript, this is the ideal format.

METHODS

Top

These methods are those required by Data::Phrasebook::Loader, they do not represent the API to this module. Please refer to the Data::Phrasebook for that information.

load ($filename)
get ($key)

SEE ALSO

Top

Data::Phrasebook
JSON::Syck
http://www.json.org/

BUGS

Top

All complex software has bugs lurking in it, and this module is no exception. If you find a bug please either email me, or add the bug to cpan-RT.

AUTHOR

Top

Stevan Little <stevan@iinteractive.com>

COPYRIGHT AND LICENSE

Top


Data-Phrasebook-Loader-JSON-Syck documentation Contained in the Data-Phrasebook-Loader-JSON-Syck distribution.

package Data::Phrasebook::Loader::JSON::Syck;

use strict;
use warnings;

use Carp        'croak';
use JSON::Syck  ();
use File::Slurp ();

use Data::Phrasebook;

our $VERSION = '0.01';

use base 'Data::Phrasebook::Loader::Base';

sub load {
    my ($class, $filename) = @_;
    (defined $filename)
        || croak "No file given as argument!";
    (-e $filename)
        || croak "The file given '$filename' could not be found";        
    my $json = File::Slurp::slurp($filename) or croak "Could not slurp JSON file '$filename' got no data";
    my $d    = JSON::Syck::Load($json);
    (ref($d) eq 'HASH')
        || croak "Badly formatted JSON file '$filename'";
	$class->{JSON} = $d;    
}

sub get { 
	my ($class, $key) = @_;
	return undef unless $key;
	return undef unless $class->{JSON};
	$class->{JSON}->{$key};    
}

#sub dicts    { return () }
#sub keywords { return () }

1;

__END__