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); }
public EFJavaScriptConverter(int maxDepth = 1, EFJavaScriptConverter parent = null) { _maxDepth = maxDepth; if (parent != null) { _currentDepth += parent._currentDepth; } }
public EFJavaScriptSerializer(int maxDepth = 1, EFJavaScriptConverter parent = null) { RegisterConverters(new List <JavaScriptConverter> { new EFJavaScriptConverter(maxDepth, parent) }); }