AllenCoder / google-gson

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

Serialization and Deserialization of Type and Class #340

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a class like 

Class Function {

public int functionId;
public Type functionId;
public String functionName

}

public void static main ()
{
                Function fc= new Function();        
        fc.functionId(100);
        fc.functionName("Divide");

        Type tmp = new Integer(100).getClass();
        Gson gson = new GsonBuilder.create();
        System.out.println(gson.togson(fc) );
}

I am getting {} ie null for functionType Filed.
What is the issue here ? am I missing something

Original issue reported on code.google.com by DRDO.YOG...@gmail.com on 1 Jul 2011 at 5:23

GoogleCodeExporter commented 9 years ago
Please post coding questions like this on our Google Group 
(https://groups.google.com/forum/#!forum/google-gson) instead of creating bug 
reports.

Not sure if your example is correct. Here are a few suggestions:
new GsonBuilder().create() is same as new Gson()

You are not using Type tmp anywhere. 
In any case, you can get it simply as: Integer.class or int.class

gson.toJson() should print all the fields of Function. Please post complete 
working code on Google group and someone will be able to help you better.

Original comment by inder123 on 1 Jul 2011 at 9:54

GoogleCodeExporter commented 9 years ago
Serializing types is actually somewhat of a security problem, so we don't want 
to support it by default. A malicious .json file could cause your application 
to load classes that it wouldn't otherwise; depending on your class path 
loading certain classes could DoS your application.

But it's quite straightforward to write a type adapter to support this in your 
own app.

Original comment by limpbizkit on 1 Jul 2011 at 9:56