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