cadence-java-client icon indicating copy to clipboard operation
cadence-java-client copied to clipboard

Fix serialization and deserialization of collections of objects

Open mfateev opened this issue 7 years ago • 0 comments

Currently parameter of type List<UUID> doesn't deserialize correctly. It happens because gson used to implement DataConverter doesn't have enough information about the type of the argument during deserialization. Current interface uses Class<T> as a type passed to gson. The solution is to pass Type object returned from Method.getGenericParameterTypes(). The following code works:

  @Test
  public void testUUIDListGson() throws NoSuchMethodException {
    Method m = JsonDataConverterTest.class.getDeclaredMethod("foo", List.class);
    Type arg = m.getGenericParameterTypes()[0];
    GsonBuilder gsonBuilder =
        new GsonBuilder()
            .serializeNulls();
    Gson gson = gsonBuilder.create();

    List<UUID> list = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
      list.add(UUID.randomUUID());
    }
    String data = gson.toJson(list);
    List<UUID> result = gson.fromJson(data, arg);
    assertEquals(result.toString(), list, result);
  }

The proposal is to change DataConverter API to either accept Method as a parameter or list of Type objects.

mfateev avatar Jun 11 '18 18:06 mfateev