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); }
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); }
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))); }
public object Deserialize(ICefValue value, Type targetType, ObjectSerializer objectSerializer) { if (!CanHandle(value, targetType)) { throw new InvalidOperationException(); } return(value.Copy()); }
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)); }
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); }
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); }
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)); } }
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); } }
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>()); }
public bool CanHandle(ICefValue cefValue, Type targetType) { return(cefValue.GetValueType() == CefValueType.List && (targetType?.IsArray == true || targetType == typeof(object))); }
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()); }
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); }
public CefPropertyDescriptor(long id, string name, ICefValue value) { Id = id; ListValue = value; Name = name; }
public bool CanHandle(ICefValue cefValue, Type targetType) { return(IsDictionary(cefValue) && (IsDictionaryType(targetType) || targetType == typeof(object))); }
public static DateTime GetTime(this ICefValue @this) { return(GetTime(() => @this)); }
public static long GetInt64(this ICefValue @this) { return(GetInt64(() => @this)); }
public static void SetTime(this ICefValue @this, DateTime value) { SetTime(_ => @this.SetBinary(_), value); }
public bool CanHandle(ICefValue cefValue, Type targetType) { return(cefValue.GetValueType() == CefValueType.String && (targetType == typeof(string) || targetType == typeof(object))); }
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))); }
public bool CanHandle(ICefValue cefValue, Type targetType) { return(cefValue.GetValueType() == CefValueType.Dictionary && targetType?.IsPrimitive == false && !targetType.IsValueType && !targetType.IsEnum); }
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); }
public void SetValue(int index, ICefValue value) { Wrapped.SetValue(index, value.Unwrap <CefValue>()); }
public bool CanHandle(ICefValue cefValue, Type targetType) { return(targetType == typeof(ICefValue)); }
public static void SetInt64(this ICefValue @this, long value) { SetInt64(_ => @this.SetBinary(_), value); }
public bool CanHandle(ICefValue cefValue, Type targetType) { return(cefValue.GetValueType() == CefValueType.Dictionary && targetType == typeof(PropertyDescriptor)); }