Loading ...
Sorry, an error occurred while loading the content.

Re: [soaplite] custom Serializer

Expand Messages
  • Paul Kulchenko
    Hi, Alexandre! ... Interesting. Here is the code that will do it for you, but you won t be able to get in output id attribute, since unqualified id and
    Message 1 of 2 , Jul 3 10:53 AM
    • 0 Attachment
      Hi, Alexandre!

      > I'm trying to write a custom serializer. It will need to serialize
      > a simple class but instead of writing all members as XML elements,
      > some members will be output as element attributes:
      Interesting. Here is the code that will do it for you, but you won't
      be able to get in output 'id' attribute, since unqualified 'id' and
      'href' attributes are reserved for SOAP encoding. You should either
      qualify those (a:id xmlns:a="some your namespace") or rename to
      something else. You MAY also put 'id' in envelope using 'xml'
      pseudotype, I just want to highlight that you won't see 'id'
      attribute as you want it. Code:

      use SOAP::Lite;

      BEGIN {
      package My::Serializer; @My::Serializer::ISA = 'SOAP::Serializer';

      # nothing special here. as_OBJECT_TYPE() method will catch
      serialization
      # of the specified type (use '__' instead of '::'), so object of
      this
      # type will be properly serializer even being inside complex data
      structures
      sub as_My__Class {
      my $self = shift;
      my($value, $name, $type, $attr) = @_;

      my %hash = %$value;

      # encode some elements as attributes
      foreach (qw(something)) {
      $attr->{$_} = delete $hash{$_} if exists $hash{$_};
      }

      return [
      $name || $self->SUPER::gen_name(), # name
      {%$attr, 'xsi:type' => $self->maptypetouri($type)}, #
      attributes (optional)
      [map {$self->encode_object($hash{$_}, $_)} keys %hash], # value
      $self->gen_id($value), # multiref
      id (optional)
      ];
      }
      }

      my $object = bless { something => 5, a => 1, b => 2 } => 'My::Class';

      print My::Serializer->serialize($object);

      will generate

      <My__Class ...namespaces skipped...
      something="5" xsi:type="My__Class">
      <a xsi:type="xsd:int">1</a>
      <b xsi:type="xsd:int">2</b>
      </My__Class>

      however

      my $object = bless { id => 5, a => 1, b => 2 } => 'My::Class';

      will NOT give you 'id' in attributes. You MAY also add prefixes using
      this interface, you may do:

      for (....) {
      $attr->{"a:$_"} = $hash{$_}
      }

      $attr->{'xmlns:a'} = 'my namespace';

      or do something like this. Next version has interface that allows you
      to manage single namespace/prefix for all your elements. Let me know
      if something is unclear here.

      Best wishes, Paul.

      --- Alexandre Maret <amaret@...> wrote:
      > hi
      >
      > I'm trying to write a custom serializer. It will need to serialize
      > a
      > simple class but instead of writing all members as XML elements,
      > some
      > members will be output as element attributes:
      >
      > what I get using default serializer:
      >
      > <item>
      > <id>...</id>
      > <value1>...</value1>
      > <value2>...</value2>
      > </item>
      >
      > what I need:
      >
      > <item id="">
      > <value1>...</value1>
      > <value2>...</value2>
      > </item>
      >
      > If I implement SOAP::Serializer::as_item(), what is the 3rd return
      > value's
      > type? Examples are only provided for very simple cases...
      >
      > Does anyone have a sample "transparent" SOAP::Serializer::as_XXX
      > method
      > that just calls the underlying Serializer methods, one that's
      > working on a
      > hash would be great?
      >
      > any pointers appreciated...
      >
      > thanks in advance
      >
      > alex
      >
      >
      > To unsubscribe from this group, send an email to:
      > soaplite-unsubscribe@yahoogroups.com
      >
      >
      >
      > Your use of Yahoo! Groups is subject to
      > http://docs.yahoo.com/info/terms/
      >
      >


      __________________________________________________
      Do You Yahoo!?
      Get personalized email addresses from Yahoo! Mail
      http://personal.mail.yahoo.com/
    Your message has been successfully submitted and would be delivered to recipients shortly.