New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MrBean does not generate full generic type signatures (TypeReference + Nested Generics) #1

Closed
cowtowncoder opened this Issue Feb 22, 2012 · 5 comments

Comments

Projects
None yet
2 participants
@cowtowncoder
Member

cowtowncoder commented Feb 22, 2012

(moved from https://jira.codehaus.org/browse/JACKSON-479)

I would like to use ObjectMapper to read json into some interfaces that I have defined and am providing a TypeReference that has nested generic types such as the following:


TypeReference<Results<Dog>>

{'records':[{'breed':'Mountain Cur','name':'Fido'}],'total':1}

interface Results<T> {
Long getTotal();

Set<T> getRecords();
}

interface Dog {
String getName();

String getBreed();
}

In this example ObjectMapper materializes the json into a Results type object, containing a set of LinkedHashMaps instead of a set of the nested generic type (Dog in this example). I have attached a unit test that reproduces this behavior.

@cowtowncoder

This comment has been minimized.

Show comment
Hide comment
@cowtowncoder

cowtowncoder Feb 22, 2012

Member
// ... imports
public class TypeReferenceNestedGenericTest {
  public interface Results<T> {
    Long getTotal();
    Set<T> getRecords();
  }
  public interface Dog {
    String getName();
    String getBreed();
  }

  @Test
 public void testTypeReferenceNestedGenerics() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.getDeserializationConfig().setAbstractTypeResolver(new AbstractTypeMaterializer());
    mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
    final String jsonString = "{'records':[{'breed':'Mountain Cur','name':'Fido'}],'total':1}";
final Results<Dog> result = mapper.readValue(jsonString, new TypeReference<Results<Dog>>() {
     for (final Object o : result.getRecords()) {
      Assert.assertTrue("Encountered record of unexpected type: " + o.getClass(), Dog.class.isInstance(o));
    }
}
Member

cowtowncoder commented Feb 22, 2012

// ... imports
public class TypeReferenceNestedGenericTest {
  public interface Results<T> {
    Long getTotal();
    Set<T> getRecords();
  }
  public interface Dog {
    String getName();
    String getBreed();
  }

  @Test
 public void testTypeReferenceNestedGenerics() {
    final ObjectMapper mapper = new ObjectMapper();
    mapper.getDeserializationConfig().setAbstractTypeResolver(new AbstractTypeMaterializer());
    mapper.configure(Feature.ALLOW_SINGLE_QUOTES, true);
    final String jsonString = "{'records':[{'breed':'Mountain Cur','name':'Fido'}],'total':1}";
final Results<Dog> result = mapper.readValue(jsonString, new TypeReference<Results<Dog>>() {
     for (final Object o : result.getRecords()) {
      Assert.assertTrue("Encountered record of unexpected type: " + o.getClass(), Dog.class.isInstance(o));
    }
}
@jonnyzzz

This comment has been minimized.

Show comment
Hide comment
@jonnyzzz

jonnyzzz Nov 6, 2012

Contributor

Have exactly the same issue

Contributor

jonnyzzz commented Nov 6, 2012

Have exactly the same issue

@cowtowncoder

This comment has been minimized.

Show comment
Hide comment
@cowtowncoder

cowtowncoder Nov 6, 2012

Member

This is unfortunately quite difficult to fix. But maybe someone with good ASM skills can figure it out.

Member

cowtowncoder commented Nov 6, 2012

This is unfortunately quite difficult to fix. But maybe someone with good ASM skills can figure it out.

@jonnyzzz

This comment has been minimized.

Show comment
Hide comment
@jonnyzzz

jonnyzzz Nov 7, 2012

Contributor

I submitted a pull request for the issue. Please take a look into it

Contributor

jonnyzzz commented Nov 7, 2012

I submitted a pull request for the issue. Please take a look into it

@cowtowncoder

This comment has been minimized.

Show comment
Hide comment
@cowtowncoder

cowtowncoder Apr 23, 2013

Member

Forgot to close; included in 2.1.2 release.

Member

cowtowncoder commented Apr 23, 2013

Forgot to close; included in 2.1.2 release.

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