Defines non-generic interface of serializers and provides entry points for MessagePackSerializer{T} usage.
You cannot derived from this class directly, use MessagePackSerializer{T} instead. This class is intended to guarantee backward compatibilities of non generic API.
Inheritance: IMessagePackSingleObjectSerializer
コード例 #1
0
        public EntityPropertiesTests()
        {
            var context = new SerializationContext();
            context.SerializationMethod = SerializationMethod.Map;

            _serializer = context.GetSerializer<BasicEntity>();
        }
コード例 #2
0
 public BatchedWebServiceTarget(IBatchWebService WebService = null)
 {
     this.logEventSerializer = MessagePackSerializer.Get<Dictionary<String,String>>();
     this.logBatchSerializer = BatchedWebService.PayLoadSerializer.getSerializer();
     if (WebService == null) {
         this.webService = new BatchWebService();
     }else {
         this.webService = WebService;
     }
     this.Layout = new DictionaryLayout();
     this.Layout.Attributes.Add(new DictionaryLayoutAttribute("time", "${longdate}"));
     this.Layout.Attributes.Add(new DictionaryLayoutAttribute("level", "${level:upperCase=True}"));
     this.Layout.Attributes.Add(new DictionaryLayoutAttribute("message", "${message}"));
     this.Layout.Attributes.Add(new DictionaryLayoutAttribute("exception", "${exception:format=ToString,Message,StackTrace}"));
 }
コード例 #3
0
 public TypedMessagePackSerializerWrapper(SerializationContext context, MessagePackSerializer underlying)
     : base(context)
 {
     this._underlyingSerializer = underlying;
     this._underlyingFactory    = underlying as ICollectionInstanceFactory;
 }
コード例 #4
0
 public static T InvokeUnpackFrom <T>(MessagePackSerializer <T> serializer, Unpacker unpacker)
 {
     return(serializer.UnpackFromCore(unpacker));
 }
コード例 #5
0
        public static void UnpackMapTo <TKey, TValue>(Unpacker unpacker, MessagePackSerializer <TKey> keySerializer, MessagePackSerializer <TValue> valueSerializer, IDictionary <TKey, TValue> dictionary)
        {
#if DEBUG
            if (unpacker == null)
            {
                throw new ArgumentNullException("unpacker");
            }

            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            if (!unpacker.IsMapHeader)
            {
                throw SerializationExceptions.NewIsNotMapHeader();
            }

            Contract.EndContractBlock();
#endif

            int count = GetItemsCount(unpacker);
            for (int i = 0; i < count; i++)
            {
                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewMissingItem(i);
                }

                TKey key;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    key = keySerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        key = keySerializer.UnpackFrom(subtreeUnpacker);
                    }
                }


                if (!unpacker.Read())
                {
                    throw SerializationExceptions.NewMissingItem(i);
                }

                TValue value;
                if (!unpacker.IsArrayHeader && !unpacker.IsMapHeader)
                {
                    value = valueSerializer.UnpackFrom(unpacker);
                }
                else
                {
                    using (Unpacker subtreeUnpacker = unpacker.ReadSubtree())
                    {
                        value = valueSerializer.UnpackFrom(subtreeUnpacker);
                    }
                }

                dictionary.Add(key, value);
            }
        }
コード例 #6
0
 private MessagePackSerializer <T> CreateTarget <T>(SerializationContext context)
 {
     return(MessagePackSerializer.CreateInternal <T>(context, PolymorphismSchema.Default));
 }
コード例 #7
0
 internal static MessagePackSerializer <T> Wrap <T>(SerializationContext context, MessagePackSerializer nonGeneric)
 {
     return
         (nonGeneric == null
                         ? null
                         : (nonGeneric as TypedMessagePackSerializerWrapper <T>)
          ?? (
              nonGeneric is ICustomizableEnumSerializer
                                 ? new EnumTypedMessagePackSerializerWrapper <T>(context, nonGeneric)
                                 : new TypedMessagePackSerializerWrapper <T>(context, nonGeneric)
              ));
 }
コード例 #8
0
        /// <summary>
        ///		Creates new <see cref="MessagePackSerializer{T}"/> instance with <see cref="SerializationContext.Default"/>.
        /// </summary>
        /// <typeparam name="T">Target type.</typeparam>
        /// <returns>
        ///		New <see cref="MessagePackSerializer{T}"/> instance to serialize/deserialize the object tree which the top is <typeparamref name="T"/>.
        /// </returns>
        public static MessagePackSerializer <T> Create <T>()
        {
            Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null);

            return(MessagePackSerializer.Create <T>(SerializationContext.Default));
        }
コード例 #9
0
 /// <summary>
 ///		Creates new <see cref="IMessagePackSerializer"/> instance with <see cref="SerializationContext.Default"/>.
 /// </summary>
 /// <param name="targetType">Target type.</param>
 /// <returns>
 ///		New <see cref="IMessagePackSingleObjectSerializer"/> instance to serialize/deserialize the object tree which the top is <paramref name="targetType"/>.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 ///		<paramref name="targetType"/> is <c>null</c>.
 /// </exception>
 /// <remarks>
 ///		To avoid boxing and strongly typed API is prefered, use <see cref="Create{T}()"/> instead when possible.
 /// </remarks>
 public static IMessagePackSingleObjectSerializer Create(Type targetType)
 {
     return(MessagePackSerializer.Create(targetType, SerializationContext.Default));
 }
コード例 #10
0
		/// <summary>
		///		Gets the <see cref="MessagePackSerializer{T}"/> with this instance.
		/// </summary>
		/// <typeparam name="T">Type of serialization/deserialization target.</typeparam>
		/// <param name="providerParameter">A provider specific parameter. See remarks section for details.</param>
		/// <returns>
		///		<see cref="MessagePackSerializer{T}"/>.
		///		If there is exiting one, returns it.
		///		Else the new instance will be created.
		/// </returns>
		/// <remarks>
		///		<para>
		///			This method automatically register new instance via <see cref="SerializerRepository.Register{T}(MessagePackSerializer{T})"/>.
		///		</para>
		///		<para>
		///			Currently, only following provider parameters are supported.
		///			<list type="table">
		///				<listheader>
		///					<term>Target type</term>
		///					<description>Provider parameter</description>
		///				</listheader>
		///				<item>
		///					<term><see cref="EnumMessagePackSerializer{TEnum}"/> or its descendants.</term>
		///					<description><see cref="EnumSerializationMethod"/>. The returning instance corresponds to this value for serialization.</description>
		///				</item>
		///			</list>
		///			<note><c>null</c> is valid value for <paramref name="providerParameter"/> and it indeicates default behavior of parameter.</note>
		///		</para>
		/// </remarks>
		public MessagePackSerializer<T> GetSerializer<T>( object providerParameter )
		{
#if DEBUG
			Contract.Ensures( Contract.Result<MessagePackSerializer<T>>() != null );
#endif // DEBUG

			// Explicitly generated serializer should always used, so get it first.
			MessagePackSerializer<T> serializer = this._serializers.Get<T>( this, providerParameter );

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

			bool lockTaken = false;
			lock ( this._generationLock )
			{
				// Re-get to check because other thread might create the serializer when I wait the lock.
				serializer = this._serializers.Get<T>( this, providerParameter );

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

				try
				{
					try { }
					finally
					{
#if !FEATURE_CONCURRENT
						lock ( this._typeLock )
						{
							var typeLock = new object();
							object aquiredLock;
							lockTaken = !this._typeLock.TryGetValue( typeof( T ), out aquiredLock );
							if ( lockTaken )
							{
								this._typeLock.Add( typeof( T ), typeLock );
							}
						}
#else
						var typeLock = new object();
						var aquiredTypeLock = this._typeLock.GetOrAdd( typeof( T ), _ => typeLock );
						lockTaken = typeLock == aquiredTypeLock;
#endif // !FEATURE_CONCURRENT
					}

					if ( lockTaken )
					{
						// First try to create generic serializer w/o code generation.
						var schema = ( providerParameter ?? PolymorphismSchema.Create( typeof( T ), null ) ) as PolymorphismSchema;
						serializer = GenericSerializer.Create<T>( this, schema );

						if ( serializer == null )
						{
#if !UNITY
							if ( !this._serializerGeneratorOptions.CanRuntimeCodeGeneration )
							{
#endif // !UNITY
								// On debugging, or AOT only envs, use reflection based aproach.
								serializer =
									this.GetSerializerWithoutGeneration<T>( schema )
									?? this.OnResolveSerializer<T>( schema )
									?? MessagePackSerializer.CreateReflectionInternal<T>( this, this.EnsureConcreteTypeRegistered( typeof( T ) ), schema );
#if !UNITY
							}
							else
							{
								// This thread creating new type serializer.
								serializer = this.OnResolveSerializer<T>( schema ) ?? MessagePackSerializer.CreateInternal<T>( this, schema );
							}
#endif // !UNITY
						}
					}
					else
					{
						// This thread owns existing lock -- thus, constructing self-composite type.
						return new LazyDelegatingMessagePackSerializer<T>( this, providerParameter );
					}


					// Some types always have to use provider. 
					MessagePackSerializerProvider provider;
					var asEnumSerializer = serializer as ICustomizableEnumSerializer;
					if ( asEnumSerializer != null )
					{
#if DEBUG
						Contract.Assert( typeof( T ).GetIsEnum(), typeof( T ) + " is not enum but generated serializer is ICustomizableEnumSerializer" );
#endif // DEBUG

						provider = new EnumMessagePackSerializerProvider( typeof( T ), asEnumSerializer );
					}
					else
					{
#if DEBUG
						Contract.Assert( !typeof( T ).GetIsEnum(), typeof( T ) + " is enum but generated serializer is not ICustomizableEnumSerializer : " + ( serializer == null ? "null" : serializer.GetType().FullName ) );
#endif // DEBUG

						// Creates provider even if no schema -- the schema might be specified future for the type.
						// It is OK to use polymorphic provider for value type.
#if !UNITY
						provider = new PolymorphicSerializerProvider<T>( serializer );
#else
						provider = new PolymorphicSerializerProvider<T>( this, serializer );
#endif // !UNITY
					}

#if !UNITY
					Type nullableType;
					MessagePackSerializerProvider nullableSerializerProvider;
					SerializerRepository.GetNullableCompanion(
						typeof( T ),
						this,
						serializer,
						out nullableType,
						out nullableSerializerProvider
					);

					this._serializers.Register(
						typeof( T ),
						provider,
						nullableType,
						nullableSerializerProvider,
						SerializerRegistrationOptions.WithNullable
					);
#else
					this._serializers.Register(
						typeof( T ),
						provider,
						null,
						null,
						SerializerRegistrationOptions.None
					);
#endif // !UNITY

					// Re-get to avoid duplicated registration and handle provider parameter or get the one created by prececing thread.
					// If T is null and schema is not provided or default schema is provided, then exception will be thrown here from the new provider.
					return this._serializers.Get<T>( this, providerParameter );
				}
				finally
				{
					if ( lockTaken )
					{
#if !FEATURE_CONCURRENT
					lock ( this._typeLock )
					{
						this._typeLock.Remove( typeof( T ) );
					}
#else
						object dummy;
						this._typeLock.TryRemove( typeof( T ), out dummy );
#endif // !FEATURE_CONCURRENT
					}
				}
			}
		}
コード例 #11
0
 public TestValueTypeWrapperSerializer(SerializationContext context)
     : base(context)
 {
     this._serializer0 = context.GetSerializer <TestValueType>(PolymorphismSchema.Default);
 }
コード例 #12
0
 private MessagePackSerializer <T> CreateTarget <T>(SerializationContext context)
 {
     return(MessagePackSerializer.CreateInternal <T>(context));
 }
コード例 #13
0
 public void TestCreation_ReferenceType_AllOk()
 {
     Assert.NotNull(MessagePackSerializer.Create <NilImplicationTestTargetForReferenceType>(this.CreateSerializationContext()));
 }