Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for custom actions on annotations #269

Open
GoogleCodeExporter opened this issue Mar 19, 2015 · 13 comments
Open

Add support for custom actions on annotations #269

GoogleCodeExporter opened this issue Mar 19, 2015 · 13 comments

Comments

@GoogleCodeExporter
Copy link

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
Copy link
Author

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
Copy link
Author

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

  • Removed labels: Milestone-Release1.7

@GoogleCodeExporter
Copy link
Author

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
Copy link
Author

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
Copy link
Author

This would be really useful.

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

@GoogleCodeExporter
Copy link
Author

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
Copy link
Author

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:

@inder123
Copy link
Collaborator

#930

@fwonce
Copy link

fwonce commented Feb 22, 2017

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

Exactly the same need here. Another reason: if MyThing in the example above is located in a common package meant to provide as a 3rd party jar and will be serialized to JSON format in some application at runtime. Provided that for good reason the common jar won't include gson in its dependencies, to archive this goal, the application can register a custom TypeAdapter or TypeAdapterFactory serving the fields annotated with @Id. Simple and clean for the client code, right?

I made a commit to my fork for a quick review on the idea. @inder123 will you please review this?

fwonce pushed a commit to fwonce/gson that referenced this issue Mar 2, 2017
fwonce pushed a commit to fwonce/gson that referenced this issue Mar 2, 2017
@amaksoft
Copy link

amaksoft commented Jun 17, 2017

Hi, i want to share my implementation of custom annotation support for gson, which is backwards compatible:
new interface ParametrizedTypeAdapterFactory extends TypeAdapterFactory and new method
<T> TypeAdapter<T> create(Gson gson, TypeToken<T> type, Annotation[] annotations)
only gets called if field is annotated with specific annotation @Parametrized

I mostly wanted it for serializing some dates by specifying custom pattern in annotation like this:

class Person {

  @Expose
  @SerializedName("birth_date")
  @Parametrized
  @CustomDateFormat("MMMM d, yyyy h:mm:ss a z")
  private Date birthDate;

}

Here is a draft implementation:
amaksoft@b264334
Can you please review it?

@amaksoft
Copy link

Just wanted to explain my position:
ParamterizedTypeAdapterFactory is just a special case of TypeAdapterFactory. In most cases Gson don't even need to know that your factory accepts annotations. But in special cases like @CustomDateFormat on Date or @Id on int, you return a customized TypeAdapter only for this case, (we don't cache it).
No new lists of adapters needed, any of your existing TypeAdapters could be easily transformed into parametrized ones by implementing a new method.

@momomo
Copy link

momomo commented Dec 3, 2020

@amaksoft Why this is not yet accepted? Jeezus.

Related?
https://stackoverflow.com/questions/65130321/adding-a-custom-typeadapterfactory-for-google-gson-to-detect-use-of-annotations

Hi, i want to share my implementation of custom annotation support for gson, which is backwards compatible:
new interface ParametrizedTypeAdapterFactory extends TypeAdapterFactory and new method
<T> TypeAdapter<T> create(Gson gson, TypeToken<T> type, Annotation[] annotations)
only gets called if field is annotated with specific annotation @Parametrized

I mostly wanted it for serializing some dates by specifying custom pattern in annotation like this:

class Person {

  @Expose
  @SerializedName("birth_date")
  @Parametrized
  @CustomDateFormat("MMMM d, yyyy h:mm:ss a z")
  private Date birthDate;

}

Here is a draft implementation:
amaksoft@b264334
Can you please review it?

@momomo
Copy link

momomo commented Dec 3, 2020

Anyway this can be added without hacking google gson library ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants