Example #1
0
 protected internal override T UnpackFromCore(Unpacker unpacker)
 {
     return
         (TypeInfoEncoder.Decode(
              unpacker,
              // ReSharper disable once ConvertClosureToMethodGroup
              u => TypeInfoEncoder.DecodeRuntimeTypeInfo(u),                      // Lamda capture is more efficient.
              (t, u) => ( T )this.GetActualTypeSerializer(t).UnpackFrom(u)
              ));
 }
 protected internal override T UnpackFromCore(Unpacker unpacker)
 {
     return
         (TypeInfoEncoder.Decode(
              unpacker,
              // Currently, lamda is more efficient than method group.
              // ReSharper disable once ConvertClosureToMethodGroup
              c => this.GetTypeFromCode(c),
              (t, u) => ( T )this.GetActualTypeSerializer(t).UnpackFrom(u)
              ));
 }
        protected internal override void PackToCore(Packer packer, T objectTree)
        {
            packer.PackArrayHeader(3);
            packer.PackExtendedTypeValue(
                this.OwnerContext.TypeEmbeddingSettings.TypeEmbeddingIdentifier,
                TypeInfoEncodingBytes.RawCompressed
                );
            TypeInfoEncoder.Encode(packer, objectTree.GetType());

            this.GetActualTypeSerializer(objectTree.GetType()).PackTo(packer, objectTree);
        }
Example #4
0
 protected internal override Task <T> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
 {
     return
         (TypeInfoEncoder.DecodeAsync <T>(
              unpacker,
              // ReSharper disable once ConvertClosureToMethodGroup
              (u, c) => TypeInfoEncoder.DecodeRuntimeTypeInfoAsync(u, c),                        // Lamda capture is more efficient.
              (t, u, c) => this.GetActualTypeSerializer(t).UnpackFromAsync(u, c),
              cancellationToken
              ));
 }
 protected internal override Task <T> UnpackFromAsyncCore(Unpacker unpacker, CancellationToken cancellationToken)
 {
     return
         (TypeInfoEncoder.DecodeAsync <T>(
              unpacker,
              // Currently, lamda is more efficient than method group.
              // ReSharper disable once ConvertClosureToMethodGroup
              c => this.GetTypeFromCode(c),
              (t, u, c) => this.GetActualTypeSerializer(t).UnpackFromAsync(u, c),
              cancellationToken
              ));
 }
        protected internal override T UnpackFromCore(Unpacker unpacker)
        {
            // It is not reasonable to identify other forms.
            if (!unpacker.IsArrayHeader || UnpackHelpers.GetItemsCount(unpacker) != 3)
            {
                throw SerializationExceptions.NewUnknownTypeEmbedding();
            }

            using (var subTreeUnpacker = unpacker.ReadSubtree())
            {
                if (!subTreeUnpacker.Read())
                {
                    throw SerializationExceptions.NewUnexpectedEndOfStream();
                }
                var header = subTreeUnpacker.LastReadData.AsMessagePackExtendedTypeObject();
                if (header.TypeCode != this.OwnerContext.TypeEmbeddingSettings.TypeEmbeddingIdentifier)
                {
                    throw new SerializationException(
                              String.Format(CultureInfo.CurrentCulture, "Unknown extension type {0}.", header.TypeCode)
                              );
                }

                if (header.Body.Length != TypeInfoEncodingBytes.RawCompressed.Length ||
                    header.Body[0] != TypeInfoEncodingBytes.RawCompressed[0])
                {
                    throw new SerializationException("Unknown type info encoding type.");
                }

                if (!subTreeUnpacker.Read())
                {
                    throw SerializationExceptions.NewUnexpectedEndOfStream();
                }

                var objectType = TypeInfoEncoder.Decode(subTreeUnpacker);

                if (!subTreeUnpacker.Read())
                {
                    throw SerializationExceptions.NewUnexpectedEndOfStream();
                }

                return(( T )this.GetActualTypeSerializer(objectType).UnpackFrom(subTreeUnpacker));
            }
        }
Example #7
0
        protected internal override void PackToCore(Packer packer, T objectTree)
        {
            string typeCode;

            if (!this._typeCodeMap.TryGetValue(objectTree.GetType().TypeHandle, out typeCode))
            {
                throw new SerializationException(
                          String.Format(
                              CultureInfo.CurrentCulture,
                              "Type '{0}' in assembly '{1}' is not defined as known types.",
                              objectTree.GetType().GetFullName(),
                              objectTree.GetType().GetAssembly()
                              )
                          );
            }

            TypeInfoEncoder.Encode(packer, this._typeCodeMap[objectTree.GetType().TypeHandle]);
            this.GetActualTypeSerializer(objectTree.GetType()).PackTo(packer, objectTree);
        }
Example #8
0
        protected internal override T UnpackFromCore(Unpacker unpacker)
        {
            return
                (TypeInfoEncoder.Decode(
                     unpacker,
                     u =>
            {
                var typeCode = u.LastReadData.AsString();

                RuntimeTypeHandle typeHandle;
                if (!this._typeHandleMap.TryGetValue(typeCode, out typeHandle))
                {
                    throw new SerializationException(
                        String.Format(CultureInfo.CurrentCulture, "Unknown type {0}.", StringEscape.ForDisplay(typeCode))
                        );
                }

                return Type.GetTypeFromHandle(typeHandle);
            },
                     (t, u) => ( T )this.GetActualTypeSerializer(t).UnpackFrom(u)
                     ));
        }
        protected internal override async Task PackToAsyncCore(Packer packer, T objectTree, CancellationToken cancellationToken)
        {
            await TypeInfoEncoder.EncodeAsync(packer, this.GetTypeCode( objectTree ), cancellationToken).ConfigureAwait(false);

            await this.GetActualTypeSerializer(objectTree.GetType()).PackToAsync(packer, objectTree, cancellationToken).ConfigureAwait(false);
        }
 protected internal override void PackToCore(Packer packer, T objectTree)
 {
     TypeInfoEncoder.Encode(packer, this.GetTypeCode(objectTree));
     this.GetActualTypeSerializer(objectTree.GetType()).PackTo(packer, objectTree);
 }