now with thaw as well as freeze, see TODOs
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / JSON.pm
CommitLineData
e59193fb 1
2package MooseX::Storage::JSON;
3use Moose::Role;
4
5with 'MooseX::Storage::Base';
6
e9739624 7use JSON::Syck ();
e59193fb 8use MooseX::Storage::Engine;
9
e9739624 10sub pack {
11 my $self = shift;
12 my $e = MooseX::Storage::Engine->new(object => $self);
13 $e->collapse_object;
14}
15
16sub unpack {
17 my ($class, $data) = @_;
18 my $e = MooseX::Storage::Engine->new(class => $class);
19 $class->new($e->expand_object($data));
20}
21
22sub load {
23 my ($class, $filename) = @_;
24 $class->unpack(JSON::Syck::LoadFile($filename));
25}
26
27sub store {
28 my ($self, $filename) = @_;
29 JSON::Syck::DumpFile($filename, $self->pack());
30}
31
32sub thaw {
33 my ($class, $json) = @_;
34 $class->unpack(JSON::Syck::Load($json));
35}
e59193fb 36
37sub freeze {
38 my $self = shift;
e9739624 39 JSON::Syck::Dump($self->pack());
e59193fb 40}
41
e59193fb 421;
e9739624 43
44__END__
45
46=pod
47
48=cut
49