zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    WPF 组织机构摄像机树 全量加载 大数据量 分页加载摄像机节点
    vue3 自定义指令(简易版防抖、节流)
    测试Writer
    The blog In The cnblogs!
    分割
    coeLmiGMmW
    js 之 setTimeout 0 分析
    vue 组件 之 注册 及 组件内data的使用
    Js/es for(let i in Obj)效率分析及优化
    vue.js 表单控件 输入绑定 vmodel的使用
  • 原文地址:https://www.cnblogs.com/lummon/p/4647266.html
Copyright © 2011-2022 走看看