Exemple #1
0
        public string Format(string json)
        {
            var tokens = new JsonLexer(json)
                         .GetTokens()
                         .Where(x => x.TokenType != JsonTokenType.WhiteSpace && x.TokenType != JsonTokenType.Comment)
                         .ToArray();

            if (ConvertToHex)
            {
                tokens = ConvertNumbersToHex(tokens);
            }

            var sb = new StringBuilder();

            foreach (var t in tokens)
            {
                switch (t.TokenType)
                {
                case JsonTokenType.RightBrace:
                case JsonTokenType.RightBracket:
                    _currentIndent--;
                    var indent = new string(' ', _currentIndent *IndentSize);
                    sb.Append("\r\n" + indent);
                    break;
                }

                sb.Append(t.Lexeme);

                if (t.TokenType == JsonTokenType.ColonOperator)
                {
                    sb.Append(' ');
                }

                switch (t.TokenType)
                {
                case JsonTokenType.LeftBrace:
                case JsonTokenType.LeftBracket:
                    _currentIndent++;
                    break;
                }

                switch (t.TokenType)
                {
                case JsonTokenType.LeftBrace:
                case JsonTokenType.LeftBracket:
                case JsonTokenType.Comma:
                    var indent = new string(' ', _currentIndent *IndentSize);
                    sb.Append("\r\n" + indent);
                    break;
                }
            }

            return(sb.ToString());
        }
        public string Format(string json)
        {
            var tokens = new JsonLexer(json)
                .GetTokens()
                .Where(x => x.TokenType != JsonTokenType.WhiteSpace && x.TokenType != JsonTokenType.Comment)
                .ToArray();

            if (ConvertToHex)
            {
                tokens = ConvertNumbersToHex(tokens);
            }

            var sb = new StringBuilder();

            foreach (var t in tokens)
            {
                switch (t.TokenType)
                {
                    case JsonTokenType.RightBrace:
                    case JsonTokenType.RightBracket:
                        _currentIndent--;
                        var indent = new string(' ', _currentIndent * IndentSize);
                        sb.Append("\r\n" + indent);
                        break;
                }

                sb.Append(t.Lexeme);

                if (t.TokenType == JsonTokenType.ColonOperator)
                {
                    sb.Append(' ');
                }

                switch (t.TokenType)
                {
                    case JsonTokenType.LeftBrace:
                    case JsonTokenType.LeftBracket:
                        _currentIndent++;
                        break;
                }

                switch (t.TokenType)
                {
                    case JsonTokenType.LeftBrace:
                    case JsonTokenType.LeftBracket:
                    case JsonTokenType.Comma:
                        var indent = new string(' ', _currentIndent * IndentSize);
                        sb.Append("\r\n" + indent);
                        break;
                }
            }

            return sb.ToString();
        }