Java Json jsonPrettyPrinter(Object object)

Here you can find the source of jsonPrettyPrinter(Object object)

Description

This printer only support JsonObject and JsonArray

License

Open Source License

Parameter

Parameter Description
object a parameter

Declaration


public static void jsonPrettyPrinter(Object object) 

Method Source Code

//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());
    }
}

Related

  1. getJsonMapper()
  2. hasKey(JsonObject object, String name)
  3. jsonEquals(JsonArray expected, JsonArray actual, boolean strict)
  4. jsonObject(String key, JsonValue val)
  5. jsonObjectToBuilder(JsonObject jo)
  6. jsonString(JsonValue value)
  7. jsonValueToJsonString(final JsonValue jsonValue, final String key)
  8. listToJsonArray(List list)
  9. mapToJsonObjectBuilder(Map map)