Type conversion to and from a String
is a very common application problem.
To aid in the process, I've created a small library - Joda-Convert.
Joda-Convert
There are many libraries designed for converting from one object to another, so why create another.
Well, partly because I wanted something focussed on the task of Object
to String
rather than the larger problem of Object
to Object
.
See this list (bottom of the page) for other projects.
The second reason I wanted to write Joda-Convert was to experiment with annotation based conversion.
This is an extension of the principles in JSR-311 (RESTful API) where type conversion is performed by
looking for a valueOf
static method or a constructor that takes a String
.
While this approach is very useful, it doesn't support some of my projects like JSR-310, where the static method
is named of
or parse
.
This leads into the third reason.
If the annotation based approach is successful, then Joda-Convert is ideally placed to be moved into the JDK.
It would be wonderful to see the JDK classes annotated to indicate which methods should be used to convert
to and from String
.
Using Joda-Convert is easy:
// conversion to String String str = StringConvert.INSTANCE.convertToString(foo); // conversion from String Foo bar = StringConvert.INSTANCE.convertFromString(Foo.class, str);
There are built in converters for many JDK types.
It is also possible to create an instance of StringConvert
to allow application converters to be registered.
When searching for a converter, if one is not already registered, then the system will search the class for annotations:
public class Distance { @FromString public static Distance parse(String str) { ... } @ToString public String getStandardOutput() { ... } }
As can be seen, adding the annotations to a class is very simple.
The Joda-Convert system will parse the class, find the annotations and create a suitable converter that calls the
annotated methods by reflection.
The key point is that they can be used on any method, rather than the convention approach used by JSR-311.
Consider a class like TimeZone
to understand why this is important.
One little known point about annotations is that the annotation class is not needed at runtime. This means that a library like Commons-Lang could add the annotations to their Mutable* classes without actually adding a runtime dependency. If a user chose to not use Joda-Convert then there would be no impact. If a user did choose to use Joda-Convert, then the conversion would just work.
If I can release Joda-Convert soon, then I will add the annotations to Joda-Time and Joda-Money. Hopefully other projects will consider the idea too! (If you're interested, let me know, as it gives an incentive to get the project to v1.0)
Summary
Joda-Convert is currently an untested Alpha that exists to test
out the annotation idea (although as far as I know it does work).
I'd welcome participants that want to help test and finish the JDK conversions.
I'd also like to see 'helper objects' added, so a DateTimeFormatter
can be used to
help convert a DateTime
, or a ClassLoader
to convert a Class
.
Feedback welcome as always!