コード例 #1
0
        public override IDictionary <string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            _processedObjects.Add(obj.GetHashCode());
            var type       = obj.GetType();
            var properties = type.GetProperties().
                             Where(a => a.CanRead &&
                                   a.GetIndexParameters().Count() == 0 &&
                                   _builtInTypes.Contains(a.PropertyType));

            var result = properties.ToDictionary(a => a.Name, a => (object)TryGetStringValue(a, obj));

            if (_maxDepth >= _currentDepth)
            {
                var complexProperties = type.GetProperties().
                                        Where(a => a.CanRead &&
                                              a.CanWrite &&
                                              !a.Name.EndsWith("Reference") &&
                                              !_builtInTypes.Contains(a.PropertyType) &&
                                              !AllreadyAdded(a, obj) &&
                                              !_processedObjects.Contains(a.GetValue(obj, null) == null ? 0 : a.GetValue(obj, null).GetHashCode()));

                foreach (var property in complexProperties)
                {
                    var complexValue = TryGetValue(property, obj);
                    if (complexValue != null)
                    {
                        var js = new EFJavaScriptConverter(_maxDepth - _currentDepth, this);
                        result.Add(property.Name, js.Serialize(complexValue, new EFJavaScriptSerializer()));
                    }
                }
            }

            return(result);
        }
コード例 #2
0
 public EFJavaScriptConverter(int maxDepth = 1, EFJavaScriptConverter parent = null)
 {
     _maxDepth = maxDepth;
     if (parent != null)
     {
         _currentDepth += parent._currentDepth;
     }
 }
コード例 #3
0
 public EFJavaScriptSerializer(int maxDepth = 1, EFJavaScriptConverter parent = null)
 {
     RegisterConverters(new List <JavaScriptConverter> {
         new EFJavaScriptConverter(maxDepth, parent)
     });
 }