Here you can find the source of jsonPrettyPrinter(Object object)
Parameter | Description |
---|---|
object | a parameter |
public static void jsonPrettyPrinter(Object object)
//package com.java2s; /*// w ww. ja v a 2s . co m * Licensed Materials - Property of IBM * * (C) COPYRIGHT IBM CORP. 2015 All Rights Reserved * * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with * IBM Corp. */ import javax.json.JsonArray; import javax.json.JsonObject; public class Main { /** * This printer only support JsonObject and JsonArray * @param object */ public static void jsonPrettyPrinter(Object object) { if (!(object instanceof JsonObject || object instanceof JsonArray)) { System.out.println(object.toString()); return; } String newLine = System.getProperty("line.separator"); StringBuilder strb = new StringBuilder(); char[] charArr = object.toString().toCharArray(); int depth = 0; boolean newLineMark = false; for (char ch : charArr) { if (ch == '{') { depth++; strb.append(ch).append(newLine); for (int i = 0; i < depth; i++) { strb.append(" "); } continue; } if (ch == '"') { newLineMark = !newLineMark; } if (ch == '[' || ch == ']') { if (ch == ']') { depth--; strb.append(newLine); for (int i = 0; i < depth; i++) { strb.append(" "); } } strb.append(ch); if (ch == '[') { strb.append(newLine); depth++; for (int i = 0; i < depth; i++) { strb.append(" "); } } continue; } if (newLineMark == false && ch == ',') { strb.append(ch).append(newLine); for (int i = 0; i < depth; i++) { strb.append(" "); } continue; } if (ch == '}') { strb.append(newLine); depth--; for (int i = 0; i < depth; i++) { strb.append(" "); } strb.append(ch); continue; } if (ch == ':') { strb.append(ch); if (newLineMark == false) { strb.append(" "); } continue; } strb.append(ch); } System.out.println(strb.toString()); } }