Every programmer has their own design style. This would be mine:

class JsonString {
  public String toString() {
    ...
  }
}

public static class ListToJsonString<T> extends AbstractConverter<List<T>, 
JsonString> {
  public ListToJsonString() {
    super(List.class, JsonString .class);
  }
  ...
}

The problem I have with your approach is the fact that there is no way to know 
that converting object x to a String will result in a JSON string. In addition, 
I was hoping we could stick to this pattern: Converting any Java type to a 
String is the same as calling the object's toString() method.

-Adrian


--- On Wed, 2/10/10, [email protected] <[email protected]> wrote:

> From: [email protected] <[email protected]>
> Subject: svn commit: r908713 - in 
> /ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion: 
> CollectionConverters.java test/MiscTests.java
> To: [email protected]
> Date: Wednesday, February 10, 2010, 2:48 PM
> Author: doogie
> Date: Wed Feb 10 22:48:07 2010
> New Revision: 908713
> 
> URL: http://svn.apache.org/viewvc?rev=908713&view=rev
> Log:
> Switch the collection/string converters to the json
> parser.
> 
> Modified:
>    
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java
>    
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java
> 
> Modified:
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java
> URL: 
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java?rev=908713&r1=908712&r2=908713&view=diff
> ==============================================================================
> ---
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java
> (original)
> +++
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/CollectionConverters.java
> Wed Feb 10 22:48:07 2010
> @@ -18,13 +18,17 @@
>  
> *******************************************************************************/
>  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.util.StringUtil;
> +import org.ofbiz.base.json.JSON;
> +import org.ofbiz.base.json.JSONWriter;
>  
>  import javolution.util.FastList;
>  import javolution.util.FastSet;
> @@ -52,7 +56,13 @@
>          }
>  
>          public String
> convert(List<T> obj) throws ConversionException {
> -            return
> obj.toString();
> +            StringWriter sw
> = new StringWriter();
> +            try {
> +               
> new JSONWriter(sw).write(obj);
> +            } catch
> (IOException e) {
> +               
> throw new ConversionException(e);
> +            }
> +            return
> sw.toString();
>          }
>      }
>  
> @@ -86,51 +96,62 @@
>          }
>  
>          public String
> convert(Map<K, V> obj) throws ConversionException {
> -            return
> obj.toString();
> +            StringWriter sw
> = new StringWriter();
> +            try {
> +               
> new JSONWriter(sw).write(obj);
> +            } catch
> (IOException e) {
> +               
> throw new ConversionException(e);
> +            }
> +            return
> sw.toString();
>          }
>      }
>  
> -    public static class StringToList extends
> AbstractConverter<String, List<String>> {
> +    public static class StringToList extends
> AbstractConverter<String, List<Object>> {
>          public
> StringToList() {
>          
>    super(String.class, List.class);
>          }
>  
> -        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 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 static class StringToMap extends
> AbstractConverter<String, Map<String, String>>
> {
> +    public static class StringToMap extends
> AbstractConverter<String, Map<String, Object>>
> {
>          public StringToMap()
> {
>          
>    super(String.class, Map.class);
>          }
>  
> -        public Map<String,
> String> convert(String obj) throws ConversionException {
> -            if
> (obj.startsWith("{") && obj.endsWith("}")) {
> -               
> return StringUtil.toMap(obj);
> +        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);
>              }
> -            throw new
> ConversionException("Could not convert " + obj + " to Map:
> ");
>          }
>      }
>  
> -    public static class StringToSet extends
> AbstractConverter<String, Set<String>> {
> +    public static class StringToSet extends
> AbstractConverter<String, Set<Object>> {
>          public StringToSet()
> {
>          
>    super(String.class, Set.class);
>          }
>  
> -        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;
> +        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);
>              }
>          }
>      }
> 
> Modified:
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java
> URL: 
> http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java?rev=908713&r1=908712&r2=908713&view=diff
> ==============================================================================
> ---
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java
> (original)
> +++
> ofbiz/trunk/framework/base/src/org/ofbiz/base/conversion/test/MiscTests.java
> Wed Feb 10 22:48:07 2010
> @@ -47,7 +47,7 @@
>      public void
> testExtendsImplements() throws Exception {
>          List<String>
> arraysList = Arrays.asList("a", "b", "c");
>          Converter converter
> = Converters.getConverter(arraysList.getClass(),
> String.class);
> -        assertEquals("", "[a, b, c]",
> converter.convert(arraysList));
> +        assertEquals("", "[\n \"a\",\n
> \"b\",\n \"c\"\n]", converter.convert(arraysList));
>          Exception caught =
> null;
>          try {
>          
>    Converters.getConverter(MiscTests.class,
> String.class);
> @@ -59,7 +59,7 @@
>          LRUMap<String,
> String> map = new LRUMap<String, String>();
>          map.put("a", "1");
>          converter =
> Converters.getConverter(LRUMap.class, String.class);
> -        assertEquals("", "{a=1}",
> converter.convert(map));
> +        assertEquals("", "{\n \"a\":
> \"1\"\n}", converter.convert(map));
>      }
>  
>      public void testPassthru() throws
> Exception {
> 
> 
> 



Reply via email to