Commit | Line | Data |
e59193fb |
1 | |
2 | package MooseX::Storage::JSON; |
3 | use Moose::Role; |
4 | |
5 | with 'MooseX::Storage::Base'; |
6 | |
e9739624 |
7 | use JSON::Syck (); |
e59193fb |
8 | use MooseX::Storage::Engine; |
9 | |
e9739624 |
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 | } |
e59193fb |
36 | |
37 | sub freeze { |
38 | my $self = shift; |
e9739624 |
39 | JSON::Syck::Dump($self->pack()); |
e59193fb |
40 | } |
41 | |
e59193fb |
42 | 1; |
e9739624 |
43 | |
44 | __END__ |
45 | |
46 | =pod |
47 | |
48 | =cut |
49 | |