Move OSAL and added array support to JSONSerializers

This commit is contained in:
Ziver Koc 2013-08-01 13:21:25 +00:00
parent 86d463be88
commit fe66cc9962
7 changed files with 181 additions and 52 deletions

78
src/zutil/ClassUtil.java Normal file
View file

@ -0,0 +1,78 @@
/*
* Copyright (c) 2013 ezivkoc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil;
import java.util.HashSet;
/**
* This class include some utility functions for classes
*
* User: Ziver
*/
public class ClassUtil {
/** A Set that contains possible wrapper objects for primitives **/
private static final HashSet<Class<?>> wrappers;
static {
wrappers = new HashSet<Class<?>>();
wrappers.add(Boolean.class);
wrappers.add(Character.class);
wrappers.add(Byte.class);
wrappers.add(Short.class);
wrappers.add(Integer.class);
wrappers.add(Long.class);
wrappers.add(Float.class);
wrappers.add(Double.class);
wrappers.add(Void.class);
}
/** A Set that contains possible primitives **/
private static final HashSet<Class<?>> primitives;
static {
primitives = new HashSet<Class<?>>();
primitives.add(boolean.class);
primitives.add(char.class);
primitives.add(byte.class);
primitives.add(short.class);
primitives.add(int.class);
primitives.add(long.class);
primitives.add(float.class);
primitives.add(double.class);
primitives.add(void.class);
primitives.add(String.class);
}
/**
* @return if the given class is a wrapper for a primitive
*/
public static boolean isWrapper(Class<?> type){
return wrappers.contains( type );
}
/**
* @return if the given class is a primitive including String
*/
public static boolean isPrimitive(Class<?> type){
return primitives.contains( type );
}
}

View file

@ -20,7 +20,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.zutil.osal; package zutil.osal;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;

View file

@ -20,7 +20,7 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.zutil.osal; package zutil.osal;
import java.io.File; import java.io.File;

View file

@ -20,16 +20,17 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
package zutil.zutil.osal; package zutil.osal;
import java.io.File; import java.io.File;
import java.io.IOException;
/** /**
* User: ezivkoc * User: ezivkoc
*/ */
public class OsalWindowsImpl extends OSAbstractionLayer { public class OsalWindowsImpl extends OSAbstractionLayer {
@Override @Override
public String getOSType() { public OSType getOSType() {
return null; //To change body of implemented methods use File | Settings | File Templates. return null; //To change body of implemented methods use File | Settings | File Templates.
} }
@ -45,7 +46,12 @@ public class OsalWindowsImpl extends OSAbstractionLayer {
@Override @Override
public String getKernelVersion() { public String getKernelVersion() {
try {
return runCommand("ver"); return runCommand("ver");
} catch (Exception e) {
e.printStackTrace();
}
return null;
} }
@Override @Override

View file

@ -22,6 +22,8 @@
package zutil.parser.json; package zutil.parser.json;
import sun.misc.ClassLoaderUtil;
import zutil.ClassUtil;
import zutil.parser.DataNode; import zutil.parser.DataNode;
import zutil.parser.DataNode.DataType; import zutil.parser.DataNode.DataType;
@ -100,10 +102,16 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
} }
protected DataNode getDataNode(Object obj) throws IOException, IllegalArgumentException, IllegalAccessException { protected DataNode getDataNode(Object obj) throws IOException, IllegalArgumentException, IllegalAccessException {
//if(!(obj instanceof Serializable)) DataNode root = null;
// throw new UnSerializable
DataNode root = new DataNode(DataNode.DataType.Map); // Check if the object is a primitive
if(ClassUtil.isPrimitive(obj.getClass()) ||
ClassUtil.isWrapper(obj.getClass())){
root = getPrimitiveDataNode(obj.getClass(), obj);
}
// Object is a complex data type
else {
root = new DataNode(DataNode.DataType.Map);
// Generate meta data // Generate meta data
if(generateMetaData){ if(generateMetaData){
// Cache // Cache
@ -122,17 +130,18 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
if((field.getModifiers() & Modifier.STATIC) == 0 && if((field.getModifiers() & Modifier.STATIC) == 0 &&
(field.getModifiers() & Modifier.TRANSIENT) == 0){ (field.getModifiers() & Modifier.TRANSIENT) == 0){
field.setAccessible(true); field.setAccessible(true);
// Add basic type (int, float...) // Add basic type (int, float...)
if(field.getType().isPrimitive() || if(ClassUtil.isPrimitive(field.getType()) ||
String.class.isAssignableFrom(field.getType())){ ClassUtil.isWrapper(field.getType())){
root.set(field.getName(), getPrimitiveDataNode(field, obj)); root.set(field.getName(), getPrimitiveDataNode(field.getType(), field.get(obj)));
} }
// Add an array // Add an array
else if(field.getType().isArray()){ else if(field.getType().isArray()){
DataNode arrayNode = new DataNode(DataNode.DataType.List); DataNode arrayNode = new DataNode(DataNode.DataType.List);
Object array = field.get(obj); Object array = field.get(obj);
for(int i=0; i< Array.getLength(array) ;i++){ for(int i=0; i< Array.getLength(array) ;i++){
arrayNode.add(Array.get(array, i).toString()); arrayNode.add(getDataNode(Array.get(array, i)));
} }
root.set(field.getName(), arrayNode); root.set(field.getName(), arrayNode);
} }
@ -147,11 +156,11 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
} }
} }
} }
}
return root; return root;
} }
private DataNode getPrimitiveDataNode(Field field, Object obj) throws UnsupportedDataTypeException, IllegalArgumentException, IllegalAccessException { private DataNode getPrimitiveDataNode(Class<?> type, Object value) throws UnsupportedDataTypeException, IllegalArgumentException, IllegalAccessException {
Class<?> type = field.getType();
DataNode node = null; DataNode node = null;
if (type == int.class || if (type == int.class ||
type == Integer.class || type == Integer.class ||
@ -172,7 +181,7 @@ public class JSONObjectOutputStream extends OutputStream implements ObjectOutput
else else
throw new UnsupportedDataTypeException("Unsupported primitive data type: "+type.getName()); throw new UnsupportedDataTypeException("Unsupported primitive data type: "+type.getName());
node.set(field.get(obj).toString()); node.set(value.toString());
return node; return node;
} }

View file

@ -128,7 +128,6 @@ public class JSONParser{
} }
// Check what type of type the data is // Check what type of type the data is
String data = tmp.toString(); String data = tmp.toString();
System.out.println("\""+data+"\"");
if( BOOLEAN_PATTERN.matcher(data).matches() ) if( BOOLEAN_PATTERN.matcher(data).matches() )
root = new DataNode(DataType.Boolean); root = new DataNode(DataType.Boolean);
else if( NUMBER_PATTERN.matcher(data).matches() ) else if( NUMBER_PATTERN.matcher(data).matches() )

View file

@ -25,6 +25,7 @@ package zutil.test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.io.*; import java.io.*;
import java.util.Arrays;
import org.junit.Test; import org.junit.Test;
@ -95,6 +96,27 @@ public class JSONSerializerTest{
assertEquals( sourceObj, targetObj ); assertEquals( sourceObj, targetObj );
} }
@Test
public void testOutputSerializerWithArrays() throws InterruptedException, IOException, ClassNotFoundException{
TestClassArray sourceObj = new TestClassArray().init();
String data = writeObjectToJson(sourceObj);
data = data.replace("\"", "'");
assertEquals(
"{'@class': 'zutil.test.JSONSerializerTest$TestClassArray', 'array': [1, 2, 3, 4], '@object_id': 1}",
data);
}
@Test
public void testInputSerializerWithArrays() throws InterruptedException, IOException, ClassNotFoundException{
TestClassArray sourceObj = new TestClassArray().init();
TestClassArray targetObj = sendReceiveObject(sourceObj);
assertEquals( sourceObj, targetObj );
}
@ -182,7 +204,22 @@ public class JSONSerializerTest{
} }
public boolean equals(Object obj){ public boolean equals(Object obj){
return obj instanceof TestObj && this.value == ((TestObj)obj).value; return obj instanceof TestObj &&
this.value == ((TestObj)obj).value;
}
}
public static class TestClassArray{
private int[] array;
public TestClassArray init(){
array = new int[]{1,2,3,4};
return this;
}
public boolean equals(Object obj){
return obj instanceof TestClassArray &&
Arrays.equals(this.array ,((TestClassArray)obj).array);
} }
} }
} }