Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        public static void SetTime <TIndex>(this ICefValue @this, DateTime value, TIndex index = default(TIndex))
        {
            SetTime(_ =>
            {
                var valueType = @this.GetValueType();
                switch (valueType)
                {
                case CefValueType.List:
                    using (var listValue = @this.GetList())
                    {
                        if (typeof(TIndex) == typeof(int))
                        {
                            listValue.SetBinary((int)Convert.ChangeType(index, typeof(int)), _);
                        }
                    }
                    break;

                case CefValueType.Dictionary:
                    using (var dictValue = @this.GetDictionary())
                    {
                        if (typeof(TIndex) == typeof(string))
                        {
                            dictValue.SetBinary((string)Convert.ChangeType(index, typeof(string)), _);
                        }
                    }
                    break;

                default:
                    @this.SetBinary(_);
                    break;
                }
            }, value);
        }
Ejemplo n.º 3
0
        public bool CanHandle(ICefValue cefValue, Type targetType)
        {
            var cefValueType = cefValue.GetValueType();

            return((targetType?.IsPrimitive == true && SupportedTypes.Contains(cefValueType)) ||
                   (cefValueType == CefValueType.Int && targetType == typeof(object)) ||
                   (cefValueType == CefValueType.Double && targetType == typeof(object)) ||
                   (cefValueType == CefValueType.Bool && targetType == typeof(object)));
        }
Ejemplo n.º 4
0
        public object Deserialize(ICefValue value, Type targetType, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(value, targetType))
            {
                throw new InvalidOperationException();
            }

            return(value.Copy());
        }
Ejemplo n.º 5
0
        public object Deserialize(ICefValue value, Type targetType, int?index = null, string key = null)
        {
            var underlyingType = Nullable.GetUnderlyingType(targetType);

            if (underlyingType != null)
            {
                targetType = underlyingType;
            }

            var deserializer = Deserializers.FirstOrDefault(s => s.CanHandle(value, targetType));

            return(deserializer?.Deserialize(value, targetType, this));
        }
Ejemplo n.º 6
0
        private static bool IsDictionary(ICefValue value)
        {
            var result = value.GetValueType() == CefValueType.Dictionary;

            if (result)
            {
                using (var dict = value.GetDictionary())
                {
                    result = dict.HasKey(ObjectSerializer.TypeIdPropertyName) &&
                             dict.GetString(ObjectSerializer.TypeIdPropertyName) == ObjectSerializer.DictionaryTypeId;
                }
            }

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

            object result = null;

            using (var dictVal = value.GetDictionary())
            {
                var typeId = dictVal.GetString(ObjectSerializer.TypeIdPropertyName);
                using (var actualValue = dictVal.GetDictionary(ObjectSerializer.ValuePropertyName))
                {
                    KnownTypes.TryGetValue(typeId, out var type);
                    if (type != null && (targetType == typeof(object) || targetType.IsAssignableFrom(type)))
                    {
                        targetType = type;
                    }

                    try
                    {
                        result = Activator.CreateInstance(targetType);
                        var properties = targetType.GetProperties()
                                         .Select(p =>
                                                 new { Prop = p, DataMember = p.GetCustomAttribute <DataMemberAttribute>() })
                                         .ToDictionary(k => k.DataMember?.Name ?? k.Prop.Name, v => v.Prop);
                        var keys = actualValue.GetKeys();
                        foreach (var dictKey in keys)
                        {
                            if (properties.TryGetValue(dictKey, out var matchingProperty))
                            {
                                matchingProperty.SetValue(result,
                                                          objectSerializer.Deserialize(actualValue.GetValue(dictKey),
                                                                                       matchingProperty.PropertyType));
                            }
                        }
                    }
                    catch
                    {
                        //TODO:logging
                    }
                }
            }

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

            using (var dict = value.GetDictionary())
                using (var val = dict.GetDictionary(ObjectSerializer.ValuePropertyName))
                {
                    var id        = val.GetInt64(nameof(PropertyDescriptor.Id));
                    var name      = val.GetString(nameof(PropertyDescriptor.Name));
                    var propValue = val.GetValue(nameof(PropertyDescriptor.Value)).Copy();

                    return(new CefPropertyDescriptor(id, name, propValue));
                }
        }
Ejemplo n.º 9
0
        public object Deserialize(ICefValue source, Type targetType, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(source, targetType))
            {
                throw new InvalidOperationException();
            }

            using (var lstVal = source.GetList())
            {
                var elementType = targetType.GetElementType();
                var array       = Activator.CreateInstance(targetType, lstVal.Count) as Array;

                for (var i = 0; i < lstVal.Count; i++)
                {
                    array.SetValue(objectSerializer.Deserialize(lstVal.GetValue(i), elementType), i);
                }

                return(array);
            }
        }
Ejemplo n.º 10
0
 public static bool IsType(this ICefValue @this, CefTypes type)
 {
     return(IsType(() => @this, type));
 }
 public void SetValue(string key, ICefValue value)
 {
     Wrapped.SetValue(key, value.Unwrap <CefValue>());
 }
Ejemplo n.º 12
0
 public bool CanHandle(ICefValue cefValue, Type targetType)
 {
     return(cefValue.GetValueType() == CefValueType.List &&
            (targetType?.IsArray == true || targetType == typeof(object)));
 }
Ejemplo n.º 13
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());
        }
Ejemplo n.º 14
0
        public object Deserialize(ICefValue value, Type targetType, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(value, targetType))
            {
                throw new InvalidOperationException();
            }

            object result = null;

            using (var dictVal = value.GetDictionary())
                using (var actualVal = dictVal.GetDictionary(ObjectSerializer.ValuePropertyName))
                {
                    var typeId = dictVal.GetString(ObjectSerializer.TypeIdPropertyName);
                    if (typeId == ObjectSerializer.DictionaryTypeId)
                    {
                        var keyType   = typeof(string);
                        var valueType = typeof(object);

                        if (targetType == typeof(object))
                        {
                            targetType = typeof(Dictionary <string, object>);
                        }
                        else if (targetType.IsInterface)
                        {
                            if (targetType == typeof(IDictionary))
                            {
                                targetType = typeof(Dictionary <string, object>);
                            }
                            else
                            {
                                var generics = targetType.GetGenericArguments();
                                targetType = typeof(Dictionary <,>).MakeGenericType(generics);
                                keyType    = generics[0];
                                valueType  = generics[1];
                            }
                        }
                        else
                        {
                            var interfaces = targetType.GetInterfaces();
                            var generic    = interfaces.FirstOrDefault(i =>
                                                                       i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IDictionary <,>));
                            if (generic != null)
                            {
                                var genericArguments = generic.GetGenericArguments();
                                keyType   = genericArguments[0];
                                valueType = genericArguments[1];
                            }
                        }

                        try
                        {
                            result = Activator.CreateInstance(targetType);
                            var keys      = actualVal.GetKeys();
                            var addMethod = targetType.GetMethod("Add", new[] { keyType, valueType });
                            foreach (var key in keys)
                            {
                                object actualKey = key;
                                if (keyType == typeof(long))
                                {
                                    actualKey = Convert.ToInt64(key);
                                }

                                addMethod.Invoke(result,
                                                 new[]
                                                 { actualKey, objectSerializer.Deserialize(actualVal.GetValue(key), valueType) });
                            }
                        }
                        catch
                        {
                            //TODO: logging
                        }
                    }
                }

            return(result);
        }
Ejemplo n.º 15
0
 public CefPropertyDescriptor(long id, string name, ICefValue value)
 {
     Id        = id;
     ListValue = value;
     Name      = name;
 }
Ejemplo n.º 16
0
 public bool CanHandle(ICefValue cefValue, Type targetType)
 {
     return(IsDictionary(cefValue) &&
            (IsDictionaryType(targetType) || targetType == typeof(object)));
 }
Ejemplo n.º 17
0
 public static DateTime GetTime(this ICefValue @this)
 {
     return(GetTime(() => @this));
 }
Ejemplo n.º 18
0
 public static long GetInt64(this ICefValue @this)
 {
     return(GetInt64(() => @this));
 }
Ejemplo n.º 19
0
 public static void SetTime(this ICefValue @this, DateTime value)
 {
     SetTime(_ => @this.SetBinary(_), value);
 }
Ejemplo n.º 20
0
 public bool CanHandle(ICefValue cefValue, Type targetType)
 {
     return(cefValue.GetValueType() == CefValueType.String &&
            (targetType == typeof(string) || targetType == typeof(object)));
 }
Ejemplo n.º 21
0
 public bool CanHandle(ICefValue cefValue, Type targetType)
 {
     return((targetType == typeof(decimal) && (cefValue.GetValueType() == CefValueType.Double ||
                                               cefValue.GetValueType() == CefValueType.Int)) ||
            (targetType == typeof(DateTime) && cefValue.IsType(CefTypes.Time)));
 }
Ejemplo n.º 22
0
 public bool CanHandle(ICefValue cefValue, Type targetType)
 {
     return(cefValue.GetValueType() == CefValueType.Dictionary && targetType?.IsPrimitive == false &&
            !targetType.IsValueType &&
            !targetType.IsEnum);
 }
Ejemplo n.º 23
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);
        }
Ejemplo n.º 24
0
 public void SetValue(int index, ICefValue value)
 {
     Wrapped.SetValue(index, value.Unwrap <CefValue>());
 }
Ejemplo n.º 25
0
 public bool CanHandle(ICefValue cefValue, Type targetType)
 {
     return(targetType == typeof(ICefValue));
 }
Ejemplo n.º 26
0
 public static void SetInt64(this ICefValue @this, long value)
 {
     SetInt64(_ => @this.SetBinary(_), value);
 }
Ejemplo n.º 27
0
 public bool CanHandle(ICefValue cefValue, Type targetType)
 {
     return(cefValue.GetValueType() == CefValueType.Dictionary && targetType == typeof(PropertyDescriptor));
 }