rlalfo / google-http-java-client

Automatically exported from code.google.com/p/google-http-java-client
0 stars 0 forks source link

Allow annotation to specify a value-converter in tandem with @Key #25

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
External references, such as a standards document, or specification?

None that I am aware of.

Java environments (e.g. Java 6, Android 2.3, App Engine 1.4.3, or All)?

All.

Please describe the feature requested.

// framework

/**
 * Specifies a conversion.
 */
interface Converter<T> {
   String toString(T obj);
   T valueOf(String value) throws ParseException;
}

/** 
 * Tell google-api-java-client to apply the specified conversion.
 */ 
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Convert {
   Class<? extends Converter> value();
   // this is better than abusing @Key
}

// application example

import java.awt.Color;

class ColorConverter implements Converter<Color> {
   // has parameterless default constructor

   @Override
   public String toString(Color obj) {
      // format
   }

   @Override
   public Color valueOf(String value) throws ParseException {
      // parse
   }
}

class Calendar {
 @Key("gCal:color")
 @Convert(ColorConverter.class)
 public Color color;
}

Design notes:
  The framework could inspect a Converter<T>'s method annotation to decide whether to invoke the method if the input is null.  This could spare Converter implementors from having to deal with null input.

  A framework-provided "abstract Class DefaultConverter implements Converter" could be useful.

  Parse errors should not stop reading input, but there probably needs to be a way to callback the caller of the parseAs() method.

  The use-case anticipated here doesn't need control over *collections* (eg. @Key("entry") List<Entry> entries;) ... this case can probably be excluded from the design of the Converter.

  This converter doesn't solve more annoying issues like when the <entry/> of a calendar event feed specialises the <gd:comments/> element from the Event kind such that the <gd:feedLink/> points to a feed containing <entry/>s of the Message kind, and shouldn't try to.

Original issue reported on code.google.com by yan...@google.com on 16 Aug 2011 at 2:47

GoogleCodeExporter commented 9 years ago
Thanks dtbull...@gmail.com for the feedback!

Very interesting idea.  We should investigate how other JSON libraries like 
Jackson and GSON handle this.

Original comment by yan...@google.com on 16 Aug 2011 at 2:47

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 16 Aug 2011 at 2:49

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 24 Jan 2013 at 1:53

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 5 Feb 2013 at 4:49

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 25 Mar 2013 at 7:33

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 10 Jun 2013 at 1:07

GoogleCodeExporter commented 9 years ago

Original comment by yan...@google.com on 26 Jul 2013 at 10:04

GoogleCodeExporter commented 9 years ago
Any news about this?

Thanks

Original comment by beto...@gmail.com on 8 May 2014 at 2:43