Mercurial > hg > truffle
annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/JSONHelper.java @ 22157:dc83cc1f94f2
Using fully qualified imports
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 16 Sep 2015 11:33:22 +0200 |
parents | 9c8c0937da41 |
children |
rev | line source |
---|---|
16152
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
1 /* |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
4 * |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
7 * published by the Free Software Foundation. Oracle designates this |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
8 * particular file as subject to the "Classpath" exception as provided |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
9 * by Oracle in the LICENSE file that accompanied this code. |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
10 * |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
15 * accompanied this code). |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
16 * |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
17 * You should have received a copy of the GNU General Public License version |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
20 * |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
22 * or visit www.oracle.com if you need additional information or have any |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
23 * questions. |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
24 */ |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
25 package com.oracle.truffle.api.utilities; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
26 |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
27 import com.oracle.truffle.api.nodes.Node; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
28 import com.oracle.truffle.api.source.SourceSection; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
29 import java.util.ArrayList; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
30 import java.util.LinkedHashMap; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
31 import java.util.List; |
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
32 import java.util.Map; |
16152
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
33 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
34 /** |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
35 * Helper function that allows to dump the AST during creation to a JSON format. |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
36 */ |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
37 public class JSONHelper { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
38 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
39 private static StringBuilder AstJsonDumpBuilder = new StringBuilder(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
40 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
41 public static void dumpNewChild(Node parentNode, Node childNode) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
42 if (AstJsonDumpBuilder != null) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
43 AstJsonDumpBuilder.append("{ \"action\": \"insertNode\", \"parentId\": \"" + getID(parentNode) + "\", \"newId\": \"" + getID(childNode) + "\" },\n"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
44 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
45 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
46 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
47 public static void dumpReplaceChild(Node oldNode, Node newNode, CharSequence reason) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
48 if (AstJsonDumpBuilder != null) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
49 AstJsonDumpBuilder.append("{ \"action\": \"replaceNode\", \"oldId\": \"" + getID(oldNode) + "\", \"newId\": \"" + getID(newNode) + "\", \"reason\": " + quote(reason) + " },\n"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
50 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
51 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
52 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
53 public static void dumpNewNode(Node newNode) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
54 if (AstJsonDumpBuilder != null) { |
16153
e8eeee2176ff
fix JSON output format
Christian Wirth <christian.wirth@oracle.com>
parents:
16152
diff
changeset
|
55 AstJsonDumpBuilder.append("{ \"action\": \"createNode\", \"newId\": \"" + getID(newNode) + "\", \"type\": \"" + getType(newNode) + "\", \"description\": \"" + newNode.getDescription() + |
e8eeee2176ff
fix JSON output format
Christian Wirth <christian.wirth@oracle.com>
parents:
16152
diff
changeset
|
56 "\", \"language\": \"" + newNode.getLanguage() + "\"" + getSourceSectionInfo(newNode) + " },\n"); |
16152
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
57 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
58 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
59 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
60 private static String getSourceSectionInfo(Node newNode) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
61 SourceSection sourceSection = newNode.getSourceSection(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
62 if (sourceSection != null) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
63 return ", \"identifier\": \"" + sourceSection.getIdentifier() + "\" "; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
64 } else { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
65 return ""; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
66 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
67 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
68 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
69 public static String getResult() { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
70 return AstJsonDumpBuilder.toString(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
71 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
72 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
73 private static String getID(Node newChild) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
74 return String.valueOf(newChild.hashCode()); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
75 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
76 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
77 private static String getType(Node node) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
78 return node.getClass().getSimpleName(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
79 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
80 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
81 private static String quote(CharSequence value) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
82 StringBuilder builder = new StringBuilder(value.length() + 2); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
83 builder.append('"'); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
84 for (int i = 0; i < value.length(); i++) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
85 char c = value.charAt(i); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
86 switch (c) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
87 case '"': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
88 builder.append("\\\""); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
89 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
90 case '\\': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
91 builder.append("\\\\"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
92 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
93 case '\b': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
94 builder.append("\\b"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
95 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
96 case '\f': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
97 builder.append("\\f"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
98 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
99 case '\n': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
100 builder.append("\\n"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
101 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
102 case '\r': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
103 builder.append("\\r"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
104 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
105 case '\t': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
106 builder.append("\\t"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
107 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
108 default: { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
109 if (c < ' ') { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
110 builder.append("\\u00"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
111 builder.append(Character.forDigit((c >> 4) & 0xF, 16)); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
112 builder.append(Character.forDigit(c & 0xF, 16)); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
113 } else { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
114 builder.append(c); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
115 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
116 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
117 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
118 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
119 builder.append('"'); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
120 return builder.toString(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
121 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
122 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
123 public static void restart() { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
124 AstJsonDumpBuilder = new StringBuilder(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
125 } |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
126 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
127 public static JSONObjectBuilder object() { |
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
128 return new JSONObjectBuilder(); |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
129 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
130 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
131 public static JSONArrayBuilder array() { |
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
132 return new JSONArrayBuilder(); |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
133 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
134 |
18163
c88ab4f1f04a
re-enabled Checkstyle with the release of 6.0 that supports Java 8; fixed existing Checkstyle warnings
Doug Simon <doug.simon@oracle.com>
parents:
16473
diff
changeset
|
135 public abstract static class JSONStringBuilder { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
136 @Override |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
137 public final String toString() { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
138 StringBuilder sb = new StringBuilder(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
139 appendTo(sb); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
140 return sb.toString(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
141 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
142 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
143 protected abstract void appendTo(StringBuilder sb); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
144 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
145 protected static void appendValue(StringBuilder sb, Object value) { |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
146 if (value instanceof JSONStringBuilder) { |
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
147 ((JSONStringBuilder) value).appendTo(sb); |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
148 } else if (value instanceof Integer || value instanceof Boolean || value == null) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
149 sb.append(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
150 } else { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
151 sb.append(quote(String.valueOf(value))); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
152 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
153 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
154 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
155 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
156 public static final class JSONObjectBuilder extends JSONStringBuilder { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
157 private final Map<String, Object> contents = new LinkedHashMap<>(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
158 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
159 private JSONObjectBuilder() { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
160 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
161 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
162 public JSONObjectBuilder add(String key, String value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
163 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
164 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
165 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
166 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
167 public JSONObjectBuilder add(String key, Number value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
168 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
169 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
170 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
171 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
172 public JSONObjectBuilder add(String key, Boolean value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
173 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
174 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
175 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
176 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
177 public JSONObjectBuilder add(String key, JSONStringBuilder value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
178 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
179 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
180 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
181 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
182 @Override |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
183 protected void appendTo(StringBuilder sb) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
184 sb.append("{"); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
185 boolean comma = false; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
186 for (Map.Entry<String, Object> entry : contents.entrySet()) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
187 if (comma) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
188 sb.append(", "); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
189 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
190 sb.append(quote(entry.getKey())); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
191 sb.append(": "); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
192 appendValue(sb, entry.getValue()); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
193 comma = true; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
194 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
195 sb.append("}"); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
196 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
197 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
198 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
199 public static final class JSONArrayBuilder extends JSONStringBuilder { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
200 private final List<Object> contents = new ArrayList<>(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
201 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
202 private JSONArrayBuilder() { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
203 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
204 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
205 public JSONArrayBuilder add(String value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
206 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
207 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
208 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
209 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
210 public JSONArrayBuilder add(Number value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
211 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
212 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
213 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
214 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
215 public JSONArrayBuilder add(Boolean value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
216 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
217 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
218 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
219 |
16473
f681a647246c
uppercase JSON for consistency
Andreas Woess <andreas.woess@jku.at>
parents:
16268
diff
changeset
|
220 public JSONArrayBuilder add(JSONStringBuilder value) { |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
221 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
222 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
223 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
224 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
225 @Override |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
226 protected void appendTo(StringBuilder sb) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
227 sb.append("["); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
228 boolean comma = false; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
229 for (Object value : contents) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
230 if (comma) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
231 sb.append(", "); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
232 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
233 appendValue(sb, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
234 comma = true; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
235 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
236 sb.append("]"); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
237 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
238 } |
16152
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
239 } |