svn commit: r1634481 - /ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/lang/JSON.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r1634481 - /ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/lang/JSON.java

adrianc
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