Beispiel #1
0
        public ICefValue Serialize(object source, Stack <object> seen, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(source?.GetType()))
            {
                throw new InvalidOperationException();
            }

            using (var value = CefDictionaryValue.Create())
                using (var dict = CefDictionaryValue.Create())
                {
                    value.SetString(ObjectSerializer.TypeIdPropertyName, ObjectSerializer.DictionaryTypeId);

                    foreach (DictionaryEntry dictionaryEntry in (IDictionary)source)
                    {
                        var cefValue = objectSerializer.Serialize(dictionaryEntry.Value, seen);
                        dict.SetValue(dictionaryEntry.Key.ToString(), cefValue);
                    }
                    value.SetDictionary(ObjectSerializer.ValuePropertyName, dict);

                    var result = CefValue.Create();
                    result.SetDictionary(value);

                    return(result);
                }
        }
Beispiel #2
0
        public CefValue Serialize(object source, HashSet <object> seen, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(source?.GetType()))
            {
                throw new InvalidOperationException();
            }

            var actualSource = (PropertyDescriptor)source;
            var result       = CefValue.Create();

            using (var resultDict = CefDictionaryValue.Create())
                using (var value = CefDictionaryValue.Create())
                {
                    resultDict.SetString(ObjectSerializer.TypeIdPropertyName, PropertyDescriptor.TypeId);

                    value.SetInt64(nameof(actualSource.Id), actualSource.Id);
                    value.SetString(nameof(PropertyDescriptor.Name), actualSource.Name);
                    value.SetValue(nameof(PropertyDescriptor.Value),
                                   objectSerializer.Serialize(actualSource.Value, new HashSet <object>()));

                    resultDict.SetDictionary(ObjectSerializer.ValuePropertyName, value);
                    result.SetDictionary(resultDict);
                }

            return(result);
        }
Beispiel #3
0
        public CefDictionaryValue AsCefDictionaryValue()
        {
            var result = CefDictionaryValue.Create();

            if (this.Value is IDictionary)
            {
                var v = (IDictionary)this.Value;

                Debug.Assert(v.Keys.Count == v.Values.Count);

                var vKeys   = new String[v.Keys.Count];
                var vValues = new Object[v.Values.Count];

                v.Keys.CopyTo(vKeys, 0);
                v.Values.CopyTo(vValues, 0);

                for (var i = 0; i < vKeys.Length; i++)
                {
                    if (vValues[i] is Byte[])
                    {
                        result.SetBinary(vKeys[i], new JSValue(vValues[i]).AsCefBinaryValue());
                    }
                    else if (vValues[i] is Boolean)
                    {
                        result.SetBool(vKeys[i], (Boolean)vValues[i]);
                    }
                    else if (vValues[i] is IDictionary)
                    {
                        result.SetDictionary(vKeys[i], new JSValue(vValues[i]).AsCefDictionaryValue());
                    }
                    else if (vValues[i] is Single || vValues[i] is Double || vValues[i] is Decimal || vValues[i] is UInt32 || vValues[i] is UInt64 || vValues[i] is Int64)
                    {
                        result.SetDouble(vKeys[i], (Double)vValues[i]);
                    }
                    else if (vValues[i] is Byte || vValues[i] is SByte || vValues[i] is UInt16 || vValues[i] is Int16 || vValues[i] is Int32)
                    {
                        result.SetInt(vKeys[i], (Int32)vValues[i]);
                    }
                    else if (vValues[i] is IList)
                    {
                        result.SetList(vKeys[i], new JSValue(vValues[i]).AsCefListValue());
                    }
                    else if (vValues[i] is String)
                    {
                        result.SetString(vKeys[i], (String)vValues[i]);
                    }
                    else
                    {
                        result.SetNull(vKeys[i]);
                    }
                }
            }

            return(result);
        }
Beispiel #4
0
        public ICefValue Serialize(object obj, Stack <object> seen, ObjectSerializer objectSerializer)
        {
            var type = obj?.GetType();

            if (!CanHandle(type))
            {
                throw new InvalidOperationException();
            }

            if (type == typeof(string))
            {
                var result = CefValue.Create();
                result.SetString((string)obj);

                return(result);
            }

            using (var value = CefDictionaryValue.Create())
                using (var dict = CefDictionaryValue.Create())
                {
                    value.SetString(ObjectSerializer.TypeIdPropertyName, GetTypeId(type));
                    var properties   = type.GetProperties();
                    var shouldFilter = type.GetCustomAttribute <DataContractAttribute>() != null;

                    foreach (var propertyDesc in properties
                             .Where(p => p.GetIndexParameters().Length <= 0)
                             .Select(p => new { Property = p, DataMember = p.GetCustomAttribute <DataMemberAttribute>() })
                             .Where(p => p.DataMember != null || !shouldFilter))
                    {
                        var propertyValue = propertyDesc.Property.GetValue(obj);
                        var name          = propertyDesc.DataMember?.Name ?? propertyDesc.Property.Name;

                        var cefValue = objectSerializer.Serialize(propertyValue, seen);
                        dict.SetValue(name, cefValue);
                    }

                    value.SetDictionary(ObjectSerializer.ValuePropertyName, dict);

                    var result = CefValue.Create();
                    result.SetDictionary(value);

                    return(result);
                }
        }
Beispiel #5
0
            static CefDictionaryValue ToDictionaryValue(CefV8Value value, CompositeDisposable disposable)
            {
                var result = CefDictionaryValue.Create();

                foreach (var key in value.GetKeys())
                {
                    var val = value.GetValue(key);
                    if (val.IsBool)
                    {
                        result.SetBool(key, val.GetBoolValue());
                    }
                    else if (val.IsInt)
                    {
                        result.SetInt(key, val.GetIntValue());
                    }
                    else if (val.IsDouble)
                    {
                        result.SetDouble(key, val.GetDoubleValue());
                    }
                    else if (val.IsString)
                    {
                        result.SetString(key, val.GetStringValue());
                    }
                    else if (val.IsNull)
                    {
                        result.SetNull(key);
                    }
                    else if (val.IsArray)
                    {
                        result.SetList(key, ToListValue(val, disposable));
                    }
                    else if (val.IsObject)
                    {
                        result.SetDictionary(key, ToDictionaryValue(val, disposable));
                    }
                }
                disposable.Add(result);
                return(result);
            }
Beispiel #6
0
        public CefValue Serialize(object source, HashSet <object> seen, ObjectSerializer objectSerializer)
        {
            if (!CanHandle(source?.GetType()))
            {
                throw new InvalidOperationException();
            }

            long frameId = 0;

            using (var context = CefV8Context.GetCurrentContext())
            {
                frameId = context.GetFrame().Identifier;
            }
            var result = CefValue.Create();

            result.SetNull();

            var value = (CefV8Value)source;

            if (value.IsString)
            {
                result.SetString(value.GetStringValue());
            }
            else if (value.IsBool)
            {
                result.SetBool(value.GetBoolValue());
            }
            else if (value.IsDouble)
            {
                result.SetDouble(value.GetDoubleValue());
            }
            else if (value.IsInt)
            {
                result.SetInt(value.GetIntValue());
            }
            else if (value.IsUInt)
            {
                result.SetDouble(value.GetUIntValue());
            }
            else if (value.IsDate)
            {
                result.SetTime(value.GetDateValue());
            }
            else if (value.IsArray)
            {
                using (var list = CefListValue.Create())
                {
                    list.SetSize(value.GetArrayLength());
                    for (var i = 0; i < value.GetArrayLength(); i++)
                    {
                        list.SetValue(i, objectSerializer.Serialize(value.GetValue(i), seen));
                    }
                    result.SetList(list);
                }
            }
            else if (value.IsFunction)
            {
                var callback = new Callback(value, promiseService, objectSerializer);
                var id       = callbackRegistry.Save(frameId, callback);

                using (var list = CefDictionaryValue.Create())
                    using (var actualValue = CefDictionaryValue.Create())
                    {
                        list.SetString(ObjectSerializer.TypeIdPropertyName, CallbackDescriptor.TypeId);

                        actualValue.SetInt64(nameof(CallbackDescriptor.FunctionId), id);

                        list.SetDictionary(ObjectSerializer.ValuePropertyName, actualValue);
                        result.SetDictionary(list);
                    }
            }
            else if (value.IsObject)
            {
                using (var dict = CefDictionaryValue.Create())
                    using (var actualValue = CefDictionaryValue.Create())
                    {
                        dict.SetString(ObjectSerializer.TypeIdPropertyName, ObjectSerializer.DictionaryTypeId);
                        if (value.TryGetKeys(out var keys))
                        {
                            foreach (var key in keys)
                            {
                                actualValue.SetValue(key, objectSerializer.Serialize(value.GetValue(key), seen));
                            }
                        }

                        dict.SetDictionary(ObjectSerializer.ValuePropertyName, actualValue);
                        result.SetDictionary(dict);
                    }
            }


            return(result);
        }
 private static ICefDictionaryValue CreateDictionary()
 {
     return(new CefDictionaryValueImpl(CefDictionaryValue.Create()));
 }