You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by ad...@apache.org on 2014/10/28 16:15:18 UTC
svn commit: r1634892 - in
/ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion:
JSONConverters.java test/TestJSONConverters.java
Author: adrianc
Date: Tue Oct 28 15:15:17 2014
New Revision: 1634892
URL: http://svn.apache.org/r1634892
Log:
Integrate JSON.java with the converter framework.
Modified:
ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java
ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/test/TestJSONConverters.java
Modified: ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java?rev=1634892&r1=1634891&r2=1634892&view=diff
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java (original)
+++ ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java Tue Oct 28 15:15:17 2014
@@ -18,14 +18,12 @@
*******************************************************************************/
package org.ofbiz.base.conversion;
+import java.io.IOException;
import java.util.List;
import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-import net.sf.json.JSON;
-import net.sf.json.JSONArray;
-import net.sf.json.JSONObject;
+import org.ofbiz.base.lang.JSON;
+import org.ofbiz.base.util.UtilGenerics;
/** JSON Converter classes. */
public class JSONConverters implements ConverterLoader {
@@ -37,10 +35,8 @@ public class JSONConverters implements C
public List<Object> convert(JSON obj) throws ConversionException {
try {
- return (List<Object>) JSONArray.toCollection((JSONArray)obj);
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
+ return UtilGenerics.<List<Object>>cast(obj.toObject(List.class));
+ } catch (IOException e) {
throw new ConversionException(e);
}
}
@@ -53,26 +49,8 @@ public class JSONConverters implements C
public Map<String, Object> convert(JSON obj) throws ConversionException {
try {
- return (Map<String, Object>) JSONObject.toBean((JSONObject)obj, Map.class);
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
- throw new ConversionException(e);
- }
- }
- }
-
- public static class MapToJSON extends AbstractConverter<Map<String, Object>, JSON> {
- public MapToJSON() {
- super(Map.class, JSON.class);
- }
-
- public JSON convert(Map<String, Object> obj) throws ConversionException {
- try {
- return JSONObject.fromObject(obj);
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
+ return UtilGenerics.<Map<String, Object>>cast(obj.toObject(Map.class));
+ } catch (IOException e) {
throw new ConversionException(e);
}
}
@@ -85,28 +63,22 @@ public class JSONConverters implements C
public JSON convert(List<Object> obj) throws ConversionException {
try {
- return JSONArray.fromObject(obj);
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
+ return JSON.from(obj);
+ } catch (IOException e) {
throw new ConversionException(e);
}
}
}
- public static class JSONToSet extends AbstractConverter<JSON, Set<Object>> {
- public JSONToSet() {
- super(JSON.class, Set.class);
+ public static class MapToJSON extends AbstractConverter<Map<String, Object>, JSON> {
+ public MapToJSON() {
+ super(Map.class, JSON.class);
}
- public Set<Object> convert(JSON obj) throws ConversionException {
+ public JSON convert(Map<String, Object> obj) throws ConversionException {
try {
- Set<Object> set = new TreeSet<Object>();
- set.addAll((JSONArray)obj);
- return set;
- } catch (RuntimeException e) {
- throw e;
- } catch (Exception e) {
+ return JSON.from(obj);
+ } catch (IOException e) {
throw new ConversionException(e);
}
}
Modified: ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/test/TestJSONConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/test/TestJSONConverters.java?rev=1634892&r1=1634891&r2=1634892&view=diff
==============================================================================
--- ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/test/TestJSONConverters.java (original)
+++ ofbiz/branches/json-integration-refactoring/framework/base/src/org/ofbiz/base/conversion/test/TestJSONConverters.java Tue Oct 28 15:15:17 2014
@@ -24,18 +24,14 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
-import java.util.Set;
-import java.util.TreeSet;
-import net.sf.json.JSONArray;
-import net.sf.json.JSONObject;
-import org.ofbiz.base.conversion.JSONConverters;
+import junit.framework.TestCase;
+
import org.ofbiz.base.conversion.Converter;
import org.ofbiz.base.conversion.ConverterLoader;
import org.ofbiz.base.conversion.Converters;
-
-import net.sf.json.JSON;
-import junit.framework.TestCase;
+import org.ofbiz.base.conversion.JSONConverters;
+import org.ofbiz.base.lang.JSON;
public class TestJSONConverters extends TestCase {
public TestJSONConverters(String name) {
@@ -46,39 +42,25 @@ public class TestJSONConverters extends
public void testJSONToMap() throws Exception {
Converter<JSON, Map> converter = Converters.getConverter(JSON.class, Map.class);
- JSON json;
Map map, convertedMap;
map = new HashMap();
map.put("field1", "value1");
- json = JSONObject.fromObject(map);
+ JSON json = JSON.from(map);
convertedMap = converter.convert(json);
assertEquals("JSON to Map", map, convertedMap);
}
public void testJSONToList() throws Exception {
Converter<JSON, List> converter = Converters.getConverter(JSON.class, List.class);
- JSON json;
List list, convertedList;
list = new ArrayList();
list.add("field1");
list.add("field2");
- json = JSONArray.fromObject(list);
+ JSON json = JSON.from(list);
convertedList = converter.convert(json);
assertEquals("JSON to List", list, convertedList);
}
- public void testJSONToSet() throws Exception {
- Converter<JSON, Set> converter = Converters.getConverter(JSON.class, Set.class);
- JSON json;
- Set set, convertedSet;
- set = new TreeSet();
- set.add("field1");
- set.add("field2");
- json = JSONArray.fromObject(set);
- convertedSet = converter.convert(json);
- assertEquals("JSON to Set", set, convertedSet);
- }
-
public void testMapToJSON() throws Exception {
Converter<Map, JSON> converter = Converters.getConverter(Map.class, JSON.class);
JSON json;