Пример #1
0
        public static string Dump(this Delegate fn)
        {
            var method = fn.GetType().GetMethod("Invoke");
            var sb     = StringBuilderThreadStatic.Allocate();

            foreach (var param in method.GetParameters())
            {
                if (sb.Length > 0)
                {
                    sb.Append(", ");
                }

                sb.AppendFormat("{0} {1}", param.ParameterType.Name, param.Name);
            }

            var methodName = fn.Method.Name;
            var info       = $"{method.ReturnType.Name} {methodName}({StringBuilderThreadStatic.ReturnAndFree(sb)})";

            return(info);
        }
        public static string Format(string serializedText)
        {
            if (string.IsNullOrEmpty(serializedText))
            {
                return(null);
            }

            var tabCount          = 0;
            var sb                = StringBuilderThreadStatic.Allocate();
            var firstKeySeparator = true;
            var inString          = false;

            for (var i = 0; i < serializedText.Length; i++)
            {
                var current  = serializedText[i];
                var previous = i - 1 >= 0 ? serializedText[i - 1] : 0;
                var next     = i < serializedText.Length - 1 ? serializedText[i + 1] : 0;

                if (current == JsWriter.MapStartChar || current == JsWriter.ListStartChar)
                {
                    if (previous == JsWriter.MapKeySeperator)
                    {
                        if (next == JsWriter.MapEndChar || next == JsWriter.ListEndChar)
                        {
                            sb.Append(current);
                            sb.Append(serializedText[++i]); //eat next
                            continue;
                        }

                        AppendTabLine(sb, tabCount);
                    }

                    sb.Append(current);
                    AppendTabLine(sb, ++tabCount);
                    firstKeySeparator = true;
                    continue;
                }

                if (current == JsWriter.MapEndChar || current == JsWriter.ListEndChar)
                {
                    AppendTabLine(sb, --tabCount);
                    sb.Append(current);
                    firstKeySeparator = true;
                    continue;
                }

                if (current == JsWriter.QuoteChar)
                {
                    sb.Append(current);
                    inString = !inString;
                    continue;
                }

                if (current == JsWriter.ItemSeperator && !inString)
                {
                    sb.Append(current);
                    AppendTabLine(sb, tabCount);
                    firstKeySeparator = true;
                    continue;
                }

                sb.Append(current);

                if (current == JsWriter.MapKeySeperator && firstKeySeparator)
                {
                    sb.Append(" ");
                    firstKeySeparator = false;
                }
            }

            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }
Пример #3
0
        public static string IndentJson(this string json)
        {
            var indent = 0;
            var quoted = false;
            var sb     = StringBuilderThreadStatic.Allocate();

            for (var i = 0; i < json.Length; i++)
            {
                var ch = json[i];
                switch (ch)
                {
                case '{':
                case '[':
                    sb.Append(ch);
                    if (!quoted)
                    {
                        sb.AppendLine();
                        times(++indent, () => sb.Append(Indent));
                    }
                    break;

                case '}':
                case ']':
                    if (!quoted)
                    {
                        sb.AppendLine();
                        times(--indent, () => sb.Append(Indent));
                    }
                    sb.Append(ch);
                    break;

                case '"':
                    sb.Append(ch);
                    var escaped = false;
                    var index   = i;
                    while (index > 0 && json[--index] == '\\')
                    {
                        escaped = !escaped;
                    }
                    if (!escaped)
                    {
                        quoted = !quoted;
                    }
                    break;

                case ',':
                    sb.Append(ch);
                    if (!quoted)
                    {
                        sb.AppendLine();
                        times(indent, () => sb.Append(Indent));
                    }
                    break;

                case ':':
                    sb.Append(ch);
                    if (!quoted)
                    {
                        sb.Append(" ");
                    }
                    break;

                default:
                    sb.Append(ch);
                    break;
                }
            }
            return(StringBuilderThreadStatic.ReturnAndFree(sb));
        }