Closed leolux closed 8 years ago
To answer my first question: The plug-in mechansim can be used by overriding the method make(Object) instead of writing a new factory. For example:
public class MyJsonFactory extends Json.DefaultFactor
@Override
public Json make(Object anything) {
if(anything instanceof Date){
String dateFormatAsString = ISO_DATETIME_FORMAT.format((Date)anything);
return string(dateFormatAsString);
}
return super.make(anything);
}
But I haven't found a solution for my second question (mapping from json to java).
There is no general mapping between Json to an arbitrary Java object. This problem is external to JSON. I've been ruminating on doing an generic Java<->Json mapper as an extension, a separate project, but I haven't really seen much need for that.
Ok, thanks! I totally agree with you. The result of mapping from Json to Java does not need to be the same type of Java object used to create a Json value because the set of possible java classes is much larger than the finite set of Json values.
However the plug-in mechanism available to customize the Java<->Json mapping works nicely. I haven't found any other Json library which is as compact and neat as this one.
As stated in the javadoc of the interface Json.Factory the method make(Object) allows me to plug-in my own mapping of java.util.Date to Json. But how to achive this without reimplementing the whole method?
Futhermore I would like to know if it is possible to globally map the other way around from a Json string to java.util.Date. I would have expected something like Json.asDate() or Json.asType(Class type) for the conversion to Java instances.