private void Serialise() { if (_text == null && _data != null) { var serialFormat = (SerialFormat)SysProps.GetValue(SysPropName.SAlg, 0); switch (serialFormat) { case SerialFormat.Binary: // try Binary serialiser _text = BinarySerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Binary); break; case SerialFormat.Soap: // try Soap serialiser _text = SoapSerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Soap); break; case SerialFormat.Json: // try Json serialiser _text = JsonSerializerHelper.SerializeToString(_data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Json); break; case SerialFormat.Xml: try { _text = XmlSerializerHelper.SerializeToString(_dataTypeType, _data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Xml); } catch (Exception excp) { throw new ApplicationException( "The XmlSerializer has thrown an exception: '" + excp.GetType().Name + "'. " + "If your intent was to use the BinaryFormatter or SoapFormatter for serialisation, " + "then you should set the SerialFormat property appropriately.", excp); } break; default: // use default xml serialiser try { _text = XmlSerializerHelper.SerializeToString(_dataTypeType, _data); SysProps.Set(SysPropName.SAlg, (int)SerialFormat.Xml); } catch (Exception excp) { throw new ApplicationException( "The XmlSerializer has thrown an exception: '" + excp.GetType().Name + "'. " + "If your intent was to use the BinaryFormatter or SoapFormatter for serialisation, " + "then you should set the SerialFormat property appropriately.", excp); } break; } } }
private void Deserialise(Type userDataType) { if (_data == null) { // decompress 1st if required Decompress(); // now deserialise Type dataTypeType = userDataType ?? _dataTypeType; if ((dataTypeType == null) && !String.IsNullOrEmpty(DataTypeName)) { dataTypeType = Type.GetType(DataTypeName); } var serialFormat = (SerialFormat)SysProps.GetValue(SysPropName.SAlg, (int)SerialFormat.Undefined); if (String.IsNullOrEmpty(_text)) { // null data object _data = null; } else if (serialFormat == SerialFormat.Binary) { // use Binary deserialiser _data = BinarySerializerHelper.DeserializeFromString(_text); } else if (serialFormat == SerialFormat.Soap) { // use Soap deserialiser _data = SoapSerializerHelper.DeserializeFromString(_text); } else if (serialFormat == SerialFormat.Json) { // use Json deserialiser _data = JsonSerializerHelper.DeserializeFromString(dataTypeType, _text); } else if (dataTypeType != null) { // try default xml serialiser _data = XmlSerializerHelper.DeserializeFromString(dataTypeType, _text); } else { throw new ApplicationException("Cannot deserialise!"); } } }