Example #1
0
            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.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());
                }
            }
Example #2
0
        private object GetObject(MessagePackObject str)
        {
            if (str.UnderlyingType == typeof(byte[]))
            {
                return(System.Text.Encoding.ASCII.GetString(str.AsBinary()));
            }
            else if (str.UnderlyingType == typeof(string))
            {
                return(str.AsString());
            }
            else if (str.UnderlyingType == typeof(byte))
            {
                return(str.AsByte());
            }
            else if (str.UnderlyingType == typeof(bool))
            {
                return(str.AsBoolean());
            }

            return(str);
        }
Example #3
0
        protected internal sealed override byte[] UnpackFromCore(Unpacker unpacker)
        {
            MessagePackObject obj2 = unpacker.Data.Value;

            return(obj2.IsNil ? null : obj2.AsBinary());
        }
Example #4
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}.");
        }