This project has retired. For details please refer to its Attic page.
JsonPrettyPrinter xref

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.server.support.filter;
20  
21  
22  /* 
23   * A super simple JSON pretty printer
24   */
25  
26  public class JsonPrettyPrinter  {
27  
28      private int indent = 0;
29      private String indentStr;
30      private StringBuffer sb = new StringBuffer();
31      
32      public JsonPrettyPrinter () {
33          init(3);
34      }
35  
36      public JsonPrettyPrinter (int indent) {
37          init(indent);
38      }
39      
40      private void init(int indent) {
41          StringBuffer sb = new StringBuffer();
42          for (int i=0; i<indent; i++)
43              sb.append(" ");
44          indentStr = sb.toString();
45      }
46          
47      public String prettyPrint(String jsonStr) {
48          for (int i=0; i<jsonStr.length(); i++)  {
49              char c = jsonStr.charAt(i);
50              writeChar(c);
51          }
52          return sb.toString();
53      }
54      
55      private void writeChar(char c) {
56          if (((char) c) == '[' || ((char) c) == '{') {
57              sb.append(c);
58              sb.append('\n');
59              indent++;
60              addIndent();
61          } else if (((char) c) == ',') {
62              sb.append(c);
63              sb.append('\n');
64              addIndent();
65          } else if (((char) c) == ']' || ((char) c) == '}') {
66              sb.append('\n');
67              indent--;
68              addIndent();
69              sb.append(c);
70          } else {
71              sb.append(c);
72          }
73  
74      }
75  
76      private void addIndent() {
77          for (int i = 0; i < indent; i++) {
78              sb.append(indentStr);
79          }
80      }
81      
82      public static void main(String[] args) {
83          args = new String[2];
84          args[0] = "[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
85          args[1] = "{\"abc\":{\"def\":{\"ghi\":{\"jkl\":[5,{\"mno\":7}]}}}}";
86          for (String s : args) {
87              JsonPrettyPrinter pp = new JsonPrettyPrinter();
88              System.out.println("Pretty Printing JSON String: " + s);
89              String result = pp.prettyPrint(s);
90              System.out.println("Pretty Printed JSON: " + result);
91          }
92      }
93  }