4
4
import java .io .IOException ;
5
5
import java .io .StringReader ;
6
6
import java .util .*;
7
+ import java .util .concurrent .ConcurrentHashMap ;
7
8
8
9
/**
9
10
* Copyright 2017 Arraying
23
24
@ SuppressWarnings ({"WeakerAccess" , "unused" , "UnusedReturnValue" })
24
25
public class JSON {
25
26
26
- private final Map <String , Object > rawContent = new LinkedHashMap <>();
27
27
private static final JSONUtil util = new JSONUtil ();
28
-
29
- private JSONMarshalFormat format = null ;
28
+ private final Object writeLock = new Object ();
29
+ private Map <String , Object > rawContent = new HashMap <>();
30
+ private JSONFormatter formatter = new JSONFormatter .DefaultImplementation ();
30
31
31
32
/**
32
33
* Creates an empty JSON object.
@@ -118,6 +119,39 @@ public JSON(File file)
118
119
this (util .getFileContent (file , true ));
119
120
}
120
121
122
+ /**
123
+ * Specifies the map type to use for internal storage.
124
+ * This is so that when marshalling some specific ordering is retained.
125
+ * This map will get cleared.
126
+ * Note that a {@link java.util.concurrent.ConcurrentHashMap} is not allowed.
127
+ * @param map The map.
128
+ */
129
+ public void use (Map <String , Object > map ) {
130
+ if (map == null ) {
131
+ throw new IllegalArgumentException ("Provided map is null" );
132
+ }
133
+ if (map instanceof ConcurrentHashMap ) {
134
+ throw new IllegalArgumentException ("ConcurrentHashMap not allowed" );
135
+ }
136
+ Map <String , Object > cache = new HashMap <>(rawContent );
137
+ map .clear ();
138
+ synchronized (writeLock ) {
139
+ rawContent = map ;
140
+ rawContent .putAll (cache );
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Specifies the formatter to use when marshalling to a string.
146
+ * @param formatter A formatter instance.
147
+ */
148
+ public void use (JSONFormatter formatter ) {
149
+ if (formatter == null ) {
150
+ throw new IllegalArgumentException ("Formatter is null" );
151
+ }
152
+ this .formatter = formatter ;
153
+ }
154
+
121
155
/**
122
156
* Adds an entry to the current JSON object.
123
157
* This entry can either be a raw JSON data type, or a custom object.
@@ -132,7 +166,9 @@ public JSON put(String key, Object entry)
132
166
if (key == null ) {
133
167
throw new IllegalArgumentException ("Provided key is null" );
134
168
}
135
- rawContent .put (key , util .getFinalValue (entry ));
169
+ synchronized (writeLock ) {
170
+ rawContent .put (key , util .getFinalValue (entry ));
171
+ }
136
172
return this ;
137
173
}
138
174
@@ -147,7 +183,9 @@ public JSON remove(String key)
147
183
if (key == null ) {
148
184
throw new IllegalArgumentException ("Provided key is null" );
149
185
}
150
- rawContent .remove (key );
186
+ synchronized (writeLock ) {
187
+ rawContent .remove (key );
188
+ }
151
189
return this ;
152
190
}
153
191
@@ -286,27 +324,19 @@ public final int length() {
286
324
* @return A string representation of the JSON object.
287
325
*/
288
326
public final String marshal () {
289
- JSONFormatter formatter = new JSONFormatter ()
290
- .startObject ();
327
+ formatter .startObject ();
291
328
Iterator <Map .Entry <String , Object >> iterator = rawContent .entrySet ().iterator ();
292
329
while (iterator .hasNext ()) {
293
330
Map .Entry <String , Object > entry = iterator .next ();
294
331
formatter .objectKey (entry .getKey ());
295
332
Object valueRaw = entry .getValue ();
296
- if (valueRaw instanceof JSON ) {
297
- formatter .object (((JSON ) valueRaw ).marshal ());
298
- } else if (valueRaw instanceof JSONArray ) {
299
- formatter .array (((JSONArray ) valueRaw ).marshal ());
300
- } else {
301
- formatter .value (valueRaw );
302
- }
333
+ util .format (formatter , valueRaw );
303
334
if (iterator .hasNext ()) {
304
335
formatter .comma ();
305
336
}
306
337
}
307
338
formatter .endObject ();
308
-
309
- return format != null ? format .format (formatter .result ()) : formatter .result ();
339
+ return formatter .result ();
310
340
}
311
341
312
342
/**
@@ -336,16 +366,6 @@ public final <T> T marshal(Class<T> clazz, String... ignoredKeys)
336
366
return new JSONORM <>(clazz ).mapTo (this , ignoredKeys );
337
367
}
338
368
339
- /**
340
- * Set formatting to be applied for String marshal()
341
- * @param format - Format to be applied to String marshal()
342
- */
343
-
344
- public JSON setFormat (JSONMarshalFormat format ) {
345
- this .format = format ;
346
- return this ;
347
- }
348
-
349
369
/**
350
370
* Converts the JSON object to a string.
351
371
* This method invokes the {@link #marshal()} method.
0 commit comments