public static void Serialise <T>(Stream fs, ESerialisationFlags format, T value) { Stream fsu; switch (format & ESerialisationFlags._CompressMask) { case ESerialisationFlags.GZip: fsu = new GZipStream(fs, CompressionMode.Compress); break; case ESerialisationFlags.NoCompression: fsu = fs; break; default: throw new SwitchException("format", format); } try { switch (format & ESerialisationFlags._SerialiserMask) { case ESerialisationFlags.Binary: BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(fsu, value); break; case ESerialisationFlags.Xml: XmlSerializer xs = new XmlSerializer(typeof(T)); xs.Serialize(fsu, value); break; case ESerialisationFlags.Ini: IniSerialiser.Serialise <T>(fsu, value); break; default: throw new SwitchException("format", format); } } finally { switch (format & ESerialisationFlags._CompressMask) { case ESerialisationFlags.GZip: fsu.Dispose(); break; case ESerialisationFlags.NoCompression: break; default: throw new SwitchException(format); } } }
public static T Deserialise <T>(FileStream fs, ESerialisationFlags format) { Stream fsu; switch (format & ESerialisationFlags._CompressMask) { case ESerialisationFlags.GZip: fsu = new GZipStream(fs, CompressionMode.Decompress); break; case ESerialisationFlags.NoCompression: fsu = fs; break; default: throw new SwitchException(format); } try { switch (format & ESerialisationFlags._SerialiserMask) { case ESerialisationFlags.Binary: BinaryFormatter bf = new BinaryFormatter(); return((T)bf.Deserialize(fsu)); case ESerialisationFlags.Xml: XmlSerializer xs = new XmlSerializer(typeof(T)); return((T)xs.Deserialize(fsu)); case ESerialisationFlags.Ini: return(IniSerialiser.Deserialise <T>(fsu)); default: throw new SwitchException("format", format); } } finally { switch (format & ESerialisationFlags._CompressMask) { case ESerialisationFlags.GZip: fsu.Dispose(); break; case ESerialisationFlags.NoCompression: break; default: throw new SwitchException(format); } } }
private static void _Serialise <T>(T data, Stream s, SerialisationFormat format, ObjectSerialiser serialiser, ProgressReporter prog) { switch (format) { case SerialisationFormat.MSerialiserBinary: MSerialiser.SerialiseStream(s, data, ETransmission.Binary, new[] { serialiser }, null); return; case SerialisationFormat.MSerialiserText: MSerialiser.SerialiseStream(s, data, ETransmission.Text, new[] { serialiser }, null); return; case SerialisationFormat.MSerialiserCompactBinary: MSerialiser.SerialiseStream(s, data, ETransmission.CompactBinary, new[] { serialiser }, null); return; case SerialisationFormat.MSerialiserFastBinary: MSerialiser.SerialiseStream(s, data, ETransmission.FastBinary, new[] { serialiser }, null); return; case SerialisationFormat.Xml: var xs = _CreateXmlSerialiser <T>(); xs.Serialize(s, data); break; case SerialisationFormat.DataContract: var dcs = _CreateDataContactSerialiser <T>(); dcs.WriteObject(s, (object)data); break; case SerialisationFormat.MsnrbfBinary: var bcs = new BinaryFormatter(); bcs.Serialize(s, data); break; case SerialisationFormat.Ini: IniSerialiser.Serialise(s, data); break; default: throw new InvalidOperationException("Invalid switch: " + format); } }
private static T _Deserialise <T>(Stream s, SerialisationFormat format, ObjectSerialiser serialiser, ProgressReporter prog) { switch (format) { case SerialisationFormat.MSerialiserBinary: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.Binary, new[] { serialiser }, null)); case SerialisationFormat.MSerialiserText: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.Text, new[] { serialiser }, null)); case SerialisationFormat.MSerialiserCompactBinary: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.CompactBinary, new[] { serialiser }, null)); case SerialisationFormat.MSerialiserFastBinary: return(MSerialiser.DeserialiseStream <T>(s, ETransmission.FastBinary, new[] { serialiser }, null)); case SerialisationFormat.Xml: var xs = _CreateXmlSerialiser <T>(); return((T)xs.Deserialize(s)); case SerialisationFormat.DataContract: var dcs = _CreateDataContactSerialiser <T>(); return((T)dcs.ReadObject(s)); case SerialisationFormat.MsnrbfBinary: var bcs = new BinaryFormatter(); // bcs.Binder = new TypeNameConverter(); - We don't do this anymore, backwards compatibility ends up taking most of the development time. return((T)bcs.Deserialize(s)); case SerialisationFormat.Ini: return(IniSerialiser.Deserialise <T>(s)); default: throw new InvalidOperationException("Invalid switch: " + format); } }