Corn Converter


    Converter module designed to offer converters from Java types  to various targets and vice versa. In the first release, the module only offers Java to JSON converters. It's published in the maven central repository, so you can use corn-cps by adding the dependency below. 

pom.xml

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-converter</artifactId>
    <version>${corn-converter.version}</version>
</dependency>

JSON

    Converter module offers JSON type implementations, converters and parsers. It's small, simple, extensible and fast implementation. 

Json Type Classes
  • JsTypeObject:
            This class represents a JSON object in java. This is the abstract base implementation, so all json type classes are extend from this class. It offers type checking methods as isSimple, isComplex, isList, isNull and isArray.. Json type classes keep the java class reference within them to simplfy conversion process. 
  • JsTypeSimple:
            Represents an object which has a single value in it. All java primitives and String class converted to this type. 

String strVal = "Test";
JsTypeSimple jsObj = new JsTypeSimple(strVal);
System.out.println(jsObj.toJsonString());
  • JsTypeNull:
            This class represents the null value in Json. It has no constructor and offers a static field to access null value.

JsTypeObject jsObj = JsTypeNull.NULL;
assertTrue(jsObj.isNull());
  • JsTypeComplex:
            Represents a complex json type which can contain multiple attributes that can be any json object type including complex and list. Java complex types such as beans and map types are converter to this type.

JsTypeComplex jsObj = new JsTypeComplex();
jsObj.put("attribute1", new JsTypeSimple("Deneme"));
jsObj.put("attribute2", new JsTypeSimple(1234.4f));
assertFalse(jsObj.isList());
assertFalse(jsObj.isNull());
assertFalse(jsObj.isEmpty());
assertTrue(jsObj.isComplex());
System.out.println(jsObj.toJsonString());
  • JsTypeList:
            The list type is a container for arrays and list types.

Json Conversion

    JsonConverter class is a generic type converter that supports almost all standard java types including primitives, beans, collections and arrays. It's usage is so simple and below are some samples: 

Simple Type:

        Long val = Long.MAX_VALUE;
        JsTypeObject jsObj = converter.toTargetType(val);
        Object obj = converter.toJava(jsObj);
        assertEquals(val, obj);

Map Type:

        Map<String,String> val = new HashMap<String,String>();
        val.put("key1","item1");
        val.put("key2","item2");
        JsTypeObject jsObj = converter.toTargetType(val);
        assertEquals(val.getClass().getName(),jsObj.getJavaClassName());
        Object obj = converter.toJava(jsObj);
        assertEquals(val, obj);
        jsObj =  converter.toTargetType((java.util.Map<String,String>)val);
        assertEquals(val.getClass().getName(),jsObj.getJavaClassName());
        obj = converter.toJava(jsObj);
        assertEquals(val, obj);

Bean:

        TestTypeBeanA val = new TestTypeBeanA();
        val.assignValues();
        val.setIntBeanField(3);
        val.setShortBeanField((short) 5);
        val.setSimpleBeanBeanField(new TestTypeSimpleBean());
        val.setStrBeanField("a string");
        JsTypeObject jsObj = (JsTypeObject) converter.toTargetType(val);
        assertEquals(val.getClass().getName(),jsObj.getJavaClassName());
        Object obj = converter.toJava(jsObj);
        assertEquals(val, obj);

Array:

        String[] strArr = {"1","2"};
        JsTypeObject jsObj =  converter.toTargetType(strArr);
        Object obj = converter.toJava(jsObj);
        assertNotNull(obj);
        assertTrue(obj.getClass().isArray());
        assertEquals(Array.getLength(obj),strArr.length);
        assertEquals(Array.get(obj, 0),strArr[0] );
        assertEquals(Array.get(obj, 1),strArr[1] );

Collection:

        Set<String> val = new HashSet<String>();
        val.add("item1");
        val.add("item2");
        JsTypeObject jsObj = (JsTypeObject) converter.toTargetType(val);
        assertEquals(val.getClass().getName(),jsObj.getJavaClassName());
        Object obj = converter.toJava(jsObj);
        assertEquals(val, obj);
        jsObj = (JsTypeObject) converter.toTargetType((java.util.Set<String>)val);
        assertEquals(val.getClass().getName(),jsObj.getJavaClassName());
        obj = converter.toJava(jsObj);
        assertEquals(val, obj);

    Although the most java types are supported, some classes like classes that has no default or String parameter constructor not supported by default. Since the module is extensible, one can easily write private converters and introduce them to JsonConverter at runtime either using its registerConverter method or put converter class names in jsonConerters.properties file in the classpath root. 

        TestTypeNotSupportedBeanA bean = new TestTypeNotSupportedBeanA(3);
        JsonConverter cnv = new JsonConverter();
        JsTypeObject jsObj = null;
        try {
            jsObj = cnv.toTargetType(bean);
        } catch (ConversionException e) {
        }
        assertNull(jsObj);
        JsonConverter.registerConverter(CnvTestTypeNotSupportedBeanA.class);
        try {
            jsObj = cnv.toTargetType(bean);
        } catch (ConversionException e) {
        }
        assertNotNull(jsObj);

Json Parser

    The last thing that one may need is how to parse Json text values to java Json types, so it can be converted to Java. The module offers JsonStringParser.parseJsonString method to convert text value to Json object type.

        Map<String,String> val = new HashMap<String,String>();
        val.put("key1","item1");
        val.put("key2","item2");
        JsTypeObject jsObj = (JsTypeObject) converter.toTargetType(val);
        assertEquals(val.getClass().getName(),jsObj.getJavaClassName());
        JsTypeObject obj =  JsonStringParser.parseJsonString(jsObj.toJsonString());
        assertEquals(obj, jsObj);JsonConverter.registerConverter(CnvTestTypeNotSupportedBeanA.class);
        try {
            jsObj = cnv.toTargetType(bean);
        } catch (ConversionException e) {
        }
        assertNotNull(jsObj);