@@ -217,37 +217,39 @@ private Class<?> toClass(ClassLoader classLoader, String name) {
217217 public JsonElement serialize (Invocation src , Type typeOfSrc , JsonSerializationContext context ) {
218218 if (version == 1 ) {
219219 log .warn ("Serializing as deprecated version {}" , version );
220- return serializeV1 (src , typeOfSrc , context );
220+ return serializeV1 (src , context );
221221 }
222222 JsonObject obj = new JsonObject ();
223223 obj .addProperty ("c" , src .getClassName ());
224224 obj .addProperty ("m" , src .getMethodName ());
225225 JsonArray params = new JsonArray ();
226- JsonArray args = new JsonArray ();
227- int i = 0 ;
228- for (Class <?> parameterType : src .getParameterTypes ()) {
229- params .add (nameForClass (parameterType ));
230- Object arg = src .getArgs ()[i ];
231- if (arg == null ) {
232- JsonObject jsonObject = new JsonObject ();
233- jsonObject .add ("t" , null );
234- jsonObject .add ("v" , null );
235- args .add (jsonObject );
236- } else {
237- JsonObject jsonObject = new JsonObject ();
238- jsonObject .addProperty ("t" , nameForClass (arg .getClass ()));
239- jsonObject .add ("v" , context .serialize (arg ));
240- args .add (jsonObject );
226+ if (src .getParameterTypes ().length > 0 ) {
227+ JsonArray args = new JsonArray ();
228+ int i = 0 ;
229+ for (Class <?> parameterType : src .getParameterTypes ()) {
230+ params .add (nameForClass (parameterType ));
231+ Object arg = src .getArgs ()[i ];
232+ if (arg == null ) {
233+ JsonObject jsonObject = new JsonObject ();
234+ jsonObject .add ("t" , null );
235+ jsonObject .add ("v" , null );
236+ args .add (jsonObject );
237+ } else {
238+ JsonObject jsonObject = new JsonObject ();
239+ jsonObject .addProperty ("t" , nameForClass (arg .getClass ()));
240+ jsonObject .add ("v" , context .serialize (arg ));
241+ args .add (jsonObject );
242+ }
243+ i ++;
241244 }
242- i ++ ;
245+ obj . add ( "a" , args ) ;
243246 }
244247 obj .add ("p" , params );
245- obj .add ("a" , args );
246248 obj .add ("x" , context .serialize (src .getMdc ()));
247249 return obj ;
248250 }
249251
250- JsonElement serializeV1 (Invocation src , Type typeOfSrc , JsonSerializationContext context ) {
252+ JsonElement serializeV1 (Invocation src , JsonSerializationContext context ) {
251253 JsonObject obj = new JsonObject ();
252254 obj .addProperty ("c" , src .getClassName ());
253255 obj .addProperty ("m" , src .getMethodName ());
@@ -274,39 +276,46 @@ public Invocation deserialize(
274276 String className = jsonObject .get ("c" ).getAsString ();
275277 String methodName = jsonObject .get ("m" ).getAsString ();
276278
277- JsonArray jsonParams = jsonObject .get ("p" ).getAsJsonArray ();
278- Class <?>[] params = new Class <?>[jsonParams .size ()];
279- for (int i = 0 ; i < jsonParams .size (); i ++) {
280- JsonElement param = jsonParams .get (i );
281- if (param .isJsonObject ()) {
282- // For backwards compatibility
283- params [i ] = classForName (param .getAsJsonObject ().get ("t" ).getAsString ());
284- } else {
285- params [i ] = classForName (param .getAsString ());
279+ Class <?>[] params = null ;
280+ if (jsonObject .has ("p" )) {
281+ JsonArray jsonParams = jsonObject .get ("p" ).getAsJsonArray ();
282+ params = new Class <?>[jsonParams .size ()];
283+ for (int i = 0 ; i < jsonParams .size (); i ++) {
284+ JsonElement param = jsonParams .get (i );
285+ if (param .isJsonObject ()) {
286+ // For backwards compatibility
287+ params [i ] = classForName (param .getAsJsonObject ().get ("t" ).getAsString ());
288+ } else {
289+ params [i ] = classForName (param .getAsString ());
290+ }
286291 }
287292 }
288293
289- JsonElement argsElement = jsonObject .get ("a" );
290- if (argsElement == null ) {
291- // For backwards compatibility
292- argsElement = jsonObject .get ("p" );
293- }
294- JsonArray jsonArgs = argsElement .getAsJsonArray ();
295- Object [] args = new Object [jsonArgs .size ()];
296- for (int i = 0 ; i < jsonArgs .size (); i ++) {
297- JsonElement arg = jsonArgs .get (i );
298- JsonElement argType = arg .getAsJsonObject ().get ("t" );
299- if (argType != null ) {
300- JsonElement argValue = arg .getAsJsonObject ().get ("v" );
301- Class <?> argClass = classForName (argType .getAsString ());
302- try {
303- args [i ] = context .deserialize (argValue , argClass );
304- } catch (Exception e ) {
305- throw new RuntimeException (
306- "Failed to deserialize arg [" + argValue + "] of type [" + argType + "]" , e );
294+ Object [] args = null ;
295+ if (jsonObject .has ("a" )) {
296+ JsonElement argsElement = jsonObject .get ("a" );
297+ if (argsElement == null ) {
298+ // For backwards compatibility
299+ argsElement = jsonObject .get ("p" );
300+ }
301+ JsonArray jsonArgs = argsElement .getAsJsonArray ();
302+ args = new Object [jsonArgs .size ()];
303+ for (int i = 0 ; i < jsonArgs .size (); i ++) {
304+ JsonElement arg = jsonArgs .get (i );
305+ JsonElement argType = arg .getAsJsonObject ().get ("t" );
306+ if (argType != null ) {
307+ JsonElement argValue = arg .getAsJsonObject ().get ("v" );
308+ Class <?> argClass = classForName (argType .getAsString ());
309+ try {
310+ args [i ] = context .deserialize (argValue , argClass );
311+ } catch (Exception e ) {
312+ throw new RuntimeException (
313+ "Failed to deserialize arg [" + argValue + "] of type [" + argType + "]" , e );
314+ }
307315 }
308316 }
309317 }
318+
310319 Map <String , String > mdc = context .deserialize (jsonObject .get ("x" ), Map .class );
311320
312321 return new Invocation (className , methodName , params , args , mdc );
0 commit comments