The PropertyBag Serializer

PocketSOAP includes a serializer that will work with the standard COM property bag serialization model. This is what VB uses when you set a VB class to be Persistable. This make it easy to serialize VB objects to/from SOAP, in many cases this can save you from having to write a custom serializer.

See the samples page, for the actual VB projects for the this code.


So, we'll take our trusty address/phone sample from earlier, and use the property bag serializer to go to/from SOAP.
	<street>Spear Street</street>
	<city>San Francisco</city>
So, we have a simple telephone class with 3 data fields, set it to peristable, and write the persistance code to read/write the values to the property bag
Option Explicit

Public area As String
Public exchange As String
Public number As String

Private Sub Class_ReadProperties(PropBag As PropertyBag)
    area = PropBag.ReadProperty("area")
    exchange = PropBag.ReadProperty("exchange")
    number = PropBag.ReadProperty("number")
End Sub

Private Sub Class_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "area", area
    PropBag.WriteProperty "exchange", exchange
    PropBag.WriteProperty "number", number
End Sub
We do the same for address
Option Explicit

Public number As String
Public street As String
Public city As String
Public state As String
Public zip As String

Public phone As telephone

Private Sub Class_Initialize()
    Set phone = New telephone
End Sub

Private Sub Class_ReadProperties(p As PropertyBag)
    number = p.ReadProperty("number")
    street = p.ReadProperty("street")
    city = p.ReadProperty("city")
    state = p.ReadProperty("state")
    zip = p.ReadProperty("zip")
    Set phone = p.ReadProperty("phone")
End Sub

Private Sub Class_WriteProperties(p As PropertyBag)
    p.WriteProperty "number", number
    p.WriteProperty "street", street
    p.WriteProperty "city", city
    p.WriteProperty "state", state
    p.WriteProperty "zip", zip
    p.WriteProperty "phone", phone
End Sub

Note that we don't have to do anything special for the nested phone object.

The default configuration for the serializer factory automatically maps the propertybag serializer to any object that can be serialized with it, so we don't need to do anything special for sending one of these.

Dim e As CoEnvelope
Set e = New CoEnvelope
e.SetMethod "setaddress", ""
e.Parameters.Create "address", GetAddress
MsgBox e.Serialize

Private Function GetAddress() As address
    Dim a As address
    Set a = New address
    a.Number = "100"
    a.street = "Spear Street" = "San Francisco"
    a.state = "CA" = "94105" = "415" = "343" = "3000"
    Set GetAddress = a
End Function
As the address and phone objects are persistable pocketSOAP will automatically serialize them with the property bag serializer, generating this SOAP message
  <address href='#a'/>
<phone E:root='0' id='b'>
 <area b:type='c:string'>415</area>
 <exchange b:type='c:string'>343</exchange>
 <number b:type='c:string'>3000</number>
<address E:root='0' id='a'>
 <number b:type='c:string'>100</number>
 <street b:type='c:string'>Spear Street</street>
 <city b:type='c:string'>San Francisco</city>
 <state b:type='c:string'>CA</state>
 <zip b:type='c:string'>94105</zip>
 <phone href='#b'/>

De-serializing one is only slightly more complicated, we just need to register a propertybag deserializer to handle the particular types. In this particular case, there is no xsi:type, so we also register element -> type mappings as well.

Dim e As CoEnvelope
Set e = New CoEnvelope
Dim sf As ISerializerFactoryConfig
Set sf = e.SerializerFactory
sf.ElementMapping "phone", "", "phone", ""
sf.ElementMapping "address", "", "address", ""
sf.Deserializer "phone", "", False, "ps.telephone", "pocketSOAP.SerializerPB.1"
sf.Deserializer "address", "", False, "ps.address", "pocketSOAP.SerializerPB.1"
e.SetMethod "setaddress", ""
e.Parameters.Create "address", GetAddress
Dim s As String
s = e.Serialize
' parse the soap message we just generated   
e.Parse s
Dim a As address
Set a = e.Parameters.Item(0).Value
res.Text = a.Number & " " & a.street & "," & vbCrLf
res.Text = res.Text + & "," & vbCrLf & a.state & " " & & vbCrLf
res.Text = res.Text + & " " & & " " &


Copyright © Simon Fell, 2000-2004. All rights reserved.