3030package org .apposed .appose .util ;
3131
3232import groovy .json .JsonGenerator ;
33- import groovy .json .JsonSlurper ;
3433import org .apposed .appose .NDArray ;
3534import org .apposed .appose .SharedMemory ;
3635
@@ -68,9 +67,9 @@ private Messages() {
6867
6968 // Registry of class -> (appose_type, encoder) for encoding custom types.
7069 private static final Map <Class <?>, String > ENCODER_TYPES = new ConcurrentHashMap <>();
71- private static final Map <Class <?>, Function <Object , Object >> ENCODER_FNS = new ConcurrentHashMap <>();
70+ private static final Map <Class <?>, Function <Object , Object >> ENCODERS = new ConcurrentHashMap <>();
7271
73- // Registry of appose_type -> factory for decoding custom types.
72+ // Registry of appose_type -> decoder for decoding custom types.
7473 private static final Map <String , Function <Map <String , Object >, Object >> DECODERS =
7574 new ConcurrentHashMap <>();
7675
@@ -83,7 +82,7 @@ private Messages() {
8382 * </p>
8483 * <p>
8584 * When decoding, if a JSON object has the given {@code apposeType}, {@code decoder}
86- * is called with the {@code "data"} field value and should return the
85+ * is called on the and should return the
8786 * reconstructed object.
8887 * </p>
8988 *
@@ -101,7 +100,7 @@ public static <T> void register(
101100 Function <Map <String , Object >, Object > decoder
102101 ) {
103102 ENCODER_TYPES .put (objType , apposeType );
104- ENCODER_FNS .put (objType , (Function <Object , Object >) (Function <?, ?>) encoder );
103+ ENCODERS .put (objType , (Function <Object , Object >) (Function <?, ?>) encoder );
105104 DECODERS .put (apposeType , decoder );
106105 }
107106
@@ -158,7 +157,7 @@ public static String encode(Map<?, ?> data) {
158157 */
159158 @ SuppressWarnings ("unchecked" )
160159 public static Map <String , Object > decode (String json ) {
161- return postProcess (new JsonSlurper (). parseText (json ));
160+ return postProcess (Json . parseJson (json ));
162161 }
163162
164163 /**
@@ -174,7 +173,6 @@ public static String stackTrace(Throwable t) {
174173 return sw .toString ();
175174 }
176175
177-
178176 // -- Serialization --
179177
180178 /*
@@ -206,22 +204,22 @@ public static String stackTrace(Throwable t) {
206204 */
207205
208206 /**
209- * Checks if a type is natively JSON-serializable by Groovy's built-in JSON encoder.
207+ * Checks if a type is natively JSON-serializable by the built-in
208+ * JSON encoder.
210209 * <p>
211210 * These are the basic JSON types that don't need special handling:
212- * Map, List, String (and other CharSequences), Number, Boolean, and primitives.
213- * </p>
214- * <p>
215- * Other types either implement {@link ForJson} (and are handled by the ForJson
216- * converter) or will be auto-proxied as worker_object references when in worker mode.
211+ * Map, List, String (and other CharSequences), Number, Boolean, and
212+ * primitives.
217213 * </p>
218214 * <p>
219- * Note: This list should remain stable. Types implementing {@link ForJson} are
220- * handled before the catch-all and do not require changes here.
215+ * Other types are handled by a registered encoder if available
216+ * (see {@link #register(Class, String, Function, Function)}),
217+ * or else will be auto-proxied as {@code worker_object} references
218+ * when in worker mode.
221219 * </p>
222220 *
223221 * @param type The class to check
224- * @return true if this is a basic JSON type that Groovy can serialize natively
222+ * @return true if this is a basic JSON type that can be serialized natively
225223 */
226224 private static boolean isNativelyJsonSerializable (Class <?> type ) {
227225 return Map .class .isAssignableFrom (type )
@@ -241,7 +239,7 @@ public boolean handles(Class<?> type) {
241239
242240 @ Override
243241 public Object convert (Object value , String key ) {
244- for (Map .Entry <Class <?>, Function <Object , Object >> entry : ENCODER_FNS .entrySet ()) {
242+ for (Map .Entry <Class <?>, Function <Object , Object >> entry : ENCODERS .entrySet ()) {
245243 if (entry .getKey ().isAssignableFrom (value .getClass ())) {
246244 Map <String , Object > map = new LinkedHashMap <>();
247245 map .put ("appose_type" , ENCODER_TYPES .get (entry .getKey ()));
@@ -262,7 +260,7 @@ public boolean handles(Class<?> type) {
262260 // Only active in worker mode.
263261 if (!workerMode ) return false ;
264262
265- // Don't auto-proxy types that Groovy's JSON encoder handles natively.
263+ // Don't auto-proxy types that the JSON encoder handles natively.
266264 // Registered types are earlier in the chain and will have already claimed theirs.
267265 return !isNativelyJsonSerializable (type );
268266 }
@@ -306,8 +304,8 @@ private static Object processValue(Object value) {
306304 // by Proxies.proxifyWorkerObjects() in Service.Task.handle().
307305 return map ;
308306 }
309- Function <Map <String , Object >, Object > factory = DECODERS .get (appose_type );
310- if (factory != null ) return factory .apply (map );
307+ Function <Map <String , Object >, Object > decoder = DECODERS .get (appose_type );
308+ if (decoder != null ) return decoder .apply (map );
311309 System .err .println ("unknown appose_type \" " + appose_type + "\" " );
312310 }
313311 return map ;
0 commit comments