123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540 |
-
- using ProtoBuf.Meta;
- using System;
- using System.IO;
- #if !NO_GENERICS
- using System.Collections.Generic;
- #endif
- #if FEAT_IKVM
- using Type = IKVM.Reflection.Type;
- using IKVM.Reflection;
- #else
- using System.Reflection;
- #endif
- namespace ProtoBuf
- {
-
-
-
-
-
-
-
-
-
-
- public
- #if FX11
- sealed
- #else
- static
- #endif
- class Serializer
- {
- #if FX11
- private Serializer() { }
- #endif
- #if !NO_RUNTIME && !NO_GENERICS
-
-
-
-
-
- public static string GetProto<T>()
- {
- return RuntimeTypeModel.Default.GetSchema(RuntimeTypeModel.Default.MapType(typeof(T)));
- }
-
-
-
- public static T DeepClone<T>(T instance)
- {
- return instance == null ? instance : (T)RuntimeTypeModel.Default.DeepClone(instance);
- }
-
-
-
-
-
-
-
-
-
- public static T Merge<T>(Stream source, T instance)
- {
- return (T)RuntimeTypeModel.Default.Deserialize(source, instance, typeof(T));
- }
-
-
-
-
-
-
- public static T Deserialize<T>(Stream source)
- {
- return (T) RuntimeTypeModel.Default.Deserialize(source, null, typeof(T));
- }
-
-
-
-
-
-
- public static object Deserialize(System.Type type, Stream source)
- {
- return RuntimeTypeModel.Default.Deserialize(source, null, type);
- }
-
-
-
-
-
- public static void Serialize<T>(Stream destination, T instance)
- {
- if(instance != null) {
- RuntimeTypeModel.Default.Serialize(destination, instance);
- }
- }
-
-
-
-
-
-
-
-
-
-
-
- public static TTo ChangeType<TFrom,TTo>(TFrom instance)
- {
- using (MemoryStream ms = new MemoryStream())
- {
- Serialize<TFrom>(ms, instance);
- ms.Position = 0;
- return Deserialize<TTo>(ms);
- }
- }
- #if PLAT_BINARYFORMATTER && !(WINRT || PHONE8 || COREFX)
-
-
-
-
-
-
- public static void Serialize<T>(System.Runtime.Serialization.SerializationInfo info, T instance) where T : class, System.Runtime.Serialization.ISerializable
- {
- Serialize<T>(info, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Persistence), instance);
- }
-
-
-
-
-
-
-
- public static void Serialize<T>(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, T instance) where T : class, System.Runtime.Serialization.ISerializable
- {
-
- if (info == null) throw new ArgumentNullException("info");
- if (instance == null) throw new ArgumentNullException("instance");
- if (instance.GetType() != typeof(T)) throw new ArgumentException("Incorrect type", "instance");
- using (MemoryStream ms = new MemoryStream())
- {
- RuntimeTypeModel.Default.Serialize(ms, instance, context);
- info.AddValue(ProtoBinaryField, ms.ToArray());
- }
- }
- #endif
- #if PLAT_XMLSERIALIZER
-
-
-
-
-
-
- public static void Serialize<T>(System.Xml.XmlWriter writer, T instance) where T : System.Xml.Serialization.IXmlSerializable
- {
- if (writer == null) throw new ArgumentNullException("writer");
- if (instance == null) throw new ArgumentNullException("instance");
- using (MemoryStream ms = new MemoryStream())
- {
- Serializer.Serialize(ms, instance);
- writer.WriteBase64(Helpers.GetBuffer(ms), 0, (int)ms.Length);
- }
- }
-
-
-
-
-
-
- public static void Merge<T>(System.Xml.XmlReader reader, T instance) where T : System.Xml.Serialization.IXmlSerializable
- {
- if (reader == null) throw new ArgumentNullException("reader");
- if (instance == null) throw new ArgumentNullException("instance");
- const int LEN = 4096;
- byte[] buffer = new byte[LEN];
- int read;
- using (MemoryStream ms = new MemoryStream())
- {
- int depth = reader.Depth;
- while(reader.Read() && reader.Depth > depth)
- {
- if (reader.NodeType == System.Xml.XmlNodeType.Text)
- {
- while ((read = reader.ReadContentAsBase64(buffer, 0, LEN)) > 0)
- {
- ms.Write(buffer, 0, read);
- }
- if (reader.Depth <= depth) break;
- }
- }
- ms.Position = 0;
- Serializer.Merge(ms, instance);
- }
- }
- #endif
- private const string ProtoBinaryField = "proto";
- #if PLAT_BINARYFORMATTER && !NO_GENERICS && !(WINRT || PHONE8 || COREFX)
-
-
-
-
-
-
- public static void Merge<T>(System.Runtime.Serialization.SerializationInfo info, T instance) where T : class, System.Runtime.Serialization.ISerializable
- {
- Merge<T>(info, new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.Persistence), instance);
- }
-
-
-
-
-
-
-
- public static void Merge<T>(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context, T instance) where T : class, System.Runtime.Serialization.ISerializable
- {
-
- if (info == null) throw new ArgumentNullException("info");
- if (instance == null) throw new ArgumentNullException("instance");
- if (instance.GetType() != typeof(T)) throw new ArgumentException("Incorrect type", "instance");
- byte[] buffer = (byte[])info.GetValue(ProtoBinaryField, typeof(byte[]));
- using (MemoryStream ms = new MemoryStream(buffer))
- {
- T result = (T)RuntimeTypeModel.Default.Deserialize(ms, instance, typeof(T), context);
- if (!ReferenceEquals(result, instance))
- {
- throw new ProtoException("Deserialization changed the instance; cannot succeed.");
- }
- }
- }
- #endif
- #if !NO_GENERICS
-
-
-
- public static void PrepareSerializer<T>()
- {
- #if FEAT_COMPILER
- RuntimeTypeModel model = RuntimeTypeModel.Default;
- model[model.MapType(typeof(T))].CompileInPlace();
- #endif
- }
- #if PLAT_BINARYFORMATTER && !(WINRT || PHONE8 || COREFX)
-
-
-
-
-
- public static System.Runtime.Serialization.IFormatter CreateFormatter<T>()
- {
- #if FEAT_IKVM
- throw new NotSupportedException();
- #else
- return RuntimeTypeModel.Default.CreateFormatter(typeof(T));
- #endif
- }
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public static IEnumerable<T> DeserializeItems<T>(Stream source, PrefixStyle style, int fieldNumber)
- {
- return RuntimeTypeModel.Default.DeserializeItems<T>(source, style, fieldNumber);
- }
-
-
-
-
-
-
-
-
- public static T DeserializeWithLengthPrefix<T>(Stream source, PrefixStyle style)
- {
- return DeserializeWithLengthPrefix<T>(source, style, 0);
- }
-
-
-
-
-
-
-
-
-
- public static T DeserializeWithLengthPrefix<T>(Stream source, PrefixStyle style, int fieldNumber)
- {
- RuntimeTypeModel model = RuntimeTypeModel.Default;
- return (T)model.DeserializeWithLengthPrefix(source, null, model.MapType(typeof(T)), style, fieldNumber);
- }
-
-
-
-
-
-
-
-
-
-
-
- public static T MergeWithLengthPrefix<T>(Stream source, T instance, PrefixStyle style)
- {
- RuntimeTypeModel model = RuntimeTypeModel.Default;
- return (T)model.DeserializeWithLengthPrefix(source, instance, model.MapType(typeof(T)), style, 0);
- }
-
-
-
-
-
-
-
-
-
-
- public static void SerializeWithLengthPrefix<T>(Stream destination, T instance, PrefixStyle style)
- {
- SerializeWithLengthPrefix<T>(destination, instance, style, 0);
- }
-
-
-
-
-
-
-
-
-
-
-
- public static void SerializeWithLengthPrefix<T>(Stream destination, T instance, PrefixStyle style, int fieldNumber)
- {
- RuntimeTypeModel model = RuntimeTypeModel.Default;
- model.SerializeWithLengthPrefix(destination, instance, model.MapType(typeof(T)), style, fieldNumber);
- }
- #endif
-
-
-
-
-
- public static bool TryReadLengthPrefix(Stream source, PrefixStyle style, out int length)
- {
- int fieldNumber, bytesRead;
- length = ProtoReader.ReadLengthPrefix(source, false, style, out fieldNumber, out bytesRead);
- return bytesRead > 0;
- }
-
-
-
-
-
-
-
- public static bool TryReadLengthPrefix(byte[] buffer, int index, int count, PrefixStyle style, out int length)
- {
- using (Stream source = new MemoryStream(buffer, index, count))
- {
- return TryReadLengthPrefix(source, style, out length);
- }
- }
- #endif
-
-
-
-
- public const int ListItemTag = 1;
- #if !NO_RUNTIME
-
-
-
- public
- #if FX11
- sealed
- #else
- static
- #endif
- class NonGeneric
- {
- #if FX11
- private NonGeneric() { }
- #endif
-
-
-
- public static object DeepClone(object instance)
- {
- return instance == null ? null : RuntimeTypeModel.Default.DeepClone(instance);
- }
-
-
-
-
-
- public static void Serialize(Stream dest, object instance)
- {
- if (instance != null)
- {
- RuntimeTypeModel.Default.Serialize(dest, instance);
- }
- }
-
-
-
-
-
-
- public static object Deserialize(System.Type type, Stream source)
- {
- return RuntimeTypeModel.Default.Deserialize(source, null, type);
- }
-
-
-
-
- public static object Merge(Stream source, object instance)
- {
- if (instance == null) throw new ArgumentNullException("instance");
- return RuntimeTypeModel.Default.Deserialize(source, instance, instance.GetType(), null);
- }
-
-
-
-
-
-
-
-
-
-
- public static void SerializeWithLengthPrefix(Stream destination, object instance, PrefixStyle style, int fieldNumber)
- {
- if (instance == null) throw new ArgumentNullException("instance");
- RuntimeTypeModel model = RuntimeTypeModel.Default;
- model.SerializeWithLengthPrefix(destination, instance, model.MapType(instance.GetType()), style, fieldNumber);
- }
-
-
-
-
-
-
-
-
-
-
-
- public static bool TryDeserializeWithLengthPrefix(Stream source, PrefixStyle style, TypeResolver resolver, out object value)
- {
- value = RuntimeTypeModel.Default.DeserializeWithLengthPrefix(source, null, null, style, 0, resolver);
- return value != null;
- }
-
-
-
- public static bool CanSerialize(Type type)
- {
- return RuntimeTypeModel.Default.IsDefined(type);
- }
- }
-
-
-
- public
- #if FX11
- sealed
- #else
- static
- #endif
- class GlobalOptions
- {
- #if FX11
- private GlobalOptions() { }
- #endif
-
-
-
- [Obsolete("Please use RuntimeTypeModel.Default.InferTagFromNameDefault instead (or on a per-model basis)", false)]
- public static bool InferTagFromName
- {
- get { return RuntimeTypeModel.Default.InferTagFromNameDefault; }
- set { RuntimeTypeModel.Default.InferTagFromNameDefault = value; }
- }
- }
- #endif
-
-
-
- public delegate Type TypeResolver(int fieldNumber);
-
-
-
-
-
- public static void FlushPool()
- {
- BufferPool.Flush();
- }
- }
- }
|