<commit_before>﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace Sugar
{
    public static class ObjectExtensions
    {
        public static string ToDump(this object @object, int depth = 1)
        {
            var sb = new StringBuilder();

            sb.AppendLine("{");

            try
            {
                if (@object.GetType().IsPrimitive || @object is string)
                {
                    sb.Append("  ");
                    sb.Append(@object.GetType().Name);
                    sb.AppendFormat(@": ""{0}""", @object);
                    sb.AppendLine(string.Empty);
                }
                else
                {
                    ToDump(@object, 1, sb);

                    sb.AppendLine(string.Empty);
                }
            }
            catch (Exception)
            {
                sb.AppendLine(string.Empty);
                sb.AppendLine("(errors occured during dumping)");
                sb.AppendLine(string.Empty);
            }

            sb.AppendLine("}");

            return sb.ToString();
        }

        private static void ToDump(object @object, int depth, StringBuilder sb, string name = null)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                name = @object.GetType().Name;
            }
            else
            {
                name = string.Format(@"""{0}""", name);
            }

            sb.Append(" ".Repeat(2 * depth));
            sb.Append(name);

            if (@object != null)
            {
                if (@object is IEnumerable)
                {
                    var enumerable = @object as IEnumerable;
                    sb.AppendLine(": [");

                    var first = true;

                    foreach (var element in enumerable)
                    {
                        if (!first)
                        {
                            sb.AppendLine(",");

                        }
                        first = false;

                        if (element != null)
                        {
                            ToDump(element, depth + 1, sb);
                        }
                        else
                        {
                            sb.Append(" ".Repeat(2 * (depth + 1)));
                            sb.Append("null");
                        }
                    }

                    sb.AppendLine(string.Empty);
                    sb.Append(" ".Repeat(2 * depth));
                    sb.Append("]");
                }
                else
                {
                    sb.AppendLine(": {");

                    var properties = @object.GetType().GetProperties();

                    for (var i = 0; i < properties.Length; i++)
                    {
                        var property = @object.GetType().GetProperties()[i];

                        object value;

                        var error = false;

                        try
                        {
                            value = property.GetValue(@object, null);
                        }
                        catch (Exception)
                        {
                            value = "?";

                            error = true;
                        }

                        if (error)
                        {
                            sb.Append(" ".Repeat(2 * (depth + 1)));
                            sb.AppendFormat(@"""{0}"": err", property.Name);
                        }
                        else if (property.PropertyType.IsPrimitive)
                        {
                            sb.Append(" ".Repeat(2*(depth + 1)));
                            sb.AppendFormat(@"""{0}"": ""{1}""", property.Name, value);
                        }
                        else if (property.PropertyType == typeof(DateTime))
                        {
                            sb.Append(" ".Repeat(2 * (depth + 1)));
                            sb.AppendFormat(@"""{0}"": ""{1:yyyy-MM-dd hh:mm:ss}""", property.Name, value);
                        }
                        else if ((!property.PropertyType.IsClass && !property.PropertyType.IsInterface) || property.PropertyType == typeof(string))
                        {
                            if (value == null)
                            {
                                sb.Append(" ".Repeat(2*(depth + 1)));
                                sb.AppendFormat(@"""{0}"": null", property.Name);
                            }
                            else
                            {
                                sb.Append(" ".Repeat(2*(depth + 1)));
                                sb.AppendFormat(@"""{0}"": ""{1}""", property.Name, value);
                            }
                        }
                        else
                        {
                            ToDump(value, depth + 1, sb, property.Name);
                        }

                        if (i < properties.Length - 1 && properties.Length > 1)
                        {
                            sb.AppendLine(",");
                        }
                        else //if (!complexType)
                        {
                            sb.AppendLine(string.Empty);
                        }
                    }
                    sb.Append(" ".Repeat(2*depth));
                    sb.Append("}");
                }
            }
            else
            {
                sb.Append(": null");
            }
        }
    }
}<commit_msg>ToJson() method fixes<commit_after>﻿using System;
using System.Collections;
using System.Linq;
using System.Text;

namespace Sugar
{
    public static class ObjectExtensions
    {
        public static string ToDump(this object @object, int depth = 1)
        {
            var sb = new StringBuilder();

            sb.AppendLine("{");

            try
            {
                if (@object.GetType().IsPrimitive || @object is string)
                {
                    sb.Append("  ");
                    sb.Append(@object.GetType().Name);
                    sb.AppendFormat(@": ""{0}""", @object);
                    sb.AppendLine(string.Empty);
                }
                else
                {
                    ToDump(@object, 1, sb);

                    sb.AppendLine(string.Empty);
                }
            }
            catch (Exception)
            {
                sb.AppendLine(string.Empty);
                sb.AppendLine("(errors occured during dumping)");
                sb.AppendLine(string.Empty);
            }

            sb.AppendLine("}");

            return sb.ToString();
        }

        private static void ToDump(object @object, int depth, StringBuilder sb, string name = null)
        {
            // Prevent too much recursion
            if (depth > 4) return;

            if (string.IsNullOrWhiteSpace(name))
            {
                name = @object.GetType().Name;
            }
            else
            {
                name = string.Format(@"""{0}""", name);
            }

            sb.Append(" ".Repeat(2 * depth));
            sb.Append(name);

            if (@object != null)
            {
                if (@object is IEnumerable)
                {
                    var enumerable = @object as IEnumerable;
                    sb.AppendLine(": [");

                    var first = true;

                    foreach (var element in enumerable)
                    {
                        if (!first)
                        {
                            sb.AppendLine(",");

                        }
                        first = false;

                        if (element != null)
                        {
                            ToDump(element, depth + 1, sb);
                        }
                        else
                        {
                            sb.Append(" ".Repeat(2 * (depth + 1)));
                            sb.Append("null");
                        }
                    }

                    sb.AppendLine(string.Empty);
                    sb.Append(" ".Repeat(2 * depth));
                    sb.Append("]");
                }
                else
                {
                    sb.AppendLine(": {");

                    var properties = @object.GetType().GetProperties();

                    for (var i = 0; i < properties.Length; i++)
                    {
                        var property = @object.GetType().GetProperties()[i];

                        object value;

                        var error = false;

                        try
                        {
                            value = property.GetValue(@object, null);
                        }
                        catch (Exception)
                        {
                            value = "?";

                            error = true;
                        }

                        if (error)
                        {
                            sb.Append(" ".Repeat(2 * (depth + 1)));
                            sb.AppendFormat(@"""{0}"": err", property.Name);
                        }
                        else if (property.PropertyType.IsPrimitive)
                        {
                            sb.Append(" ".Repeat(2 * (depth + 1)));
                            sb.AppendFormat(@"""{0}"": ""{1}""", property.Name, value);
                        }
                        else if (property.PropertyType == typeof(DateTime))
                        {
                            sb.Append(" ".Repeat(2 * (depth + 1)));
                            sb.AppendFormat(@"""{0}"": ""{1:yyyy-MM-dd hh:mm:ss}""", property.Name, value);
                        }
                        else if ((!property.PropertyType.IsClass && !property.PropertyType.IsInterface) || property.PropertyType == typeof(string))
                        {
                            if (value == null)
                            {
                                sb.Append(" ".Repeat(2 * (depth + 1)));
                                sb.AppendFormat(@"""{0}"": null", property.Name);
                            }
                            else
                            {
                                sb.Append(" ".Repeat(2 * (depth + 1)));
                                sb.AppendFormat(@"""{0}"": ""{1}""", property.Name, value);
                            }
                        }
                        else
                        {
                            ToDump(value, depth + 1, sb, property.Name);
                        }

                        if (i < properties.Length - 1 && properties.Length > 1)
                        {
                            sb.AppendLine(",");
                        }
                        else //if (!complexType)
                        {
                            sb.AppendLine(string.Empty);
                        }
                    }
                    sb.Append(" ".Repeat(2 * depth));
                    sb.Append("}");
                }
            }
            else
            {
                sb.Append(": null");
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.11291184327693678, 'fraction_numerical': 0.00578806767586821, 'mean_word_length': 0.7273146724084897, 'pattern_counts': {'":': 18, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5754629', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2377, 'n_words': 476}