• JsonFormatter PrettyPrint


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace prettycode.org
    {
        public static class JsonFormatter
        {
            public static string JsCasePropertyNames(string json)
            {
                var buffer = new StringBuilder();
                var inString = false;
    
                for (var i = 0; i < json.Length; i++)
                {
                    var currentChar = json[i];
                    char? previousChar = (i > 0) ? (char?)json[i - 1] : null;
    
                    if (currentChar == '"' && previousChar.HasValue && previousChar != '\')
                    {
                        inString = !inString;
                    }
    
                    if (inString && currentChar == '"' && "{,".Contains(previousChar.Value))
                    {
                        buffer.Append(""" + Char.ToLowerInvariant(json[++i]));
                    }
                    else
                    {
                        buffer.Append(currentChar);
                    }
                }
    
                return buffer.ToString();
            }
    
            public static string PrettyPrint(string json, string indent = "   ")
            {
                var buffer = new StringBuilder();
                var depth = 0;
                var inString = false;
    
                for (var i = 0; i < json.Length; i++)
                {
                    var currentChar = json[i];
    
                    if (currentChar == '"' && i > 0 && json[i - 1] != '\')
                    {
                        inString = !inString;
                    }
    
                    if (inString)
                    {
                        buffer.Append(currentChar);
                    }
                    else if (currentChar == '{' || currentChar == '[')
                    {
                        buffer.Append(currentChar + "
    " + string.Concat(Enumerable.Repeat(indent, ++depth)));
                    }
                    else if (currentChar == '}' || currentChar == ']')
                    {
                        buffer.Append("
    " + string.Concat(Enumerable.Repeat(indent, --depth)) + currentChar);
                    }
                    else if (currentChar == ',')
                    {
                        buffer.Append(",
    " + string.Concat(Enumerable.Repeat(indent, depth)));
                    }
                    else if (currentChar == ':')
                    {
                        buffer.Append(": ");
                    }
                    else
                    {
                        buffer.Append(currentChar);
                    }
                }
    
                return buffer.ToString();
            }
        }
    }
  • 相关阅读:
    SVN Windows环境搭建,简洁演示
    SVN-linux配置
    链接文本在a标签内标签里也可以用driver.find_element_by_link_text
    selenium python自动化简明演示
    关键字中mysql数据库查询条件带中文无结果解决办法
    python 最短路径
    python 难度分割
    c语言实现一个高铁乘客管理系统
    Linux(Ubuntu)系统安装图文教程
    字符串排序
  • 原文地址:https://www.cnblogs.com/lummon/p/4647266.html
Copyright © 2020-2023  润新知