コード例 #1
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);
        }
コード例 #2
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);
                }
        }
コード例 #3
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);
                }
        }
コード例 #4
0
        public CefValue Serialize(object obj, HashSet <object> seen, ObjectSerializer objectSerializer)
        {
            var type = obj?.GetType();

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

            var array = (Array)obj;

            using (var value = CefListValue.Create())
            {
                for (var i = 0; i < array.Length; i++)
                {
                    value.SetValue(i, objectSerializer.Serialize(array.GetValue(i), seen));
                }

                var result = CefValue.Create();
                result.SetList(value);
                return(result);
            }
        }