Ejemplo n.º 1
0
        private static string _ToJson <T>(this T to, long depthCount = 1)
        {
            var json = "{";

            var properties = to.GetType().GetProperties();

            foreach (var property in properties)
            {
                var ommit      = false;
                var toProperty = to.GetType().GetProperty(property.Name);

                if (toProperty == null)
                {
                    continue;
                }

                ommit = ommitPropertiesBySegment.Any(ommitPropertySegment =>
                {
                    if (property.Name.Contains(ommitPropertySegment))
                    {
                        return(true);
                    }

                    return(false);
                });

                if (ommit)
                {
                    continue;
                }

                var    isAssemblyObject = false;
                object value            = to;

                if (to.GetType().GetInterface("IEnumerable") == null && (to.GetType() != typeof(System.String)))
                {
                    value = property.GetValue(to, null) ?? "";

                    if (value.ToString().Length > 0)
                    {
                        var assemblyClasses = Assembly.GetExecutingAssembly().GetTypes().Where(i => i.IsClass);

                        if (assemblyClasses.Any(assemblyClass =>
                        {
                            if (assemblyClass.ToString() == value.ToString())
                            {
                                return(true);
                            }

                            if (value.GetType().GetInterface("IEnumerable") != null)
                            {
                                foreach (var tmp in (IEnumerable)value)
                                {
                                    if (assemblyClass.ToString() == tmp.ToString())
                                    {
                                        return(true);
                                    }
                                }
                            }

                            return(false);
                        }))
                        {
                            isAssemblyObject = true;
                        }
                    }
                }

                var type = value.GetType().ToString();

                if (type.Contains("Elmah") || type.Contains("Exception"))
                {
                    isAssemblyObject = true;
                }

                if ((isAssemblyObject) && (depthCount <= maxDepthAllowed))
                {
                    value = value.ToJson(depthCount + 1);
                }
                else
                {
                    if (value.GetType().GetInterface("IEnumerable") != null)
                    {
                        var dynValue = (dynamic)value;
                        int max      = 0;

                        try
                        {
                            max = Enumerable.Count(dynValue);
                        }
                        catch (Exception ex)
                        {
                        }

                        if (max == 0)
                        {
                            value = "[]";
                        }
                        else
                        {
                            var tmpJson = "[";
                            var counter = 1;

                            if (depthCount < maxDepthAllowed)
                            {
                                foreach (var item in dynValue)
                                {
                                    //tmpJson += ObjectExtentions.ToJson(item, depthCount);
                                    if (item.GetType().GetInterface("IEnumerator") != null)
                                    {
                                        tmpJson += ObjectExtentions.ToJson(item, depthCount);
                                    }
                                    else
                                    {
                                        tmpJson += item;
                                    }

                                    if (counter >= max)
                                    {
                                        continue;
                                    }
                                    tmpJson += ", ";
                                    counter++;
                                }
                            }

                            tmpJson += "]";

                            value = tmpJson;
                        }
                    }
                    else
                    {
                        if (value is string || value is int || value is long || value is DateTime)
                        {
                            value = "\"" + StringHelper.JavascriptStringEncode(value.ToString().Replace(System.Environment.NewLine, "")) + "\"";
                        }
                        else if (value is object)
                        {
                            if (depthCount < maxDepthAllowed)
                            {
                                depthCount = depthCount + 1;
                                value      = _ToJson(value, depthCount);
                            }
                            else
                            {
                                value = "\"" + StringHelper.JavascriptStringEncode(value.ToString().Replace(System.Environment.NewLine, "")) + "\"";
                            }
                        }
                    }
                }

                if (ommitValuesBySegment.Any(ommitValueBySegment => value.ToString().Contains(ommitValueBySegment)))
                {
                    ommit = true;
                }

                if (ommit)
                {
                    continue;
                }

                json += "\"" + property.Name + "\" : " + value.ToString() + ", ";
            }

            json += "}";

            json = json.Replace(Environment.NewLine, "").Replace(", }", "}");

            return(json);
        }