Skip to content

Fix serialization and deserialization of collections of objects #175

Open
@mfateev

Description

@mfateev

Currently parameter of type List 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 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs-triageIssues that need triaging or validation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions