shau-lok / google-gson

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

Add support for custom actions on annotations #269

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
See the thread: 
http://groups.google.com/group/google-gson/browse_thread/thread/1ea949a5e50cc507

by supporting a custom action on an annotation, we will enable support for 
JPA/Hibernate and other kinds of annotations.

Original issue reported on code.google.com by inder123 on 26 Nov 2010 at 7:00

GoogleCodeExporter commented 9 years ago
This support will also fix the problem where you want to mark certain fields as 
required. 

Original comment by inder123 on 26 Nov 2010 at 7:10

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 12 Feb 2012 at 8:44

GoogleCodeExporter commented 9 years ago
What's the status of this issue? If fixed, can you explain me how to do it in 
version 2.2.4?

Original comment by laurent.verbruggen@gmail.com on 7 Jun 2013 at 1:27

GoogleCodeExporter commented 9 years ago
No current plans to implement this though it is something I want to happen.
If you have a specific proposal, it would be great.

Original comment by inder123 on 7 Jun 2013 at 6:20

GoogleCodeExporter commented 9 years ago
This would be really useful.

Original comment by wagnerfr...@gmail.com on 18 Jul 2013 at 12:24

GoogleCodeExporter commented 9 years ago
One simple example: I'm using Objectify on AppEngine, and so one of my entity 
classes is like so:

public class MyThing {
  @Id Long id;
  String title;
}

I want this to map to:
{"id":443,"title":"Thing"}

If I could write a type adapter that triggers off annotations, I could just use 
the existing @Id annnotation to ensure the Long is serialized as a String

Original comment by steve.ar...@gmail.com on 7 Aug 2014 at 5:40

GoogleCodeExporter commented 9 years ago
I needed this kind of functionality too so I started to modify gson code as a 
proof of concept. Basically, I created a new adapter factory interface 
(AnnotatedTypeAdapterFactory) that takes an extra argument (TypeAttributes, 
which is an abstraction of FieldAttributes similar to AnnotatedElement in Java)

  <T> TypeAdapter<T> create(Gson gson, TypeAttributes attributes, TypeToken<T> type);

This way, it is possible to create a custom factory that does what it needs to 
do (and possibly, there could be annotations supported directly by Gson).

The attached diff file can be applied to the trunk svn version from within the 
gson directory.

Original comment by benja...@bpiwowar.net on 4 Oct 2014 at 10:46

Attachments: