Closed GoogleCodeExporter closed 9 years ago
Here are a few links that might help:
-
http://google-gson.googlecode.com/svn/tags/1.2.3/docs/javadocs/com/google/gson/a
nnotations/Expose.html
-
http://sites.google.com/site/gson/gson-user-guide#TOC-Excluding-Fields-From-Seri
alization
In short, you need to create your Gson instance as follows:
Gson gson = GsonBuilder.excludeFieldsWithoutExposeAnnotation().create();
Hope this helps,
Joel
Original comment by joel.leitch@gmail.com
on 26 Nov 2008 at 1:13
Hello,
I have problem when I specify interface type
@Expose
private ObjectInterface1 obj1;
But everything works fine when I use class type
@Expose
private Object1 obj1;
Thanks
Original comment by ant%biz...@gtempaccount.com
on 26 Nov 2008 at 12:27
Seems to be working for me...what's the issue?
Some test were submitted under r316 and are passing with the current Gson
codebase.
Original comment by joel.leitch@gmail.com
on 26 Nov 2008 at 10:57
Hi,
I've created a few test classes, please verify
Thanks.
Original comment by ant%biz...@gtempaccount.com
on 26 Nov 2008 at 11:53
Attachments:
I'm using gson-1.2.3.jar from the maven repository.
My result is
{"id":1,"name":"Name","manager":{},"contractors":[{}],"employees":{"3":{},"2":{}
}}
Original comment by ant%biz...@gtempaccount.com
on 26 Nov 2008 at 11:57
OK, I see the issue. The tests that I added above only test that the actual
interface field exists in the Json output. In my case, the object had no
fields so I
was expecting an empty object (i.e. {}).
This is somewhat similar to Issue #5. Right now, I do not exactly know the best
approach on how this bug should be fixed, but I do agree that it should be
considered
and fixed in the 1.3 release.
As a workaround, you can register a type adapter for these two interfaces.
Here's an
example of TestInterfaceA (basically replace the A to B to get TestInterfaceB):
public class TestInterfaceATypeAdapter implements
JsonSerializer<TestInterfaceA>,
JsonDeserializer<TestInterfaceA>, InstanceCreator<TestInterfaceA> {
public JsonElement serialize(
TestInterfaceA src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src);
}
public TestInterfaceA createInstance(Type type) {
return new TestClassA();
}
public TestInterfaceA deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return context.deserialize(json, TestClassA.class);
}
}
NOTE: The above also will allow for proper deserialization of the interface
type.
Now when you build your Gson instance you would build it as follows:
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation()
.registerTypeAdapter(TestInterfaceA.class, new TestInterfaceATypeAdapter())
.registerTypeAdapter(TestInterfaceB.class, new TestInterfaceBTypeAdapter())
.create();
Keep tracking this bug to find out how we handle interfaces in the future so
that you
do not have to add these silly type adapters for all interfaces that you define.
Hope this helps,
Joel
Original comment by joel.leitch@gmail.com
on 28 Nov 2008 at 8:42
Here's the output of your provided program when registering the type adapters
for
these interfaces:
{"id":1,"name":"Name","manager":{"id":1,"name":"Manager","history":["ManagerH1",
"ManagerH2"]},"contractors":[{"id":1,"name":"Manager","history":["ManagerH1","Ma
nagerH2"]}],"employees":{"3":{"id":3,"name":"Name3","history":["NameH3"]},"2":{"
id":2,"name":"Name2","history":["NameH2"]}}}
Original comment by joel.leitch@gmail.com
on 28 Nov 2008 at 8:46
Hello,
Any news about this issue?
I've tried google-gson-1.3-release version, but interfaces exposing without Type
Adapters still doesn't work.
Thank you.
Original comment by ant%biz...@gtempaccount.com
on 14 Apr 2009 at 1:13
This is fixed in 1.4 (will be released soon).
Added tests to prove it (r458).
Original comment by joel.leitch@gmail.com
on 29 Sep 2009 at 7:05
Original issue reported on code.google.com by
ant%biz...@gtempaccount.com
on 25 Nov 2008 at 10:18