private static void PackCore <T>(this Packer source, IEnumerable <T> items, SerializationContext context) { if (items == null) { source.PackNull(); return; } // ReSharper disable SuspiciousTypeConversion.Global var asPackable = items as IPackable; // ReSharper restore SuspiciousTypeConversion.Global if (asPackable != null) { asPackable.PackToMessage(source, new PackingOptions()); return; } var asCollection = items as ICollection <T>; if (asCollection == null) { asCollection = items.ToArray(); } var itemSerializer = context.GetSerializer <T>(); source.PackArrayHeader(asCollection.Count); foreach (var item in asCollection) { itemSerializer.PackTo(source, item); } }
private static void PackObjectCore(Packer source, object value, SerializationContext context) { /* * MessagePackSerializer.Create<T>( context ).PackTo( source, value ); */ if (value == null) { source.PackNull(); return; } var type = value.GetType(); var contextParameter = Expression.Parameter(typeof(SerializationContext), "context"); var packerParameter = Expression.Parameter(typeof(Packer), "packer"); var valueParameter = Expression.Parameter(typeof(object), "value"); Expression.Lambda <Action <SerializationContext, Packer, object> >( Expression.Call( Expression.Call( typeof(MessagePackSerializer).GetMethod("Create", _messagePackSerializer_Create_ParameterTypes).MakeGenericMethod(type), contextParameter ), typeof(MessagePackSerializer <>).MakeGenericType(type).GetMethod("PackTo"), packerParameter, Expression.Convert( valueParameter, type ) ), contextParameter, packerParameter, valueParameter ).Compile()(context, source, value); }
internal static void PackDictionaryCore <TKey, TValue>( Packer source, IDictionary <TKey, TValue> dictionary, MessagePackSerializer <TKey> keySerializer, MessagePackSerializer <TValue> valueSerializer) { // ReSharper disable once CompareNonConstrainedGenericWithNull if (dictionary == null) { source.PackNull(); return; } // ReSharper disable once SuspiciousTypeConversion.Global var asPackable = dictionary as IPackable; if (asPackable != null) { asPackable.PackToMessage(source, new PackingOptions()); return; } source.PackMapHeader(dictionary.Count); foreach (var entry in dictionary) { keySerializer.PackTo(source, entry.Key); valueSerializer.PackTo(source, entry.Value); } }
private static void PackObjectCore(Packer source, object value, SerializationContext context) { if (value == null) { source.PackNull(); } else { ParameterExpression expression; ParameterExpression expression2; ParameterExpression expression3; Type type = value.GetType(); Expression.Lambda <Action <SerializationContext, Packer, object> >(Expression.Call(Expression.Call(typeof(MessagePackSerializer).GetMethod("Create", _messagePackSerializer_Create_ParameterTypes).MakeGenericMethod(new Type[] { type }), new Expression[] { expression = Expression.Parameter(typeof(SerializationContext), "context") }), typeof(MessagePackSerializer <>).MakeGenericType(new Type[] { type }).GetMethod("PackTo"), new Expression[] { expression2 = Expression.Parameter(typeof(Packer), "packer"), Expression.Convert(expression3 = Expression.Parameter(typeof(object), "value"), type) }), new ParameterExpression[] { expression, expression2, expression3 }).Compile()(context, source, value); } }
private static void PackCore <T>(Packer source, T value, SerializationContext context) { if (value == null) { source.PackNull(); } else if ((typeof(T) != typeof(MessagePackObject)) && typeof(IPackable).IsAssignableFrom(typeof(T))) { (value as IPackable).PackToMessage(source, new PackingOptions()); } else { MessagePackSerializer.Create <T>(context).PackTo(source, value); } }
private static void PackObjectCore(Packer source, object value, SerializationContext context) { /* * MessagePackSerializer.Create<T>( context ).PackTo( source, value ); */ if (value == null) { source.PackNull(); return; } var type = value.GetType(); context.GetSerializer(type).PackTo(source, value); }
internal static void PackCollectionCore <T>(Packer source, IEnumerable <T> collection, MessagePackSerializer <T> itemSerializer) { // ReSharper disable once CompareNonConstrainedGenericWithNull if (collection == null) { source.PackNull(); return; } // ReSharper disable once SuspiciousTypeConversion.Global var asPackable = collection as IPackable; if (asPackable != null) { asPackable.PackToMessage(source, new PackingOptions()); return; } int count; ICollection <T> asCollectionT; ICollection asCollection; if ((asCollectionT = collection as ICollection <T>) != null) { count = asCollectionT.Count; } else if ((asCollection = collection as ICollection) != null) { count = asCollection.Count; } else { var asArray = collection.ToArray(); count = asArray.Length; collection = asArray; } source.PackArrayHeader(count); foreach (var item in collection) { itemSerializer.PackTo(source, item); } }
private static void PackCore <T>(Packer source, T value, SerializationContext context) { // ReSharper disable CompareNonConstrainedGenericWithNull if (value == null) // ReSharper restore CompareNonConstrainedGenericWithNull { source.PackNull(); return; } var asPackable = value as IPackable; if (asPackable != null) { asPackable.PackToMessage(source, new PackingOptions()); return; } context.GetSerializer <T>().PackTo(source, value); }
void Pack(Packer packer, object o) { if (o == null) { packer.PackNull(); return; } if (o is int) packer.Pack ((int)o); else if (o is uint) packer.Pack ((uint)o); else if (o is float) packer.Pack ((float)o); else if (o is double) packer.Pack ((double)o); else if (o is long) packer.Pack ((long)o); else if (o is ulong) packer.Pack ((ulong)o); else if (o is bool) packer.Pack ((bool)o); else if (o is byte) packer.Pack ((byte)o); else if (o is sbyte) packer.Pack ((sbyte)o); else if (o is short) packer.Pack ((short)o); else if (o is ushort) packer.Pack ((ushort)o); else if (o is string) packer.PackString((string)o, Encoding.ASCII); else if (o is Dictionary<string, object>) { packer.PackMapHeader((o as Dictionary<string, object>).Count); foreach (var pair in (o as Dictionary<string, object>)) { Pack(packer, pair.Key); Pack(packer, pair.Value); } } else if (o is string[]) { packer.PackArrayHeader((o as string[]).Length); foreach (var obj in (o as string[])) packer.Pack(obj as string); } else throw new Exception("Cant handle type: " + o.GetType().Name);; }