ChineSouad / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Cannot toJson custom Class, that has got a parameterized member #257

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Create following class:

public abstract class RequestBuilder<TParameter, TResponse extends 
IJsonResponse>
{
    private void WriteRequestContent(Object SendObject) throws Exception
    {
        Gson serializer = new Gson();

        String data = null;

        if(ConnectRequest.class.isInstance(SendObject)) {
            data = serializer.toJson((ConnectRequest)SendObject);
        } else { ... }

        this.myWebRequest.setRequestProperty("Content-Length", data.length()+"");
        OutputStreamWriter wr = new OutputStreamWriter(this.myWebRequest.getOutputStream());
        wr.write(data);
        wr.flush();
        wr.close();
    }

    public class ConnectRequest
    {
        // Fields
        public ConnectHeader header;
        public String method;
        public TParameter parameters;

        // Methods
        public ConnectGroovesharkRequest(ConnectHeader myHeader, TParameter myParameter, String myMethod)
        {
            this.header = myHeader;
            this.parameters = myParameter;
            this.method = myMethod;
        }
    }
}
2.
Execute  ;)
3.
Error occurs

What is the expected output? What do you see instead?
Just to convert it to a json-string, instead i got "Expecting parameterized 
type, got class Base.RequestBuilder$ConnectRequest."

What version of the product are you using? On what operating system?
Gson 1.5;

What to do? :(

Original issue reported on code.google.com by scilor.hacker@googlemail.com on 27 Oct 2010 at 6:12

GoogleCodeExporter commented 9 years ago
Issue 258 has been merged into this issue.

Original comment by limpbizkit on 31 Oct 2010 at 2:30

GoogleCodeExporter commented 9 years ago
Your core problem is that you're trying to operate on type parameters whose 
actual types has not been specified. If you call the overload of toJson() that 
takes a type object, you might have more success.

See the TypeToken docs for an example of how to obtain the proper parameterized 
type:
  http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html

Original comment by limpbizkit on 31 Oct 2010 at 2:33

GoogleCodeExporter commented 9 years ago
Issue 259 has been merged into this issue.

Original comment by limpbizkit on 31 Oct 2010 at 2:40