Example #1
0
        private static object MapDateTime(PropertyInfo property, MessagePackObject source)
        {
            var conversionMethod = property?.GetCustomAttribute <MessagePackDateTimeMemberAttribute>()
                                   ?.DateTimeConversionMethod;

            switch (conversionMethod)
            {
            case DateTimeMemberConversionMethod.UnixEpoc:
                var epoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
                return(epoch.AddMilliseconds(source.AsInt64()));

            default: return(DateTime.FromBinary(source.AsInt64()));
            }
        }
Example #2
0
        private object ConvertFromMessagePackObject(MessagePackObject msgPackObject)
        {
            if (msgPackObject.IsTypeOf(typeof(long)) ?? false)
            {
                return(msgPackObject.AsInt64());
            }

            if (msgPackObject.IsTypeOf(typeof(double)) ?? false)
            {
                return(msgPackObject.AsDouble());
            }

            if (msgPackObject.IsArray)
            {
                return(msgPackObject.AsEnumerable().Select(ConvertFromMessagePackObject)
                       .ToArray());
            }

            if (msgPackObject.IsDictionary)
            {
                var msgPackDictionary = msgPackObject.AsDictionary();
                return(msgPackDictionary.ToDictionary(
                           keyValuePair => ConvertFromMessagePackObject(keyValuePair.Key),
                           keyValuePair => ConvertFromMessagePackObject(keyValuePair.Value)));
            }

            var obj = msgPackObject.ToObject();

            if (obj is MessagePackExtendedTypeObject msgpackExtObj)
            {
                return(GetExtensionType(msgpackExtObj));
            }

            return(obj);
        }
Example #3
0
 /// <summary>
 ///		Invokes <see cref="MessagePackObject.AsInt64()"/> in deserializaton manner.
 /// </summary>
 /// <param name="source"><see cref="MessagePackObject"/>.</param>
 /// <returns>A deserialized value.</returns>
 /// <exception cref="SerializationException"><paramref name="source"/> is not expected type.</exception>
 public static long DeserializeAsInt64(this MessagePackObject source)
 {
     try
     {
         return(source.AsInt64());
     }
     catch (InvalidOperationException ex)
     {
         throw new SerializationException(String.Format(CultureInfo.CurrentCulture, "The unpacked value is not expected type. {0}", ex.Message), ex);
     }
 }
Example #4
0
        private static JToken CreateToken(MessagePackObject curObj)
        {
            if (curObj.IsDictionary)
            {
                JObject resultObj = new JObject();

                Dictionary <string, MessagePackObject> curDict = curObj.AsDictionary("inputFile");

                foreach (KeyValuePair <string, MessagePackObject> curProp in curDict)
                {
                    resultObj[curProp.Key] = CreateToken(curProp.Value);
                }

                return(resultObj);
            }
            else if (curObj.IsArray)
            {
                JArray resultArray = new JArray();

                IList <MessagePackObject> curArray = curObj.AsList();

                foreach (MessagePackObject curElem in curArray)
                {
                    resultArray.Add(CreateToken(curElem));
                }

                return(resultArray);
            }
            else if (curObj.IsNil)
            {
                return(null);
            }
            else if (curObj.IsTypeOf <Int64>().HasValue&& curObj.IsTypeOf <Int64>().Value)
            {
                return(curObj.AsInt64());
            }
            else if (curObj.IsTypeOf <double>().HasValue&& curObj.IsTypeOf <double>().Value)
            {
                return(curObj.AsDouble());
            }
            else if (curObj.IsTypeOf <string>().HasValue&& curObj.IsTypeOf <string>().Value)
            {
                return(curObj.AsString());
            }
            else if (curObj.IsTypeOf <bool>().HasValue&& curObj.IsTypeOf <bool>().Value)
            {
                return(curObj.AsBoolean());
            }
            else
            {
                throw new Exception("Unknown Type!");
            }
        }
Example #5
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));
        }
Example #6
0
        public static object Map(Type targetType, MessagePackObject source, PropertyInfo property = null)
        {
            if (source.IsNil)
            {
                return(null);
            }
            if (targetType == typeof(string))
            {
                return(source.AsString());
            }
            if (targetType == typeof(int) || targetType == typeof(int?))
            {
                return(source.AsInt32());
            }
            if (targetType == typeof(uint) || targetType == typeof(uint?))
            {
                return(source.AsUInt32());
            }
            if (targetType == typeof(long) || targetType == typeof(long?))
            {
                return(source.AsInt64());
            }
            if (targetType == typeof(ulong) || targetType == typeof(ulong?))
            {
                return(source.AsUInt64());
            }
            if (targetType == typeof(float) || targetType == typeof(float?))
            {
                return(source.AsSingle());
            }
            if (targetType == typeof(double) || targetType == typeof(double?))
            {
                return(source.AsDouble());
            }
            if (targetType == typeof(bool) || targetType == typeof(bool?))
            {
                return(source.AsBoolean());
            }
            if (targetType == typeof(byte[]))
            {
                return(source.AsBinary());
            }
            if (targetType == typeof(byte) || targetType == typeof(byte?))
            {
                return(source.AsByte());
            }
            if (targetType == typeof(sbyte) || targetType == typeof(sbyte?))
            {
                return(source.AsSByte());
            }
            if (targetType == typeof(char[]))
            {
                return(source.AsCharArray());
            }
            if (targetType == typeof(short) || targetType == typeof(short?))
            {
                return(source.AsInt16());
            }
            if (targetType == typeof(ushort) || targetType == typeof(ushort?))
            {
                return(source.AsUInt16());
            }
            if (targetType == typeof(DateTime) || targetType == typeof(DateTime?))
            {
                return(MapDateTime(property, source));
            }
            if (targetType == typeof(IList <MessagePackObject>))
            {
                return(source.AsList());
            }
            if (targetType == typeof(IEnumerable <MessagePackObject>))
            {
                return(source.AsEnumerable());
            }

            var ti = targetType.GetTypeInfo();

            if (targetType == typeof(MessagePackObject))
            {
                return(source);
            }

            if (ti.IsGenericType && (targetType.GetGenericTypeDefinition() == typeof(List <>) ||
                                     targetType.GetGenericTypeDefinition() == typeof(IList <>)))
            {
                return(MapList(targetType, source.AsList()));
            }

            if (ti.IsClass && source.IsList)
            {
                return(MapClass(targetType, source));
            }

            if (ti.IsClass && source.IsMap)
            {
                return(MapDictionary(targetType, source.AsDictionary()));
            }

            if (ti.IsEnum)
            {
                return(MapEnum(targetType, source));
            }

            throw new MessagePackMapperException(
                      $"Cannot find MsgPackObject converter for type {targetType.FullName}.");
        }