示例#1
0
        protected internal override DateTimeOffset UnpackFromCore(Unpacker unpacker)
        {
            if (unpacker.IsArrayHeader)
            {
                if (UnpackHelpers.GetItemsCount(unpacker) != 2)
                {
                    SerializationExceptions.ThrowInvalidArrayItemsCount(unpacker, typeof(DateTimeOffset), 2);
                }

                long ticks;
                if (!unpacker.ReadInt64(out ticks))
                {
                    SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                }

                short offsetMinutes;
                if (!unpacker.ReadInt16(out offsetMinutes))
                {
                    SerializationExceptions.ThrowUnexpectedEndOfStream(unpacker);
                }

                return(new DateTimeOffset(DateTime.FromBinary(ticks), TimeSpan.FromMinutes(offsetMinutes)));
            }
            else
            {
                return(MessagePackConvert.ToDateTimeOffset(unpacker.LastReadData.AsInt64()));
            }
        }
示例#2
0
        public static Int64 UnpackInt64Value(Unpacker unpacker, Type objectType, String memberName)
        {
            try
            {
                Int64 result;
                if (!unpacker.ReadInt64(out result))
                {
                    throw SerializationExceptions.NewFailedToDeserializeMember(objectType, memberName, null);
                }

                return(result);
            }
            catch (MessageTypeException ex)
            {
                throw SerializationExceptions.NewFailedToDeserializeMember(objectType, memberName, ex);
            }
        }
示例#3
0
        private static Message DeserializeMessage(Unpacker unpacker)
        {
            Message message = new Message();

            long fields;

            unpacker.ReadMapLength(out fields);
            string fieldName;

            for (int i = 0; i < fields; i++)
            {
                unpacker.ReadString(out fieldName);
                switch (fieldName)
                {
                case Message.NamePropertyName:
                {
                    string result;
                    unpacker.ReadString(out result);
                    message.Name = result;
                }
                break;

                case Message.TimestampPropertyName:
                {
                    long result;
                    unpacker.ReadInt64(out result);
                    message.Timestamp = result.FromUnixTimeInMilliseconds();
                }
                break;

                case Message.DataPropertyName:
                {
                    MessagePackObject result = unpacker.ReadItemData();
                    message.Data = ParseResult(result);
                }
                break;
                }
            }

            return(message);
        }
        public void UnpackFromMessage(Unpacker unpacker)
        {
            // Unpack fields are here:

            // temp variables
            long   id;
            string name;

            // It should be packed as array because we use hand-made packing implementation above.
            if (!unpacker.IsArrayHeader)
            {
                throw SerializationExceptions.NewIsNotArrayHeader();
            }

            // Check items count.
            if (UnpackHelpers.GetItemsCount(unpacker) != 2)
            {
                throw SerializationExceptions.NewUnexpectedArrayLength(2, UnpackHelpers.GetItemsCount(unpacker));
            }

            // Unpack fields here:
            if (!unpacker.ReadInt64(out id))
            {
                throw SerializationExceptions.NewMissingProperty("Id");
            }

            this.Id = id;

            if (!unpacker.ReadString(out name))
            {
                throw SerializationExceptions.NewMissingProperty("Name");
            }

            this.Name = name;

            // ...Instead, you can unpack from map as follows:
            //if ( !unpacker.IsMapHeader )
            //{
            //	throw SerializationExceptions.NewIsNotMapHeader();
            //}

            //// Check items count.
            //if ( UnpackHelpers.GetItemsCount( unpacker ) != 2 )
            //{
            //	throw SerializationExceptions.NewUnexpectedArrayLength( 2, UnpackHelpers.GetItemsCount( unpacker ) );
            //}

            //// Unpack fields here:
            //for ( int i = 0; i < 2 /* known count of fields */; i++ )
            //{
            //	// Unpack and verify key of entry in map.
            //	string key;
            //	if ( !unpacker.ReadString( out key ) )
            //	{
            //		// Missing key, incorrect.
            //		throw SerializationExceptions.NewUnexpectedEndOfStream();
            //	}

            //	switch ( key )
            //	{
            //		case "Id":
            //		{
            //			if ( !unpacker.ReadInt64( out id ) )
            //			{
            //				throw SerializationExceptions.NewMissingProperty( "Id" );
            //			}
            //
            //          this.Id = id;
            //			break;
            //		}
            //		case "Name":
            //		{
            //			if ( !unpacker.ReadString( out name ) )
            //			{
            //				throw SerializationExceptions.NewMissingProperty( "Name" );
            //			}
            //
            //          this.Name = name;
            //			break;
            //		}

            //		// Note: You should ignore unknown fields for forward compatibility.
            //	}
            //}
        }
		public static Int64 UnpackInt64Value( Unpacker unpacker, Type objectType, String memberName )
		{
			try
			{
				Int64 result;
				if ( !unpacker.ReadInt64( out result ) )
				{
					throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, null );
				}

				return result;
			}
			catch ( MessageTypeException ex )
			{
				throw SerializationExceptions.NewFailedToDeserializeMember( objectType, memberName, ex );
			}
		}
		public static Int64 UnpackInt64Value( Unpacker unpacker, Type objectType, String memberName )
		{
			if ( unpacker == null )
			{
				SerializationExceptions.ThrowArgumentNullException( "unpacker" );
			}

			if ( objectType == null )
			{
				SerializationExceptions.ThrowArgumentNullException( "objectType" );
			}

			if ( memberName == null )
			{
				SerializationExceptions.ThrowArgumentNullException( "memberName" );
			}

#if ASSERT
			Contract.Assert( unpacker != null );
			Contract.Assert( objectType != null );
			Contract.Assert( memberName != null );
#endif // ASSERT

			// ReSharper disable once RedundantAssignment
			var ctx = default( UnpackerTraceContext );
			InitializeUnpackerTrace( unpacker, ref ctx );

			try
			{
				Int64 result;
				if ( !unpacker.ReadInt64( out result ) )
				{
					SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, null );
				}

				Trace( ctx, "ReadDirect", unpacker, memberName );

				return result;
			}
			catch ( MessageTypeException ex )
			{
				SerializationExceptions.ThrowFailedToDeserializeMember( objectType, memberName, ex );
				return default( Int64 ); // never reaches.
			}
		}
示例#7
0
        private static PresenceMessage DeserializePresenceMessage(Unpacker unpacker)
        {
            PresenceMessage message = new PresenceMessage();

            long fields;

            unpacker.ReadMapLength(out fields);
            string fieldName;

            for (int i = 0; i < fields; i++)
            {
                unpacker.ReadString(out fieldName);
                switch (fieldName)
                {
                case PresenceMessage.ActionPropertyName:
                {
                    int result;
                    unpacker.ReadInt32(out result);
                    message.Action = (PresenceMessage.ActionType)result;
                }
                break;

                case PresenceMessage.IdPropertyName:
                {
                    string result;
                    unpacker.ReadString(out result);
                    message.Id = result;
                }
                break;

                case PresenceMessage.ClientIdPropertyName:
                {
                    string result;
                    unpacker.ReadString(out result);
                    message.ClientId = result;
                }
                break;

                case PresenceMessage.ConnectionIdPropertyName:
                {
                    string result;
                    unpacker.ReadString(out result);
                    message.ConnectionId = result;
                }
                break;

                case PresenceMessage.TimestampPropertyName:
                {
                    long result;
                    unpacker.ReadInt64(out result);
                    message.Timestamp = result.FromUnixTimeInMilliseconds();
                }
                break;

                case PresenceMessage.DataPropertyName:
                {
                    MessagePackObject result = unpacker.ReadItemData();
                    message.Data = ParseResult(result);
                }
                break;
                }
            }

            return(message);
        }