Skip to content

Support deserialization of generic types #293

Description

@xiazuojie

Describe the proposal

The following deserialize API asks for a Class<T>. It does not work with generic types.

<T> T deserialize(byte[] data, Class<T> clazz) throws IOException

Maybe add another method with the second param as java.lang.reflect.Type, which can accept both java.lang.Class and java.lang.reflect.ParameterizedType.

<T> T deserialize(byte[] data, java.lang.reflect.Type type) throws IOException

Jackson works with java.lang.reflect.Type as follows:

    public <T> T deserialize(byte[] data, java.lang.reflect.Type type) throws IOException {
        return objectMapper.readValue(data, objectMapper.constructType(type));
    }

Activity

  1. changed the title [-]Support deserialization of generic type[/-] [+]Support deserialization of generic types[/+] on Jun 9, 2020
  2. artursouza commented on Jun 9, 2020

    @artursouza
    Contributor

    I agree with this change 100%. Can you make a PR?

  3. xiazuojie commented on Jun 9, 2020

    @xiazuojie
    ContributorAuthor

    I agree with this change 100%. Can you make a PR?

    However, java.lang.reflect.Type is not easy for users to create an instance of. Jackson has a TypeReference to help users specify generic types easier. Maybe we should work on something like that.

    It involves quite a bit of work. I can PR if it agrees with you.

  4. artursouza commented on Jun 9, 2020

    @artursouza
    Contributor

    Makes sense. Please, go ahead.

  5. artursouza commented on Jul 7, 2020

    @artursouza
    Contributor

    @xiazuojie Do you have plans to create a PR for this?

  6. artursouza commented on Jul 8, 2020

    @artursouza
    Contributor

    I have created a PR. @xiazuojie please, take a look.

  7. self-assigned this
    on Jul 8, 2020
  8. xiazuojie commented on Jul 9, 2020

    @xiazuojie
    ContributorAuthor

    It's exactly what I want. However, maybe we should also keep
    <T> T deserialize(byte[] data, Class<T> clazz)
    so it's not a breaking change. Also for non-generic values, It's easier to use.

    Sorry that I did not take time on the PR.

  9. xiazuojie commented on Jul 9, 2020

    @xiazuojie
    ContributorAuthor

    Also, TypeRef<T> should implement Comparable<TypeRef<T>> (just as Jackson did) as suggested here.

  10. artursouza commented on Jul 9, 2020

    @artursouza
    Contributor
  11. artursouza commented on Jul 9, 2020

    @artursouza
    Contributor

    I've tried the Comparable<TypeRef> trick. It does not block new TypeRef(){} from compiling. So, I did not add it. Actually, even new TypeReference(){}; from Jackson also compiles. This trick does not seem to be applicable anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions