comparison 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
comparison
equal deleted inserted replaced
16267:c04bdca850a9 16268:0e092c1ecd64
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any 22 * or visit www.oracle.com if you need additional information or have any
23 * questions. 23 * questions.
24 */ 24 */
25 package com.oracle.truffle.api.utilities; 25 package com.oracle.truffle.api.utilities;
26
27 import java.util.*;
26 28
27 import com.oracle.truffle.api.nodes.*; 29 import com.oracle.truffle.api.nodes.*;
28 import com.oracle.truffle.api.source.*; 30 import com.oracle.truffle.api.source.*;
29 31
30 /** 32 /**
117 } 119 }
118 120
119 public static void restart() { 121 public static void restart() {
120 AstJsonDumpBuilder = new StringBuilder(); 122 AstJsonDumpBuilder = new StringBuilder();
121 } 123 }
124
125 public static JsonObjectBuilder object() {
126 return new JsonObjectBuilder();
127 }
128
129 public static JsonArrayBuilder array() {
130 return new JsonArrayBuilder();
131 }
132
133 public static abstract class JsonStringBuilder {
134 @Override
135 public final String toString() {
136 StringBuilder sb = new StringBuilder();
137 appendTo(sb);
138 return sb.toString();
139 }
140
141 protected abstract void appendTo(StringBuilder sb);
142
143 protected static void appendValue(StringBuilder sb, Object value) {
144 if (value instanceof JsonStringBuilder) {
145 ((JsonStringBuilder) value).appendTo(sb);
146 } else if (value instanceof Integer || value instanceof Boolean || value == null) {
147 sb.append(value);
148 } else {
149 sb.append(quote(String.valueOf(value)));
150 }
151 }
152 }
153
154 public static final class JsonObjectBuilder extends JsonStringBuilder {
155 private final Map<String, Object> contents = new LinkedHashMap<>();
156
157 private JsonObjectBuilder() {
158 }
159
160 public JsonObjectBuilder add(String key, String value) {
161 contents.put(key, value);
162 return this;
163 }
164
165 public JsonObjectBuilder add(String key, Number value) {
166 contents.put(key, value);
167 return this;
168 }
169
170 public JsonObjectBuilder add(String key, Boolean value) {
171 contents.put(key, value);
172 return this;
173 }
174
175 public JsonObjectBuilder add(String key, JsonStringBuilder value) {
176 contents.put(key, value);
177 return this;
178 }
179
180 @Override
181 protected void appendTo(StringBuilder sb) {
182 sb.append("{");
183 boolean comma = false;
184 for (Map.Entry<String, Object> entry : contents.entrySet()) {
185 if (comma) {
186 sb.append(", ");
187 }
188 sb.append(quote(entry.getKey()));
189 sb.append(": ");
190 appendValue(sb, entry.getValue());
191 comma = true;
192 }
193 sb.append("}");
194 }
195 }
196
197 public static final class JsonArrayBuilder extends JsonStringBuilder {
198 private final List<Object> contents = new ArrayList<>();
199
200 private JsonArrayBuilder() {
201 }
202
203 public JsonArrayBuilder add(String value) {
204 contents.add(value);
205 return this;
206 }
207
208 public JsonArrayBuilder add(Number value) {
209 contents.add(value);
210 return this;
211 }
212
213 public JsonArrayBuilder add(Boolean value) {
214 contents.add(value);
215 return this;
216 }
217
218 public JsonArrayBuilder add(JsonStringBuilder value) {
219 contents.add(value);
220 return this;
221 }
222
223 @Override
224 protected void appendTo(StringBuilder sb) {
225 sb.append("[");
226 boolean comma = false;
227 for (Object value : contents) {
228 if (comma) {
229 sb.append(", ");
230 }
231 appendValue(sb, value);
232 comma = true;
233 }
234 sb.append("]");
235 }
236 }
122 } 237 }