Exemplo n.º 1
0
        public MessagePackSerializer <TObject> BuildSerializerInstance(SerializationContext context, Type concreteType, PolymorphismSchema schema)
        {
#if DEBUG && !UNITY
            Contract.Assert(
                CollectionTraitsOfThis.DetailedCollectionType != CollectionDetailedKind.Array,
                typeof(TObject) + "(" + CollectionTraitsOfThis.DetailedCollectionType + ") != CollectionDetailedKind.Array"
                );
#endif // DEBUG
            Func <SerializationContext, MessagePackSerializer <TObject> > constructor;
            var codeGenerationContext = this.CreateCodeGenerationContextForSerializerCreation(context);
            if (typeof(TObject).GetIsEnum())
            {
                this.BuildEnumSerializer(codeGenerationContext);
                constructor = this.CreateEnumSerializerConstructor(codeGenerationContext);
            }
            else
            {
                this.BuildSerializer(codeGenerationContext, concreteType, schema);
                constructor = this.CreateSerializerConstructor(codeGenerationContext, schema);
            }

            if (constructor != null)
            {
                var serializer = constructor(context);
#if DEBUG && !UNITY
                Contract.Assert(serializer != null);
#endif // DEBUG && !UNITY
                return(serializer);
            }

            throw SerializationExceptions.NewTypeCannotSerialize(typeof(TObject));
        }
Exemplo n.º 2
0
        public MessagePackSerializer <TObject> BuildSerializerInstance(SerializationContext context)
        {
            var genericSerializer = GenericSerializer.Create <TObject>(context);

            if (genericSerializer != null)
            {
                return(genericSerializer);
            }

            var codeGenerationContext = this.CreateCodeGenerationContextForSerializerCreation(context);

            if (typeof(TObject).GetIsEnum())
            {
                this.BuildEnumSerializer(codeGenerationContext);
                Func <SerializationContext, MessagePackSerializer <TObject> > constructor =
                    this.CreateEnumSerializerConstructor(codeGenerationContext);

                if (constructor != null)
                {
                    var serializer = constructor(context);
#if DEBUG
                    Contract.Assert(serializer != null);
#endif
                    if (!context.Serializers.Register(serializer))
                    {
                        serializer = context.Serializers.Get <TObject>(context);
#if DEBUG
                        Contract.Assert(serializer != null);
#endif
                    }

                    return(serializer);
                }
            }
            else
            {
                this.BuildSerializer(codeGenerationContext);
                Func <SerializationContext, MessagePackSerializer <TObject> > constructor =
                    this.CreateSerializerConstructor(codeGenerationContext);

                if (constructor != null)
                {
                    var serializer = constructor(context);
#if DEBUG
                    Contract.Assert(serializer != null);
#endif
                    if (!context.Serializers.Register(serializer))
                    {
                        serializer = context.Serializers.Get <TObject>(context);
#if DEBUG
                        Contract.Assert(serializer != null);
#endif
                    }

                    return(serializer);
                }
            }

            throw SerializationExceptions.NewTypeCannotSerialize(typeof(TObject));
        }
        public MessagePackSerializer BuildSerializerInstance(SerializationContext context, Type concreteType, PolymorphismSchema schema)
        {
#if DEBUG
            Contract.Assert(
                this.CollectionTraits.DetailedCollectionType != CollectionDetailedKind.Array,
                this.TargetType + "(" + this.CollectionTraits.DetailedCollectionType + ") != CollectionDetailedKind.Array"
                );
#endif // DEBUG

            Func <SerializationContext, MessagePackSerializer> constructor;
            var codeGenerationContext = this.CreateCodeGenerationContextForSerializerCreation(context);
            if (this.TargetType.GetIsEnum())
            {
                this.BuildEnumSerializer(codeGenerationContext);
                constructor = this.CreateEnumSerializerConstructor(codeGenerationContext);
            }
            else
            {
                SerializationTarget targetInfo;
                this.BuildSerializer(codeGenerationContext, concreteType, schema, out targetInfo);
                constructor = this.CreateSerializerConstructor(codeGenerationContext, targetInfo, schema);
            }

            if (constructor != null)
            {
                var serializer = constructor(context);
#if DEBUG
                Contract.Assert(serializer != null);
#endif // DEBUG
                return(serializer);
            }

            throw SerializationExceptions.NewTypeCannotSerialize(this.TargetType);
        }
Exemplo n.º 4
0
    protected override void PackToCore(Packer packer, T objectTree)
    {
        IMessagePackSerializer serializer;
        string typeName = objectTree.GetType().Name;

        // Find matching serializer
        if (!_serializers.TryGetValue(typeName, out serializer))
        {
            throw SerializationExceptions.NewTypeCannotSerialize(typeof(T));
        }
        packer.PackArrayHeader(2);                 // Two-element array:
        packer.PackString(typeName);               //  0: Type name
        serializer.PackTo(packer, objectTree);     //  1: Packed object
    }
Exemplo n.º 5
0
    protected override void PackToCore(Packer packer, T objectTree)
    {
        IMessagePackSerializer serializer;
        string typeName = objectTree.GetType().Name;

        if (_serializers.TryGetValue(typeName, out serializer))
        {
            packer.PackArrayHeader(2);                 // We send an array of two elements
            packer.PackString(typeName);               // Type name
            serializer.PackTo(packer, objectTree);     // And packed object
        }
        else
        {
            throw SerializationExceptions.NewTypeCannotSerialize(typeof(T));
        }
    }