예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
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);
        }
예제 #4
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));
                }
        }
예제 #5
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);
        }
예제 #6
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());
        }