[Pharo-project] [squeak-dev] Any serializer can customize serialization strategy?

Masashi UMEZAWA masashi.umezawa at gmail.com
Sat Oct 15 13:26:43 UTC 2011


Hernán Morales Durand <hernan.morales at gmail.com> wrote:
>Is there any serializer (Fuel/StOMP/Ma object
>serialization/BOSS/SRP/SIXX??) which let me attach an external
>compressor/decompressor for specific Strings to the
>serialization/deserialization process?

Mariano Martinez Peck <marianopeck at gmail.com> wrote:
> I think StOMP should support this as well:
> http://stomp.smalltalk-users.jp/home/how-to-use-stomp/hook-methods

Yes. StOMP does support such kind of custom serialization.

Pattern 1: Using Memento.

StOMP has two basic hook methods (stompWriteValue/stompReadValue).
With a memento pattern, you can customize
serialization/deserialization behavior easily.

SpecificString>> stompWriteValue
 ^ StringCompressedMemento on: self

StringCompressedMemento>> stompReadValue
 ^ SpecificString on: self uncompressToString

SpecificString is a mere wrapper class (holding original string). And
StringCompressedMemento should implement compress/uncompress logic.

Pattern 2: Subclassing Writer/Reader

Pattern 1 is usable, but it is basically suitable for pointer objects.
For String-like objects, defining a new String wrapper and Memento is
overwhelming. You can also subclass StompWriter/Reader for adding
custom serialization/deserialization behaviors.

MyStompWriter>>writeString: aString
 (self isSpecificString: aString) ifTrue: [^super writeString: (self
compressString: aString)].
 ^ super writeString: string

 | rawString |
 rawString := self basicReadObject asString.
 ^ (self isCompressedString: rawString)
	ifTrue: [self uncompressString: rawString]
	ifFalse: [rawString]

You have to add some marking headers to the original string for
determining it should be compressed/uncompressed. (Yes. this is a
little awkward, but works well).

I've attached working sample codes. So please see for details.

Best regards,
[:masashi | ^umezawa]
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Stomp-Exmaple1.st
Type: application/octet-stream
Size: 3483 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20111015/bf9adfac/Stomp-Exmaple1.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Stomp-Example2.st
Type: application/octet-stream
Size: 2147 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20111015/bf9adfac/Stomp-Example2.obj

More information about the Squeak-dev mailing list