| 1 | package RPC::Lite::Serializer::XML; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base qw( RPC::Lite::Serializer ); | ||
| 5 | |||
| 6 | use RPC::Lite::Request; | ||
| 7 | use RPC::Lite::Response; | ||
| 8 | use RPC::Lite::Notification; | ||
| 9 | use RPC::Lite::Error; | ||
| 10 | use RPC::Lite::Signature; | ||
| 11 | |||
| 12 | use XML::Simple; | ||
| 13 | |||
| 14 | use Data::Dumper; | ||
| 15 | |||
| 16 | our $DEBUG = $ENV{DEBUG_SERIALIZER}; | ||
| 17 | |||
| 18 | sub VersionSupported | ||
| 19 | { | ||
| 20 | my $self = shift; | ||
| 21 | my $version = shift; | ||
| 22 | |||
| 23 | # FIXME make sure we support this version of serialization | ||
| 24 | return 1; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub GetVersion | ||
| 28 | { | ||
| 29 | my $self = shift; | ||
| 30 | return $XML::Simple::VERSION; # FIXME should be *this* module's version, not XML::Simple's. we may require a minimum version of XML::Simple in the use() of course. | ||
| 31 | } | ||
| 32 | |||
| 33 | sub Serialize | ||
| 34 | { | ||
| 35 | my $self = shift; | ||
| 36 | my $object = shift; | ||
| 37 | |||
| 38 | my $type = ref($object); | ||
| 39 | |||
| 40 | # plain perl data structures... | ||
| 41 | if(!defined($type)) | ||
| 42 | { | ||
| 43 | # no-op | ||
| 44 | } | ||
| 45 | elsif($type eq 'HASH') | ||
| 46 | { | ||
| 47 | # no-op | ||
| 48 | } | ||
| 49 | elsif($type eq 'ARRAY') | ||
| 50 | { | ||
| 51 | # no-op | ||
| 52 | } | ||
| 53 | elsif($type eq 'RPC::Lite::Request') | ||
| 54 | { | ||
| 55 | # effectively 'unbless' this thing | ||
| 56 | $object = | ||
| 57 | { | ||
| 58 | class => $type, | ||
| 59 | method => $object->{method}, | ||
| 60 | params => $object->{params}, # assume simple types, could recurse to attempt to throw real objects over the wire | ||
| 61 | id => $object->{id}, | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | elsif($type eq 'RPC::Lite::Response') | ||
| 65 | { | ||
| 66 | $object = | ||
| 67 | { | ||
| 68 | class => $type, | ||
| 69 | result => $object->{result}, # assume simple types | ||
| 70 | error => $object->{error}, | ||
| 71 | id => $object->{id}, | ||
| 72 | }; | ||
| 73 | } | ||
| 74 | elsif($type eq 'RPC::Lite::Notification') | ||
| 75 | { | ||
| 76 | $object = | ||
| 77 | { | ||
| 78 | class => $type, | ||
| 79 | params => $object->{params}, # assume simple types | ||
| 80 | method => $object->{method}, | ||
| 81 | id => $object->{id}, # will be undef | ||
| 82 | }; | ||
| 83 | } | ||
| 84 | elsif($type eq 'RPC::Lite::Error') | ||
| 85 | { | ||
| 86 | $object = | ||
| 87 | { | ||
| 88 | class => $type, | ||
| 89 | result => $object->{result}, # undef | ||
| 90 | error => $object->{error}, | ||
| 91 | id => $object->{id}, | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | elsif($type eq 'RPC::Lite::Signature') | ||
| 95 | { | ||
| 96 | $object = | ||
| 97 | { | ||
| 98 | class => $type, | ||
| 99 | signature => $object->AsString(), | ||
| 100 | }; | ||
| 101 | } | ||
| 102 | else # try our best | ||
| 103 | { | ||
| 104 | # unbless shit? | ||
| 105 | } | ||
| 106 | |||
| 107 | my $data; | ||
| 108 | { | ||
| 109 | # work around uninitialized value warning in XML::Simple (keeps test output clean) | ||
| 110 | local $^W = 0; | ||
| 111 | $data = XMLout( $object, RootName => 'rpc-lite' ); | ||
| 112 | } | ||
| 113 | |||
| 114 | $self->_Debug('Serializing', Dumper($object), $data) if($DEBUG); | ||
| 115 | |||
| 116 | return $data; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub Deserialize | ||
| 120 | { | ||
| 121 | my $self = shift; | ||
| 122 | my $data = shift; | ||
| 123 | |||
| 124 | length($data) or return undef; | ||
| 125 | |||
| 126 | my $object = XMLin( $data, ForceArray => ['params'] ); | ||
| 127 | |||
| 128 | my $result = $object; | ||
| 129 | |||
| 130 | if(defined($object->{class})) | ||
| 131 | { | ||
| 132 | my $type = delete $object->{class}; | ||
| 133 | |||
| 134 | if($type eq 'RPC::Lite::Request') | ||
| 135 | { | ||
| 136 | my $params = $object->{params} || []; | ||
| 137 | $result = $type->new($object->{method}, $params); | ||
| 138 | $result->Id($object->{id}); | ||
| 139 | } | ||
| 140 | elsif($type eq 'RPC::Lite::Response') | ||
| 141 | { | ||
| 142 | $result = $type->new($object->{result}); | ||
| 143 | $result->Id($object->{id}); | ||
| 144 | } | ||
| 145 | elsif($type eq 'RPC::Lite::Notification') | ||
| 146 | { | ||
| 147 | my $params = $object->{params} || []; | ||
| 148 | $result = $type->new($object->{method}, $params); | ||
| 149 | } | ||
| 150 | elsif($type eq 'RPC::Lite::Error') | ||
| 151 | { | ||
| 152 | $result = $type->new($object->{error}); | ||
| 153 | $result->Id($object->{id}); | ||
| 154 | } | ||
| 155 | elsif($type eq 'RPC::Lite::Signature') | ||
| 156 | { | ||
| 157 | $result = $type->new($object->{signature}); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | $self->_Debug('Deserializing', $data, Dumper($result)) if($DEBUG); | ||
| 162 | |||
| 163 | return $result; | ||
| 164 | } | ||
| 165 | |||
| 166 | sub _Debug | ||
| 167 | { | ||
| 168 | my $self = shift; | ||
| 169 | my $action = shift; | ||
| 170 | my $input = shift; | ||
| 171 | my $output = shift; | ||
| 172 | |||
| 173 | print qq{ | ||
| 174 | $action | ||
| 175 | =================================================================== | ||
| 176 | |||
| 177 | input: | ||
| 178 | |||
| 179 | $input | ||
| 180 | |||
| 181 | output: | ||
| 182 | |||
| 183 | $output | ||
| 184 | |||
| 185 | =================================================================== | ||
| 186 | |||
| 187 | }; | ||
| 188 | } | ||
| 189 | |||
| 190 | 1; |