Closed GoogleCodeExporter closed 9 years ago
Please post a code sample that we can use to reproduce this problem.
Original comment by inder123
on 11 Mar 2009 at 9:56
Is this related to Issue #40?
Original comment by joel.leitch@gmail.com
on 12 Mar 2009 at 5:35
I've got the same error with class:
public class BaseEvent<C extends IContent> implements IEvent<C> {
//private static final Log log = LogFactory.getLog( BaseEvent.class );
private final long timestamp;
private Severity severity;
private final C content;
private transient final Object source;
private final Tag tag;
public BaseEvent( final Tag tag,
final Severity severity,
final C content,
final Object source,
final long timestamp ) {
if ( tag == null ) {
throw new IllegalArgumentException( "Type can't be null" );
}
if ( severity == null ) {
throw new IllegalArgumentException( "Severity can't be null" );
}
if ( source == null ) {
throw new IllegalArgumentException( "Source can't be null" );
}
this.tag = tag;
this.severity = severity;
this.content = content;
this.source = source;
this.timestamp = timestamp;
}
public BaseEvent( final Tag tag,
final Severity severity,
final C content,
final Object source ) {
this( tag, severity, content, source, System.currentTimeMillis() );
}
public Tag getTag() {
return tag;
}
public long getTimestamp() {
return timestamp;
}
public Severity getSeverity() {
return severity;
}
public synchronized void promote( final Severity s ) {
if ( s.more( severity ) ) {
severity = s;
}
}
public C getContent() {
return content;
}
public Object getSource() {
return source;
}
public String toString() {
final StringBuilder sb = new StringBuilder();
sb.append( "Event[" );
sb.append( tag ).append( "; " );
sb.append( severity ).append( "; " );
sb.append( new Date( timestamp ) );
sb.append( "]\n" );
sb.append( content );
return sb.toString();
}
}
Original comment by chere...@gmail.com
on 1 Apr 2009 at 7:25
We have enough tests that use similar kind of types in Gson. See
http://code.google.com/p/google-gson/source/browse/trunk/gson/src/test/java/com/
google/gson/functional/ParameterizedTypesTest.java
for examples.
I also took the specified test and ran it without any problems. One thing to
note is
that if you are directly serializing/deserializing an instance of BaseEvent
then you
need to pass the correct parameterized type using the TypeToken idiom. See
http://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializ
ing-Gener
for examples.
Original comment by inder123
on 1 Apr 2009 at 10:17
r415 includes a fix to show a more helpful message when this situation occurs.
Original comment by inder123
on 1 Apr 2009 at 10:27
Well, may be it is not right place to ask, but I do not really understand why I
require to specify additional params just to serialize generalized type. JS is
easy
typed, why you need so much type info to serialize to it? Is it any option to
serialize "by real type", as if all generics are converted to their <?> version?
Original comment by chere...@gmail.com
on 8 Apr 2009 at 9:14
Unfortunately, this is a Java issue. The way Java Generics were designed, an
pbject
loses all the Generics information (because of type-erasure). This is
generally not
a problem during serialization except in the cases like above. In your example,
BaseEvent class defines its content field as type C (which is called a
TypeVariable).
After type-erasure, the object's field has no information as to what C actually
maps
to. Gson uses some tricks to figure that out, but for that it needs the generic
type
information as specified.
Original comment by inder123
on 8 Apr 2009 at 6:44
Yes, I understand about type-erasure. I do understand about how it complicate
deserializing json -> java too. But I do not
understand what prevent Gson from serializing, say, class Struct<C>{ private C
content; } as if it was class Struct{ private
Object content;} ? As far, as I know, second is exactly what we'll have in
bytecode after type-erasure, and exactly what
we'll see throught Reflection API. In any case, object, reffered by field
"content" will be serialized using it actual type,
not declared one -- why we ever need field declared type? May be I miss
something?
Original comment by chere...@gmail.com
on 9 Apr 2009 at 11:59
Original issue reported on code.google.com by
brsan...@gmail.com
on 19 Feb 2009 at 8:07