Java String Format formatJson(String json)

Here you can find the source of formatJson(String json)

Description

format Json

License

Apache License

Declaration

public static String formatJson(String json) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;

public class Main {

    public static String formatJson(String json) {
        if (json == null || json.trim().length() == 0) {
            return null;
        }/*  w  w  w . j  a va2 s. c  o  m*/
        int fixedLenth = 0;
        ArrayList<String> tokenList = new ArrayList<String>();
        {
            String jsonTemp = json;
            while (jsonTemp.length() > 0) {
                String token = getToken(jsonTemp);
                jsonTemp = jsonTemp.substring(token.length());
                token = token.trim();
                tokenList.add(token);
            }
        }

        for (int i = 0; i < tokenList.size(); i++) {
            String token = tokenList.get(i);
            int length = token.getBytes().length;
            if (length > fixedLenth && i < tokenList.size() - 1 && tokenList.get(i + 1).equals(":")) {
                fixedLenth = length;
            }
        }

        StringBuilder buf = new StringBuilder();
        int count = 0;
        for (int i = 0; i < tokenList.size(); i++) {

            String token = tokenList.get(i);

            if (token.equals(",")) {
                buf.append(token);
                doFill(buf, count);
                continue;
            }
            if (token.equals(":")) {
                buf.append(token);
                continue;
            }
            if (token.equals("{")) {
                String nextToken = tokenList.get(i + 1);
                if (nextToken.equals("}")) {
                    i++;
                    buf.append("{ }");
                } else {
                    count++;
                    buf.append(token);
                    doFill(buf, count);
                }
                continue;
            }
            if (token.equals("}")) {
                count--;
                doFill(buf, count);
                buf.append(token);
                continue;
            }
            if (token.equals("[")) {
                String nextToken = tokenList.get(i + 1);
                if (nextToken.equals("]")) {
                    i++;
                    buf.append("[ ]");
                } else {
                    count++;
                    buf.append(token);
                    doFill(buf, count);
                }
                continue;
            }
            if (token.equals("]")) {
                count--;
                doFill(buf, count);
                buf.append(token);
                continue;
            }

            buf.append(token);
        }
        return buf.toString();
    }

    /**
     * @param json
     * @return
     */
    private static String getToken(String json) {
        StringBuilder buf = new StringBuilder();
        boolean isInYinHao = false;
        while (json.length() > 0) {
            String token = json.substring(0, 1);
            System.err.println(token);
            json = json.substring(1);
            System.err.println(json);

            if (!isInYinHao && (token.equals(":") || token.equals("{") || token.equals("}") || token.equals("[")
                    || token.equals("]") || token.equals(","))) {
                if (buf.toString().trim().length() == 0) {
                    buf.append(token);
                }
                break;
            }
            if (token.equals("\\")) {
                buf.append(token);
                buf.append(json.substring(0, 1));
                json = json.substring(1);
                continue;
            }
            if (token.equals("\"")) {
                buf.append(token);
                if (isInYinHao) {
                    break;
                } else {
                    isInYinHao = true;
                    continue;
                }
            }
            buf.append(token);
        }
        return buf.toString();
    }

    private static void doFill(StringBuilder buf, int count) {
        buf.append("\n");
    }
}

Related

  1. format(String source, String placeholder, Object... objs)
  2. format(String string)
  3. formatCommand(String cmd)
  4. formatDescriptors(String descriptorString)
  5. formatError(String errorMsg)
  6. formatLore(String str)
  7. formatMailtoLink(String emailAddress)
  8. formatMessage(String message)
  9. formatNameToMimeType(final String formatName)