Mercurial > hg > graal-compiler
annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/JSONHelper.java @ 16268:0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
author | Andreas Woess <andreas.woess@jku.at> |
---|---|
date | Thu, 26 Jun 2014 18:04:47 +0200 |
parents | e8eeee2176ff |
children | f681a647246c |
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 |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
27 import java.util.*; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
28 |
16152
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
29 import com.oracle.truffle.api.nodes.*; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
30 import com.oracle.truffle.api.source.*; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
31 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
32 /** |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
33 * 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
|
34 */ |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
35 public class JSONHelper { |
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 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
|
38 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
39 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
|
40 if (AstJsonDumpBuilder != null) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
41 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
|
42 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
43 } |
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 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
|
46 if (AstJsonDumpBuilder != null) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
47 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
|
48 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
49 } |
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 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
|
52 if (AstJsonDumpBuilder != null) { |
16153
e8eeee2176ff
fix JSON output format
Christian Wirth <christian.wirth@oracle.com>
parents:
16152
diff
changeset
|
53 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
|
54 "\", \"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
|
55 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
56 } |
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 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
|
59 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
|
60 if (sourceSection != null) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
61 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
|
62 } else { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
63 return ""; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
64 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
65 } |
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 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
|
68 return AstJsonDumpBuilder.toString(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
69 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
70 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
71 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
|
72 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
|
73 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
74 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
75 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
|
76 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
|
77 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
78 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
79 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
|
80 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
|
81 builder.append('"'); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
82 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
|
83 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
|
84 switch (c) { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
85 case '"': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
86 builder.append("\\\""); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
87 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
88 case '\\': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
89 builder.append("\\\\"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
90 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
91 case '\b': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
92 builder.append("\\b"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
93 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
94 case '\f': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
95 builder.append("\\f"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
96 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
97 case '\n': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
98 builder.append("\\n"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
99 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
100 case '\r': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
101 builder.append("\\r"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
102 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
103 case '\t': |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
104 builder.append("\\t"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
105 break; |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
106 default: { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
107 if (c < ' ') { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
108 builder.append("\\u00"); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
109 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
|
110 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
|
111 } else { |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
112 builder.append(c); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
113 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
114 } |
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 builder.append('"'); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
118 return builder.toString(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
119 } |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
120 |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
121 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
|
122 AstJsonDumpBuilder = new StringBuilder(); |
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
123 } |
16268
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
124 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
125 public static JsonObjectBuilder object() { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
126 return new JsonObjectBuilder(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
127 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
128 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
129 public static JsonArrayBuilder array() { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
130 return new JsonArrayBuilder(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
131 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
132 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
133 public static abstract class JsonStringBuilder { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
134 @Override |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
135 public final String toString() { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
136 StringBuilder sb = new StringBuilder(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
137 appendTo(sb); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
138 return sb.toString(); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
139 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
140 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
141 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
|
142 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
143 protected static void appendValue(StringBuilder sb, Object value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
144 if (value instanceof JsonStringBuilder) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
145 ((JsonStringBuilder) value).appendTo(sb); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
146 } 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
|
147 sb.append(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
148 } else { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
149 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
|
150 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
151 } |
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 public static final class JsonObjectBuilder extends JsonStringBuilder { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
155 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
|
156 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
157 private JsonObjectBuilder() { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
158 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
159 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
160 public JsonObjectBuilder add(String key, String value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
161 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
162 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
163 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
164 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
165 public JsonObjectBuilder add(String key, Number value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
166 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
167 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
168 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
169 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
170 public JsonObjectBuilder add(String key, Boolean value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
171 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
172 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
173 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
174 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
175 public JsonObjectBuilder add(String key, JsonStringBuilder value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
176 contents.put(key, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
177 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
178 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
179 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
180 @Override |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
181 protected void appendTo(StringBuilder sb) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
182 sb.append("{"); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
183 boolean comma = false; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
184 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
|
185 if (comma) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
186 sb.append(", "); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
187 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
188 sb.append(quote(entry.getKey())); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
189 sb.append(": "); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
190 appendValue(sb, entry.getValue()); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
191 comma = true; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
192 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
193 sb.append("}"); |
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 } |
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 public static final class JsonArrayBuilder extends JsonStringBuilder { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
198 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
|
199 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
200 private JsonArrayBuilder() { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
201 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
202 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
203 public JsonArrayBuilder add(String value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
204 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
205 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
206 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
207 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
208 public JsonArrayBuilder add(Number value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
209 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
210 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
211 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
212 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
213 public JsonArrayBuilder add(Boolean value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
214 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
215 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
216 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
217 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
218 public JsonArrayBuilder add(JsonStringBuilder value) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
219 contents.add(value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
220 return this; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
221 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
222 |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
223 @Override |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
224 protected void appendTo(StringBuilder sb) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
225 sb.append("["); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
226 boolean comma = false; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
227 for (Object value : contents) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
228 if (comma) { |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
229 sb.append(", "); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
230 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
231 appendValue(sb, value); |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
232 comma = true; |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
233 } |
0e092c1ecd64
JSONHelper: add builders for JSON objects and arrays
Andreas Woess <andreas.woess@jku.at>
parents:
16153
diff
changeset
|
234 sb.append("]"); |
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 } |
16152
da0847068801
fix line endings, add description and language to node info
Christian Wirth <christian.wirth@oracle.com>
parents:
16151
diff
changeset
|
237 } |