public object Deserialize(ICefValue value, Type targetType, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(value, targetType))
            {
                throw new InvalidOperationException();
            }

            object result = null;

            if (value.IsType(CefTypes.Time) && targetType == typeof(DateTime))
            {
                result = value.GetTime();
            }
            else if (targetType == typeof(decimal))
            {
                var cefValueType = value.GetValueType();
                switch (cefValueType)
                {
                case CefValueType.Int:
                    result = Convert.ToDecimal(value.GetInt());
                    break;

                case CefValueType.Double:
                    result = Convert.ToDecimal(value.GetDouble());
                    break;
                }
            }

            return(result);
        }
Esempio n. 2
0
        public object Deserialize(ICefValue value, Type targetType, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(value, targetType))
            {
                throw new InvalidOperationException();
            }

            object result = null;
            var    type   = value.GetValueType();

            switch (type)
            {
            case CefValueType.Bool:
                if (targetType == typeof(bool) || targetType == typeof(object))
                {
                    result = value.GetBool();
                }
                break;

            case CefValueType.Binary:
                if (value.IsType(CefTypes.Int64) && (targetType == typeof(object) || targetType == typeof(long)))
                {
                    result = value.GetInt64();
                }
                break;

            case CefValueType.Int:
                var intVal = value.GetInt();
                if (targetType == typeof(byte))
                {
                    result = Convert.ToByte(intVal);
                }
                else if (targetType == typeof(sbyte))
                {
                    result = Convert.ToSByte(intVal);
                }
                else if (targetType == typeof(short))
                {
                    result = Convert.ToInt16(intVal);
                }
                else if (targetType == typeof(ushort))
                {
                    result = Convert.ToUInt16(intVal);
                }
                else if (targetType == typeof(int) || targetType == typeof(object))
                {
                    result = intVal;
                }
                else if (targetType == typeof(uint))
                {
                    result = Convert.ToUInt32(intVal);
                }
                else if (targetType == typeof(long))
                {
                    result = Convert.ToInt64(intVal);
                }
                else if (targetType == typeof(ulong))
                {
                    result = Convert.ToUInt64(intVal);
                }
                else if (targetType == typeof(double))
                {
                    result = Convert.ToDouble(intVal);
                }
                else if (targetType == typeof(float))
                {
                    result = Convert.ToSingle(intVal);
                }
                break;

            case CefValueType.Double:
                var doubleVal = value.GetDouble();
                if (targetType == typeof(byte))
                {
                    result = Convert.ToByte(doubleVal);
                }
                else if (targetType == typeof(sbyte))
                {
                    result = Convert.ToSByte(doubleVal);
                }
                else if (targetType == typeof(short))
                {
                    result = Convert.ToInt16(doubleVal);
                }
                else if (targetType == typeof(ushort))
                {
                    result = Convert.ToUInt16(doubleVal);
                }
                else if (targetType == typeof(int))
                {
                    result = Convert.ToInt32(doubleVal);
                }
                else if (targetType == typeof(uint))
                {
                    result = Convert.ToUInt32(doubleVal);
                }
                else if (targetType == typeof(long))
                {
                    result = Convert.ToInt64(doubleVal);
                }
                else if (targetType == typeof(ulong))
                {
                    result = Convert.ToUInt64(doubleVal);
                }
                else if (targetType == typeof(double) || targetType == typeof(object))
                {
                    result = doubleVal;
                }
                else if (targetType == typeof(float))
                {
                    result = Convert.ToSingle(doubleVal);
                }
                break;

            case CefValueType.String:
                var strVal = value.GetString();
                if (targetType == typeof(char) && !string.IsNullOrEmpty(strVal))
                {
                    result = strVal.First();
                }
                break;
            }

            return(result);
        }
Esempio n. 3
0
        public object Deserialize(ICefValue value, Type targetType, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(value, targetType))
            {
                throw new InvalidOperationException();
            }

            var valueType = value.GetValueType();

            if (valueType == CefValueType.String)
            {
                return(CefV8Value.CreateString(value.GetString()));
            }

            if (valueType == CefValueType.Int)
            {
                return(CefV8Value.CreateInt(value.GetInt()));
            }

            if (valueType == CefValueType.Double)
            {
                return(CefV8Value.CreateDouble(value.GetDouble()));
            }

            if (value.IsType(CefTypes.Int64))
            {
                return(CefV8Value.CreateDouble(value.GetInt64()));
            }

            if (value.IsType(CefTypes.Time))
            {
                return(CefV8Value.CreateDate(value.GetTime()));
            }

            if (valueType == CefValueType.Bool)
            {
                return(CefV8Value.CreateBool(value.GetBool()));
            }

            if (valueType == CefValueType.List)
            {
                using (var list = value.GetList())
                {
                    if (list.Count > 0)
                    {
                        var array = CefV8Value.CreateArray(list.Count);
                        for (var i = 0; i < list.Count; i++)
                        {
                            using (var cefValue = list.GetValue(i))
                            {
                                array.SetValue(i,
                                               (ICefV8Value)objectSerializer.Deserialize(cefValue, typeof(ICefV8Value)));
                            }
                        }

                        return(array);
                    }
                }
            }

            if (valueType == CefValueType.Dictionary)
            {
                using (var dictionary = value.GetDictionary())
                    using (var valDict = dictionary.GetDictionary(ObjectSerializer.ValuePropertyName))
                    {
                        var typeId = dictionary.GetString(ObjectSerializer.TypeIdPropertyName);
                        if (typeId == ObjectSerializer.DictionaryTypeId)
                        {
                            var obj = CefV8Value.CreateObject();
                            foreach (var key in valDict.GetKeys())
                            {
                                obj.SetValue(key, (ICefV8Value)objectSerializer.Deserialize(valDict.GetValue(key), typeof(ICefV8Value)));
                            }
                            return(obj);
                        }
                        else
                        {
                            var deserialized = objectSerializer.Deserialize(value, typeof(object));
                            if (deserialized is ObjectDescriptor descriptor)
                            {
                                return(new ObjectBinder(descriptor, objectSerializer, functionCallPromiseRegistry).BindToNew());
                            }
                        }
                    }
            }

            return(CefV8Value.CreateNull());
        }