Hello,
I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:
public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
[...]
I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.
Hello,
I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:
public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
{
if ( p_class.isAssignableFrom( List.class ) )
{
final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
// here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
....
}
...
}
Hello,
I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:
public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
{
if ( p_class.isAssignableFrom( List.class ) )
{
final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
// here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
....
}
...
}
I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.
Can you please give a little bit help to get the information?
On 10/21/2020 7:38 AM, Philipp Kraus wrote:
Hello,
I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:
public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
{
if ( p_class.isAssignableFrom( List.class ) )
{
final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
// here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
....
}
...
}
I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.
Can you please give a little bit help to get the information?As already pointed out by other then getting the information from
l_data itself may be impossible due to type erasure.
But even if Java did not lose the type, then you would probably be told Object, since the JSON in theory can contain both integers and strings.
If you for whatever reasons are sure that all elements have the same
type, then you can get the first element and look at its type.
Arne
// here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
....
}
...
}
I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.
Can you please give a little bit help to get the information?
Thanks a lot
On 10/21/2020 7:38 AM, Philipp Kraus wrote:
Hello,
I'm implementing an interface of a library, which I can use for deserialization a byte array into my own datastructures. The method is:
public <T> T fromData( @Nonnull final byte[] p_bytes, @Nonnull final Class<T> p_class, @Nonnull final Type p_type ) throws DataConverterException
{
if ( p_class.isAssignableFrom( List.class ) )
{
final List<?> l_data = JsonDataConverter.getInstance().fromData( p_bytes, List.class, List.class );
// here I need to know what explicit is, I know T is a list, but I need a generic type e.g. List<String> => String, List<Integer> => Integer
....
}
...
}
I have taken a look into Google Guava and Apache Commons with the ReflectUtils / TypeUtils, but I don't know How I get get from my p_class Variable the details auf T during runtime.
Can you please give a little bit help to get the information?As already pointed out by other then getting the information from
l_data itself may be impossible due to type erasure.
But even if Java did not lose the type, then you would probably be told Object, since the JSON in theory can contain both integers and strings.
If you for whatever reasons are sure that all elements have the same
type, then you can get the first element and look at its type.
Arne
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 90:24:34 |
Calls: | 6,658 |
Files: | 12,203 |
Messages: | 5,334,090 |