Move all JSON converters to a separate class, change the collection
converters that used to use JSON back to calling toString, and implement separate JSON conversion. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@910112 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
org.ofbiz.base.conversion.BooleanConverters
|
||||
org.ofbiz.base.conversion.CollectionConverters
|
||||
org.ofbiz.base.conversion.DateTimeConverters
|
||||
org.ofbiz.base.conversion.JSONConverters
|
||||
org.ofbiz.base.conversion.MiscConverters
|
||||
org.ofbiz.base.conversion.NetConverters
|
||||
org.ofbiz.base.conversion.NumberConverters
|
||||
|
||||
@@ -19,16 +19,13 @@
|
||||
package org.ofbiz.base.conversion;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.json.JSON;
|
||||
import org.ofbiz.base.json.JSONWriter;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
|
||||
import javolution.util.FastList;
|
||||
import javolution.util.FastSet;
|
||||
@@ -56,13 +53,7 @@ public class CollectionConverters implements ConverterLoader {
|
||||
}
|
||||
|
||||
public String convert(List<T> obj) throws ConversionException {
|
||||
StringWriter sw = new StringWriter();
|
||||
try {
|
||||
new JSONWriter(sw).write(obj);
|
||||
} catch (IOException e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
return sw.toString();
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -96,62 +87,51 @@ public class CollectionConverters implements ConverterLoader {
|
||||
}
|
||||
|
||||
public String convert(Map<K, V> obj) throws ConversionException {
|
||||
StringWriter sw = new StringWriter();
|
||||
try {
|
||||
new JSONWriter(sw).write(obj);
|
||||
} catch (IOException e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
return sw.toString();
|
||||
return obj.toString();
|
||||
}
|
||||
}
|
||||
|
||||
public static class StringToList extends AbstractConverter<String, List<Object>> {
|
||||
public static class StringToList extends AbstractConverter<String, List<String>> {
|
||||
public StringToList() {
|
||||
super(String.class, List.class);
|
||||
}
|
||||
|
||||
public List<Object> convert(String obj) throws ConversionException {
|
||||
try {
|
||||
return new JSON(new StringReader(obj)).JSONArray();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
public List<String> convert(String obj) throws ConversionException {
|
||||
if (obj.startsWith("[") && obj.endsWith("]")) {
|
||||
return StringUtil.toList(obj);
|
||||
} else {
|
||||
List<String> tempList = FastList.newInstance();
|
||||
tempList.add(obj);
|
||||
return tempList;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class StringToMap extends AbstractConverter<String, Map<String, Object>> {
|
||||
public static class StringToMap extends AbstractConverter<String, Map<String, String>> {
|
||||
public StringToMap() {
|
||||
super(String.class, Map.class);
|
||||
}
|
||||
|
||||
public Map<String, Object> convert(String obj) throws ConversionException {
|
||||
try {
|
||||
return new JSON(new StringReader(obj)).JSONObject();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
public Map<String, String> convert(String obj) throws ConversionException {
|
||||
if (obj.startsWith("{") && obj.endsWith("}")) {
|
||||
return StringUtil.toMap(obj);
|
||||
}
|
||||
throw new ConversionException("Could not convert " + obj + " to Map: ");
|
||||
}
|
||||
}
|
||||
|
||||
public static class StringToSet extends AbstractConverter<String, Set<Object>> {
|
||||
public static class StringToSet extends AbstractConverter<String, Set<String>> {
|
||||
public StringToSet() {
|
||||
super(String.class, Set.class);
|
||||
}
|
||||
|
||||
public Set<Object> convert(String obj) throws ConversionException {
|
||||
try {
|
||||
Set<Object> set = FastSet.newInstance();
|
||||
set.addAll(new JSON(new StringReader(obj)).JSONArray());
|
||||
return set;
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
public Set<String> convert(String obj) throws ConversionException {
|
||||
if (obj.startsWith("[") && obj.endsWith("]")) {
|
||||
return StringUtil.toSet(obj);
|
||||
} else {
|
||||
Set<String> tempSet = FastSet.newInstance();
|
||||
tempSet.add(obj);
|
||||
return tempSet;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,228 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.base.conversion;
|
||||
|
||||
import java.io.StringReader;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.base.json.JSON;
|
||||
import org.ofbiz.base.json.JSONWriter;
|
||||
|
||||
import javolution.util.FastList;
|
||||
import javolution.util.FastSet;
|
||||
|
||||
/** JSON Converter classes. */
|
||||
public class JSONConverters implements ConverterLoader {
|
||||
public static class ObjectToJSONResultCreator<R extends JSONResult> implements ConverterCreater, ConverterLoader {
|
||||
public void loadConverters() {
|
||||
Converters.registerCreater(this);
|
||||
}
|
||||
|
||||
public <S, T> Converter<S, T> createConverter(Class<S> sourceClass, Class<T> targetClass) {
|
||||
if (!JSONResult.class.isAssignableFrom(targetClass)) {
|
||||
return null;
|
||||
}
|
||||
if (Collection.class.isAssignableFrom(sourceClass)) {
|
||||
} else if (Map.class.isAssignableFrom(sourceClass)) {
|
||||
} else if (Byte.class == sourceClass) {
|
||||
} else if (Character.class == sourceClass) {
|
||||
} else if (Double.class == sourceClass) {
|
||||
} else if (Float.class == sourceClass) {
|
||||
} else if (Integer.class == sourceClass) {
|
||||
} else if (Long.class == sourceClass) {
|
||||
} else if (Short.class == sourceClass) {
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
return UtilGenerics.cast(new ObjectToJSONWriterResult<S, JSONResult>(sourceClass, UtilGenerics.<Class<JSONResult>>cast(targetClass)));
|
||||
}
|
||||
}
|
||||
|
||||
private static class ObjectToJSONWriterResult<S, T extends JSONResult> extends AbstractConverter<S, T> {
|
||||
public ObjectToJSONWriterResult(Class<S> sourceClass, Class<T> targetClass) {
|
||||
super(sourceClass, targetClass);
|
||||
}
|
||||
|
||||
public T convert(S obj) throws ConversionException {
|
||||
try {
|
||||
T result = UtilGenerics.<T>cast(getTargetClass().newInstance());
|
||||
result.getWriter().write(obj);
|
||||
return result;
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToByte extends AbstractConverter<JSON, Byte> {
|
||||
public JSONToByte() {
|
||||
super(JSON.class, Byte.class);
|
||||
}
|
||||
|
||||
public Byte convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONLong().byteValue();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToDouble extends AbstractConverter<JSON, Double> {
|
||||
public JSONToDouble() {
|
||||
super(JSON.class, Double.class);
|
||||
}
|
||||
|
||||
public Double convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONFloat();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToFloat extends AbstractConverter<JSON, Float> {
|
||||
public JSONToFloat() {
|
||||
super(JSON.class, Float.class);
|
||||
}
|
||||
|
||||
public Float convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONFloat().floatValue();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToInteger extends AbstractConverter<JSON, Integer> {
|
||||
public JSONToInteger() {
|
||||
super(JSON.class, Integer.class);
|
||||
}
|
||||
|
||||
public Integer convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONLong().intValue();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToList extends AbstractConverter<JSON, List<Object>> {
|
||||
public JSONToList() {
|
||||
super(JSON.class, List.class);
|
||||
}
|
||||
|
||||
public List<Object> convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONArray();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToLong extends AbstractConverter<JSON, Long> {
|
||||
public JSONToLong() {
|
||||
super(JSON.class, Long.class);
|
||||
}
|
||||
|
||||
public Long convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONLong();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToMap extends AbstractConverter<JSON, Map<String, Object>> {
|
||||
public JSONToMap() {
|
||||
super(JSON.class, Map.class);
|
||||
}
|
||||
|
||||
public Map<String, Object> convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONObject();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToShort extends AbstractConverter<JSON, Short> {
|
||||
public JSONToShort() {
|
||||
super(JSON.class, Short.class);
|
||||
}
|
||||
|
||||
public Short convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
return obj.JSONLong().shortValue();
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class JSONToSet extends AbstractConverter<JSON, Set<Object>> {
|
||||
public JSONToSet() {
|
||||
super(JSON.class, Set.class);
|
||||
}
|
||||
|
||||
public Set<Object> convert(JSON obj) throws ConversionException {
|
||||
try {
|
||||
Set<Object> set = FastSet.newInstance();
|
||||
set.addAll(obj.JSONArray());
|
||||
return set;
|
||||
} catch (RuntimeException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new ConversionException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void loadConverters() {
|
||||
Converters.loadContainedConverters(JSONConverters.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.base.conversion;
|
||||
|
||||
import java.io.StringWriter;
|
||||
|
||||
import org.ofbiz.base.json.JSONWriter;
|
||||
|
||||
public interface JSONResult {
|
||||
JSONWriter getWriter();
|
||||
String getResult();
|
||||
|
||||
public static final class Indenting implements JSONResult {
|
||||
private final StringWriter sw = new StringWriter();
|
||||
private final JSONWriter writer = new JSONWriter(sw);
|
||||
|
||||
public JSONWriter getWriter() {
|
||||
return writer;
|
||||
}
|
||||
|
||||
public String getResult() {
|
||||
return sw.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,7 @@ import javolution.util.FastMap;
|
||||
import org.ofbiz.base.concurrent.TTLObject;
|
||||
import org.ofbiz.base.conversion.Converter;
|
||||
import org.ofbiz.base.conversion.Converters;
|
||||
import org.ofbiz.base.conversion.JSONResult;
|
||||
import org.ofbiz.base.test.GenericTestCaseBase;
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.base.util.UtilMisc;
|
||||
@@ -45,15 +46,15 @@ public class MiscTests extends GenericTestCaseBase {
|
||||
super(name);
|
||||
}
|
||||
|
||||
private static <S, T> void assertConversion(String label, String wanted, Class<T> targetClass, Object source, Class<S> sourceClass) throws Exception {
|
||||
private static <S, T extends JSONResult.Indenting> void assertConversion(String label, String wanted, Class<T> targetClass, Object source, Class<S> sourceClass) throws Exception {
|
||||
Converter<S, T> converter = Converters.getConverter(sourceClass, targetClass);
|
||||
assertTrue(label + " can convert", converter.canConvert(sourceClass, targetClass));
|
||||
assertEquals(label, wanted, converter.convert(UtilGenerics.<S>cast(source)));
|
||||
assertEquals(label, wanted, converter.convert(UtilGenerics.<S>cast(source)).getResult());
|
||||
}
|
||||
|
||||
public void testExtendsImplements() throws Exception {
|
||||
List<String> arraysList = Arrays.asList("a", "b", "c");
|
||||
assertConversion("", "[\n \"a\",\n \"b\",\n \"c\"\n]", String.class, arraysList, arraysList.getClass());
|
||||
assertConversion("", "[\n \"a\",\n \"b\",\n \"c\"\n]", JSONResult.Indenting.class, arraysList, arraysList.getClass());
|
||||
Exception caught = null;
|
||||
try {
|
||||
Converters.getConverter(MiscTests.class, String.class);
|
||||
@@ -64,7 +65,7 @@ public class MiscTests extends GenericTestCaseBase {
|
||||
}
|
||||
LRUMap<String, String> map = new LRUMap<String, String>();
|
||||
map.put("a", "1");
|
||||
assertConversion("", "{\n \"a\": \"1\"\n}", String.class, map, LRUMap.class);
|
||||
assertConversion("", "{\n \"a\": \"1\"\n}", JSONResult.Indenting.class, map, LRUMap.class);
|
||||
}
|
||||
|
||||
public static <S> void assertPassThru(Object wanted, Class<S> sourceClass) throws Exception {
|
||||
|
||||
@@ -19,6 +19,7 @@
|
||||
package org.ofbiz.base.json;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
Reference in New Issue
Block a user