From 5da12fa6cd7c902efbb87fb4e47a36294bece09f Mon Sep 17 00:00:00 2001 From: Adam Heath Date: Sun, 14 Feb 2010 22:44:39 +0000 Subject: [PATCH] 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 --- .../org.ofbiz.base.conversion.ConverterLoader | 1 + .../base/conversion/CollectionConverters.java | 68 ++---- .../ofbiz/base/conversion/JSONConverters.java | 228 ++++++++++++++++++ .../org/ofbiz/base/conversion/JSONResult.java | 42 ++++ .../ofbiz/base/conversion/test/MiscTests.java | 9 +- .../src/org/ofbiz/base/json/JSONWriter.java | 1 + 6 files changed, 301 insertions(+), 48 deletions(-) create mode 100644 framework/base/src/org/ofbiz/base/conversion/JSONConverters.java create mode 100644 framework/base/src/org/ofbiz/base/conversion/JSONResult.java diff --git a/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader b/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader index 8551e65fc1..3a19a683d8 100644 --- a/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader +++ b/framework/base/src/META-INF/services/org.ofbiz.base.conversion.ConverterLoader @@ -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 diff --git a/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java b/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java index 2b35a8250c..6d13bef7d6 100644 --- a/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java +++ b/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java @@ -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 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 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> { + public static class StringToList extends AbstractConverter> { public StringToList() { super(String.class, List.class); } - public List 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 convert(String obj) throws ConversionException { + if (obj.startsWith("[") && obj.endsWith("]")) { + return StringUtil.toList(obj); + } else { + List tempList = FastList.newInstance(); + tempList.add(obj); + return tempList; } } } - public static class StringToMap extends AbstractConverter> { + public static class StringToMap extends AbstractConverter> { public StringToMap() { super(String.class, Map.class); } - public Map 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 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> { + public static class StringToSet extends AbstractConverter> { public StringToSet() { super(String.class, Set.class); } - public Set convert(String obj) throws ConversionException { - try { - Set 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 convert(String obj) throws ConversionException { + if (obj.startsWith("[") && obj.endsWith("]")) { + return StringUtil.toSet(obj); + } else { + Set tempSet = FastSet.newInstance(); + tempSet.add(obj); + return tempSet; } } } diff --git a/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java b/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java new file mode 100644 index 0000000000..660a3bd5bb --- /dev/null +++ b/framework/base/src/org/ofbiz/base/conversion/JSONConverters.java @@ -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 implements ConverterCreater, ConverterLoader { + public void loadConverters() { + Converters.registerCreater(this); + } + + public Converter createConverter(Class sourceClass, Class 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(sourceClass, UtilGenerics.>cast(targetClass))); + } + } + + private static class ObjectToJSONWriterResult extends AbstractConverter { + public ObjectToJSONWriterResult(Class sourceClass, Class targetClass) { + super(sourceClass, targetClass); + } + + public T convert(S obj) throws ConversionException { + try { + T result = UtilGenerics.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 { + 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 { + 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 { + 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 { + 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> { + public JSONToList() { + super(JSON.class, List.class); + } + + public List 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 { + 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> { + public JSONToMap() { + super(JSON.class, Map.class); + } + + public Map 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 { + 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> { + public JSONToSet() { + super(JSON.class, Set.class); + } + + public Set convert(JSON obj) throws ConversionException { + try { + Set 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); + } +} diff --git a/framework/base/src/org/ofbiz/base/conversion/JSONResult.java b/framework/base/src/org/ofbiz/base/conversion/JSONResult.java new file mode 100644 index 0000000000..c310dbc108 --- /dev/null +++ b/framework/base/src/org/ofbiz/base/conversion/JSONResult.java @@ -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(); + } + } +} + diff --git a/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java b/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java index 356148e6be..2e3ffde7e6 100644 --- a/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java +++ b/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java @@ -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 void assertConversion(String label, String wanted, Class targetClass, Object source, Class sourceClass) throws Exception { + private static void assertConversion(String label, String wanted, Class targetClass, Object source, Class sourceClass) throws Exception { Converter converter = Converters.getConverter(sourceClass, targetClass); assertTrue(label + " can convert", converter.canConvert(sourceClass, targetClass)); - assertEquals(label, wanted, converter.convert(UtilGenerics.cast(source))); + assertEquals(label, wanted, converter.convert(UtilGenerics.cast(source)).getResult()); } public void testExtendsImplements() throws Exception { List 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 map = new LRUMap(); 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 void assertPassThru(Object wanted, Class sourceClass) throws Exception { diff --git a/framework/base/src/org/ofbiz/base/json/JSONWriter.java b/framework/base/src/org/ofbiz/base/json/JSONWriter.java index 31e743276d..52c1af8914 100644 --- a/framework/base/src/org/ofbiz/base/json/JSONWriter.java +++ b/framework/base/src/org/ofbiz/base/json/JSONWriter.java @@ -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;