/// <summary>
        /// Defines the deserialization for the Datetime.
        /// </summary>
        /// <returns>The from core.</returns>
        /// <param name="unpacker">Unpacker.</param>
        protected override DateTime UnpackFromCore(Unpacker unpacker)
        {
            DateTime                      epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            MessagePackObject             obj   = unpacker.LastReadData;
            MessagePackExtendedTypeObject ext   = obj.AsMessagePackExtendedTypeObject();

            byte[] bytes = ext.GetBody();
            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }
            double val;

            if (bytes.Length == 1)
            {
                val = Convert.ToDouble(bytes[0]);
            }
            else if (bytes.Length == 2)
            {
                val = Convert.ToDouble(BitConverter.ToInt16(bytes, 0));
            }
            else if (bytes.Length == 4)
            {
                val = Convert.ToDouble(BitConverter.ToInt32(bytes, 0));
            }
            else
            {
                val = Convert.ToDouble(BitConverter.ToInt64(bytes, 0));
            }
            return(epoch.AddMilliseconds(val));
        }
Esempio n. 2
0
 /// <summary>
 ///		Invokes <see cref="MessagePackObject.AsMessagePackExtendedTypeObject()"/> in deserializaton manner.
 /// </summary>
 /// <param name="source"><see cref="MessagePackExtendedTypeObject"/>.</param>
 /// <returns>A deserialized value.</returns>
 /// <exception cref="SerializationException"><paramref name="source"/> is not expected type.</exception>
 public static MessagePackExtendedTypeObject DeserializeAsMessagePackExtendedTypeObject(this MessagePackObject source)
 {
     try
     {
         return(source.AsMessagePackExtendedTypeObject());
     }
     catch (InvalidOperationException ex)
     {
         throw new SerializationException(String.Format(CultureInfo.CurrentCulture, "The unpacked value is not expected type. {0}", ex.Message), ex);
     }
 }
            protected void ReadPrimitive()
            {
                MessagePackObject lastReadData = mUnpacker.LastReadData;

                if (lastReadData.IsNil)
                {
                    mReader.SetToken(JsonToken.Null, null);
                }
                else if (lastReadData.UnderlyingType == typeof(byte[]))
                {
                    mReader.SetToken(JsonToken.Bytes, lastReadData.AsBinary());
                }
                else if (lastReadData.UnderlyingType == typeof(bool))
                {
                    mReader.SetToken(JsonToken.Boolean, lastReadData.AsBoolean());
                }
                else if (lastReadData.UnderlyingType == typeof(string))
                {
                    mReader.SetToken(JsonToken.String, lastReadData.AsString());
                }
                else if (lastReadData.UnderlyingType == typeof(double) ||
                         lastReadData.UnderlyingType == typeof(float))
                {
                    mReader.SetToken(JsonToken.Float, lastReadData.ToObject());
                }
                else if (lastReadData.UnderlyingType == typeof(MessagePackExtendedTypeObject))
                {
                    MessagePackExtendedTypeObject extendedTypeObject =
                        lastReadData.AsMessagePackExtendedTypeObject();

                    if (extendedTypeObject.TypeCode == KnownExtTypeCode.Timestamp)
                    {
                        mReader.SetToken(JsonToken.Date, lastReadData.AsTimestamp().ToDateTime());
                    }
                }
                else if (lastReadData.IsTypeOf <sbyte>() == true ||
                         lastReadData.IsTypeOf <short>() == true ||
                         lastReadData.IsTypeOf <ushort>() == true ||
                         lastReadData.IsTypeOf <int>() == true ||
                         lastReadData.IsTypeOf <uint>() == true ||
                         lastReadData.IsTypeOf <long>() == true ||
                         lastReadData.IsTypeOf <ulong>() == true)
                {
                    mReader.SetToken(JsonToken.Integer, lastReadData.ToObject());
                }
            }
Esempio n. 4
0
        public static long AsInt64orExt(this MessagePackObject o)
        {
            if ((bool)o.IsTypeOf <long>())
            {
                return(o.AsInt64());
            }

            var bytes = o.AsMessagePackExtendedTypeObject().GetBody();

            if (bytes.Length < 8)
            {
                Array.Resize(ref bytes, 8);
            }

            if (!BitConverter.IsLittleEndian)
            {
                Array.Reverse(bytes);
            }

            return(BitConverter.ToInt64(bytes, 0));
        }