No, because that would expose Jackson to the rest of the framework.
There is a TODO in JSON.java to find a generic way to include those
features.
So, if you can think of a way to add a generic method to JSON.java that
will configure Jackson, that would be great.
Something like JSON.setDateFormat(String format)...
Adrian Crum
Sandglass Software
www.sandglass-software.com
On 11/6/2014 11:35 AM, Chatree Srichart wrote:
Hi Adrian,
Would it be better if you make "mapper" property to be public?
Now, I cannot not change date format in JSON string even though Jackson
allow to do.
http://wiki.fasterxml.com/JacksonFAQDateHandling
For example, I want to change:
createdTxStamp: 1386071477388
to:
createdTxStamp: "2013-12-03"
Best Regards,
Chatree Srichart
On Mon, Oct 27, 2014 at 4:06 PM, <adri...@apache.org> wrote:
Author: adrianc
Date: Mon Oct 27 09:06:59 2014
New Revision: 1634481
URL: http://svn.apache.org/r1634481
Log:
Integrate JSON.java with the Jackson library.
Modified:
ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/lang/JSON.java
Modified:
ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/lang/JSON.java
URL:
http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/lang/JSON.java?rev=1634481&r1=1634480&r2=1634481&view=diff
==============================================================================
---
ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/lang/JSON.java
(original)
+++
ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/lang/JSON.java
Mon Oct 27 09:06:59 2014
@@ -23,23 +23,30 @@ import java.io.InputStream;
import java.io.Reader;
import org.apache.commons.io.IOUtils;
-import org.ofbiz.base.lang.ThreadSafe;
import org.ofbiz.base.util.Assert;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
/** A JSON object. */
@ThreadSafe
public final class JSON {
+ // TODO: Find a generic way to modify mapper options
+ private static final ObjectMapper mapper = new ObjectMapper();
+
public static JSON from(InputStream inStream) throws IOException {
Assert.notNull("inStream", inStream);
String jsonString = IOUtils.toString(inStream, "UTF-8");
return from(jsonString);
}
- public static JSON from(Object object) {
+ public static JSON from(Object object) throws IOException {
Assert.notNull("object", object);
- // TODO: Finish implementation.
- return null;
+ try {
+ return from(mapper.writeValueAsString(object));
+ } catch (Exception e) {
+ throw new IOException(e);
+ }
}
public static JSON from(Reader reader) throws IOException {
@@ -70,9 +77,14 @@ public final class JSON {
return jsonString.hashCode();
}
- public <T> T toObject() {
- // TODO: Finish implementation.
- return null;
+ public <T> T toObject(Class<T> targetClass) throws IOException {
+ try {
+ return mapper.readValue(jsonString, targetClass);
+ } catch (IOException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new IOException(e);
+ }
}
@Override