What it Does
Sample application 3 demonstrates how the MethodInvocationSerializer class can be extended by adding and overriding the following functionality...
- Add support for serialization of a custom class
- Override the default serialization of boolean data types
- Override the default XML tag names used to serialize method invocations
Code Analysis
Class CustomMethodInvocationSerializer is created, deriving from the MethodInvocationSerializer class. Within the C# implementation of CustomMethodInvocationSerializer, private methods SerializeInterestRateCurve() and DeserializeInterestRateCurve() perform serialization and deserialization of custom object InterestRateCurve. References to these 2 methods are then added to the ISerializerOperationMap member of the base class, via the following method call...
operationMap.AddMapping(typeof(InterestRateCurve), "interestRateCurve", new XmlSerializationOperation(SerializeInterestRateCurve), new XmlDeserializationOperation(DeserializeInterestRateCurve));
... to allow the CustomMethodInvocationSerializer to handle InterestRateCurve objects. In Java the process is similar, except the serialization and deserialization methods are defined through inner class 'InterestRateCurveSerializer' which is added the the ISerializerOperationMap member via the following code...
operationMap.AddMapping(InterestRateCurve.class, "interestRateCurve", new InterestRateCurveSerializer());
Additionally support for arrays of InterestRateCurve objects are added using the generic array serializer routines, using this method call in C#...
operationMap.AddMapping(typeof(InterestRateCurve[]), "interestRateCurveArray", new XmlSerializationOperation(SerializeArray), new XmlDeserializationOperation(DeserializeArray));
...and this in Java...
operationMap.AddMapping(InterestRateCurve[].class, "interestRateCurveArray", genericArraySerializer);
Whilst the MethodInvocationSerializer base class already has support for boolean types, in CustomMethodInvocationSerializer the serialization and deserialization routines are overridden with a custom implementation. The serialization routines are defined through methods CustomSerializeBoolean() and CustomDeserializeBoolean() in C#, and inner class CustomBooleanSerializer in Java. These routines then override the built in boolean type support using the ISerializerOperationMap UpdateMapping() method...
operationMap.UpdateMapping(typeof(Boolean), "bool", new XmlSerializationOperation(CustomSerializeBoolean), new XmlDeserializationOperation(CustomDeserializeBoolean));
... in C# and...
operationMap.UpdateMapping(Boolean.class, "bool", new CustomBooleanSerializer());
... in Java. Note that support for arrays of boolean types is already included in the MethodInvocationSerializer base class using the generic array serialization routines. Since these generic routines use the underlying boolean type serialization routines to serialize elements of arrays, you do not need to explicitly add or override support for arrays of boolean types.
In addition, you do not necessarily need to derive a new class from MethodInvocationSerializer to override or add serialization support for data types. The 2 steps described above could be applied directly to a MethodInvocationSerializer object by calling the AddMapping() and UpdateMapping() methods directly on its ISerializerOperationMap member.
The final changes in CustomMethodInvocationSerializer are to override the default XML tags used by MethodInvocationSerializer with shorter tags. This sacrifices readability of the serialized method invocations, but has the benefit of reducing the size of the XML document, particulary for arrays. The relevant base class fields are simply overridden as follows...
rootElementName = "MI";
methodNameElementName = "MN";
parametersElementName = "Ps";
parameterElementName = "P";
dataTypeElementName = "DT";
dataElementName = "D";
returnTypeElementName = "RT";
returnValueElementName = "RV";
arrayElementDataTypeElementName = "EDT";
arrayElementElementName = "E";
This results in a void method with a single boolean parameter being serialized to the following XML...
<?xml version="1.0" encoding="utf-8"?>
<MI>
<MN>TestMethodInvocation</MN>
<Ps>
<P>
<DT>bool</DT>
<D>1</D>
</P>
</Ps>
<RT/>
</MI>
Finally the MethodInvocationSerializer 'doubleFloatingPointDigits' field is overridden to reduce the number of digits used when serializing floating point types, using the following statement...
doubleFloatingPointDigits = 10;
Running the sample application creates a CustomMethodInvocationSerializer object, which is used to serialize and deserialize the new and overridden data types, writing the results to the console...
<?xml version="1.0" encoding="utf-8"?><MI><MN>TestMethodInvocation</MN><Ps><P><DT>interestRateCurve</DT><D><Currency>AUD</Currency><Curve><Point><Term>24</Term><Rate>2.56</Rate></Point><Point><Term>180</Term><Rate>3.82</Rate></Point><Point><Term>120</Term><Rate>3.45</Rate></Point><Point><Term>60</Term><Rate>2.85</Rate></Point></Curve></D></P><P><DT>interestRateCurveArray</DT><D><EDT>interestRateCurve</EDT><E><DT>interestRateCurve</DT><D><Currency>GBP</Currency><Curve><Point><Term>24</Term><Rate>0.75</Rate></Point><Point><Term>180</Term><Rate>2.86</Rate></Point><Point><Term>120</Term><Rate>2.31</Rate></Point><Point><Term>60</Term><Rate>1.37</Rate></Point></Curve></D></E><E><DT>interestRateCurve</DT><D><Currency>JPY</Currency><Curve><Point><Term>24</Term><Rate>0.18583</Rate></Point><Point><Term>180</Term><Rate>1.65875</Rate></Point><Point><Term>120</Term><Rate>0.83771</Rate></Point><Point><Term>60</Term><Rate>0.38375</Rate></Point></Curve></D></E><E><DT>interestRateCurve</DT><D><Currency>USD</Currency><Curve><Point><Term>24</Term><Rate>0.35</Rate></Point><Point><Term>180</Term><Rate>2.36</Rate></Point><Point><Term>120</Term><Rate>1.93</Rate></Point><Point><Term>60</Term><Rate>0.8</Rate></Point></Curve></D></E></D></P><P><DT>bool</DT><D>1</D></P><P><DT>boolArray</DT><D><EDT>bool</EDT><E><DT>bool</DT><D>0</D></E><E><DT>bool</DT><D>1</D></E><E><DT>bool</DT><D>0</D></E></D></P><P><DT>double</DT><D>3.1415926536E0</D></P></Ps><RT></RT></MI>
-- Interest Rate Curve Parameter --
Currency: AUD
24 2.56
180 3.82
120 3.45
60 2.85
-- Interest Rate Curve Array Parameter --
Currency: GBP
24 0.75
180 2.86
120 2.31
60 1.37
Currency: JPY
24 0.18583
180 1.65875
120 0.83771
60 0.38375
Currency: USD
24 0.35
180 2.36
120 1.93
60 0.8
-- Boolean Parameter --
Value: true
-- Boolean Array Parameter --
Element 1 value: false
Element 2 value: true
Element 3 value: false
-- Double Parameter --
Value: 3.1415926536
Note that if logging is enabled, additional log information may be output to the console along with the above information.