Commits

John Marsden committed c99a700

Updates

Comments (0)

Files changed (3)

src/main/java/cc/plural/jsonij/marshal/JavaType.java

 import java.util.Map;
 
 /**
- * JavaType Utility. Queries objects for their types and manages inspector 
+ * JavaType Utility. Queries objects for their types and manages inspector
  * instances so time is not wasted re-inspecting the same objects.
- * 
- * Inspecting is the task of reflecting attributes and methods and their 
+ *
+ * Inspecting is the task of reflecting attributes and methods and their
  * types for access.
- * 
+ *
  * @author jmarsden@plural.cc
  */
 public enum JavaType {
     ARRAY_MAP,
     ARRAY_ARRAY,
     UNKOWN;
+
     boolean primitive;
+
     JavaArrayType arrayType;
-    
+
     static final protected Map<Class<?>, Inspector> inspectedClasses;
 
     static {
 
     public static JavaType inspectObjectType(Class<?> c) {
         JavaType type = null;
-        if (c == boolean.class) {
+        if(c == boolean.class) {
             type = JavaType.BOOLEAN;
             type.setPrimitive(true);
-	    return type;
-        } else if (c == Boolean.class) {
+            return type;
+        } else if(c == Boolean.class) {
             type = JavaType.BOOLEAN;
             type.setPrimitive(false);
-	    return type;
-        } else if (c == int.class) {
+            return type;
+        } else if(c == int.class) {
             type = JavaType.INTEGER;
             type.setPrimitive(true);
-	    return type;
-        } else if (c == Integer.class) {
+            return type;
+        } else if(c == Integer.class) {
             type = JavaType.INTEGER;
             type.setPrimitive(false);
-	    return type;
-        } else if (c == double.class) {
+            return type;
+        } else if(c == double.class) {
             type = JavaType.DOUBLE;
             type.setPrimitive(true);
-	    return type;
-        } else if (c == Double.class) {
+            return type;
+        } else if(c == Double.class) {
             type = JavaType.DOUBLE;
             type.setPrimitive(false);
-	    return type;
-        } else if (c == float.class) {
+            return type;
+        } else if(c == float.class) {
             type = JavaType.FLOAT;
             type.setPrimitive(true);
-	    return type;
-        } else if (c == Float.class) {
+            return type;
+        } else if(c == Float.class) {
             type = JavaType.FLOAT;
             type.setPrimitive(false);
-	    return type;
-        } else if (c == long.class) {
+            return type;
+        } else if(c == long.class) {
             type = JavaType.LONG;
             type.setPrimitive(true);
-	    return type;
-        } else if (c == Long.class) {
+            return type;
+        } else if(c == Long.class) {
             type = JavaType.LONG;
             type.setPrimitive(false);
-	    return type;
-        } else if (c == short.class) {
+            return type;
+        } else if(c == short.class) {
             type = JavaType.SHORT;
             type.setPrimitive(true);
-	    return type;
-        } else if (c == Short.class) {
+            return type;
+        } else if(c == Short.class) {
             type = JavaType.SHORT;
             type.setPrimitive(false);
-	    return type;
-        } else if (c == byte.class) {
+            return type;
+        } else if(c == byte.class) {
             type = JavaType.BYTE;
             type.setPrimitive(true);
-	    return type;
-        } else if (c == Byte.class) {
+            return type;
+        } else if(c == Byte.class) {
             type = JavaType.BYTE;
             type.setPrimitive(false);
-	    return type;
-        } else if (c.isEnum()) {
+            return type;
+        } else if(c.isEnum()) {
             type = JavaType.ENUM;
             type.setPrimitive(false);
-	    return type;
-        } else if (c == String.class || ( ( c == char.class || c == Character.class ) )) {
+            return type;
+        } else if(c == String.class || ((c == char.class || c == Character.class))) {
             type = JavaType.STRING;
             type.setPrimitive(false);
-	    return type;
-        } else if (c.isArray()) {
+            return type;
+        } else if(c.isArray()) {
             type = getArrayType(c);
-	    return type;
-        } else if (c instanceof Class) {
-	    type = OBJECT;
-	    return type;
-	}
-        if (type == null) {
-            if (c == List.class) {
+            return type;
+        }
+        if(type == null) {
+            if(c == List.class) {
                 type = JavaType.LIST;
                 type.setPrimitive(false);
-		return type;
+                return type;
             } else {
                 // Test if Object is a List
                 Class<?>[] interfaces = c.getInterfaces();
-                for (int i = 0; i < Array.getLength(interfaces); i++) {
-                    if (interfaces[i] == List.class) {
+                for(int i = 0; i < Array.getLength(interfaces); i++) {
+                    if(interfaces[i] == List.class) {
                         type = JavaType.LIST;
                         type.setPrimitive(false);
-			return type;
+                        return type;
                     }
                 }
             }
             Class<?>[] interfaces = c.getInterfaces();
             // Test if super classes are List
             Class<?> parent = c.getSuperclass();
-            if (parent != null) {
+            if(parent != null) {
                 do {
                     interfaces = parent.getInterfaces();
-                    for (int i = 0; i < Array.getLength(interfaces); i++) {
-                        if (interfaces[i] == List.class) {
+                    for(int i = 0; i < Array.getLength(interfaces); i++) {
+                        if(interfaces[i] == List.class) {
                             type = JavaType.LIST;
-			    return type;
+                            return type;
                         }
                     }
-                } while (( parent = parent.getSuperclass() ) != null);
+                } while((parent = parent.getSuperclass()) != null);
             }
         }
-        if (type == null) {
-            if (c == Map.class) {
+        if(type == null) {
+            if(c == Map.class) {
                 type = JavaType.MAP;
                 type.setPrimitive(false);
-		return type;
+                return type;
             } else {
                 // Test if Object is a List
                 Class<?>[] interfaces = c.getInterfaces();
-                for (int i = 0; i < Array.getLength(interfaces); i++) {
-                    if (interfaces[i] == Map.class) {
+                for(int i = 0; i < Array.getLength(interfaces); i++) {
+                    if(interfaces[i] == Map.class) {
                         type = JavaType.MAP;
                         type.setPrimitive(false);
-			return type;
+                        return type;
                     }
                 }
             }
             Class<?>[] interfaces = c.getInterfaces();
             Class<?> parent = c.getSuperclass();
-            if (parent != null) {
+            if(parent != null) {
                 do {
                     interfaces = parent.getInterfaces();
-                    for (int i = 0; i < Array.getLength(interfaces); i++) {
-                        if (interfaces[i] == Map.class) {
+                    for(int i = 0; i < Array.getLength(interfaces); i++) {
+                        if(interfaces[i] == Map.class) {
                             type = JavaType.MAP;
-			    return type;
-			}
+                            return type;
+                        }
                     }
-                } while (( parent = parent.getSuperclass() ) != null);
+                } while((parent = parent.getSuperclass()) != null);
             }
         }
-        if (type == null) {
-            if (isObjectType(c)) {
+        if(c.getComponentType() ==  Class.class) {
+            type = JavaType.OBJECT;
+            return type;
+        }
+         
+        if(type == null) {
+            if(isObjectType(c)) {
                 type = JavaType.OBJECT;
                 type.setPrimitive(false);
-		return type;
+                return type;
             }
+            
         }
-        if (type != null) {
+        if(type != null) {
             return type;
         } else {
             return JavaType.UNKOWN;
 
     public static Inspector getInstpector(Class<?> objectClass) {
         Inspector inspector = null;
-        synchronized (inspectedClasses) {
-            if (inspectedClasses.containsKey(objectClass)) {
+        synchronized(inspectedClasses) {
+            if(inspectedClasses.containsKey(objectClass)) {
                 inspector = inspectedClasses.get(objectClass);
             } else {
                 inspector = new Inspector(objectClass);
     public static boolean isObjectType(Class<?> objectClass) {
         Inspector inspector = getInstpector(objectClass);
         InspectorProperty[] properties = inspector.getProperties();
-        if (( properties != null && Array.getLength(properties) > 0 ) || inspector.hasInnerArray() || inspector.hasInnerObject()) {
+        if((properties != null && Array.getLength(properties) > 0) || inspector.hasInnerArray() || inspector.hasInnerObject()) {
             return true;
         }
         return false;
     }
 
     @SuppressWarnings("unused")
-	public static JavaType getArrayType(Class<?> objectClass) {
+    public static JavaType getArrayType(Class<?> objectClass) {
         JavaType resultType = null;
         Class<?> objectComponentType = objectClass.getComponentType();
-        
-        if (!objectComponentType.isArray()) {
-            
-            
+
+        if(!objectComponentType.isArray()) {
+
+
             JavaType componentType = inspectObjectType(objectComponentType);
             //resultType.arrayType = new JavaArrayType(objectClass);
-            switch (componentType) {
+            switch(componentType) {
                 case BOOLEAN:
                     resultType = JavaType.ARRAY_BOOLEAN;
                     resultType.setPrimitive(componentType.isPrimitive());
             resultType = JavaType.ARRAY_ARRAY;
             resultType.setPrimitive(false);
         }
-        
-        if (resultType != null) {
+
+        if(resultType != null) {
             return resultType;
         } else {
             return JavaType.UNKOWN;
         }
     }
-    
+
     public static class JavaArrayType {
+
         Class<?> arrayType;
+
         int dimension;
-        
+
         public JavaArrayType(Class<?> arrayType) {
             this.arrayType = arrayType;
             dimension = 0;
             this.inspect();
         }
-        
+
         public final void inspect() {
             if(arrayType != null && arrayType.isArray()) {
                 Class<?> innerType = arrayType.getComponentType();
         @Override
         public String toString() {
             return "Array " + arrayType.getSimpleName() + "[" + dimension + "]";
-        }       
+        }
     }
 }

src/test/java/cc/plural/jsonij/marshal/JSONDocumentMarshalerTest.java

-/*
+ /*
  * Copyright 2011 jmarsden.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
 package cc.plural.jsonij.marshal;
 
 import java.io.IOException;
-import java.lang.Class;
-import java.lang.String;
 
 import cc.plural.jsonij.JSON;
 import cc.plural.jsonij.JSONMarshaler;
-import cc.plural.jsonij.marshal.HandlerConfig;
 import cc.plural.jsonij.marshal.codec.ClassJSONCodec;
 import cc.plural.jsonij.marshal.helpers.JSONObject;
 import cc.plural.jsonij.parser.ParserException;
-import org.junit.Test;
-
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import org.junit.Test;
 
 /**
  *
 
     @Test
     public void testMarshalJSONStringIntoIntArray() throws JSONMarshalerException, IOException, ParserException {
-	//System.out.println("marshal JSON into Array");
-	//String inputJSONDocument = "[1,2,3,4,5]";
-	//JSON inputJSON = JSON.parse(inputJSONDocument);
-	//Object marshal = JSONMarshaler.marshalJSON(inputJSON, int.class);
-	//JSON outputJSON = JSONMarshaler.marshalObject(marshal);
-	//System.out.println(String.format("InputJSON: %s", inputJSON));
-	//System.out.println(String.format("MarshaledObjectToString: %s", marshal));
-	//System.out.println(String.format("OutputJSON: %s", outputJSON));
-	//assertNotNull(marshal);
-	//assertEquals(inputJSON, outputJSON);
+        //System.out.println("marshal JSON into Array");
+        //String inputJSONDocument = "[1,2,3,4,5]";
+        //JSON inputJSON = JSON.parse(inputJSONDocument);
+        //Object marshal = JSONMarshaler.marshalJSON(inputJSON, int.class);
+        //JSON outputJSON = JSONMarshaler.marshalObject(marshal);
+        //System.out.println(String.format("InputJSON: %s", inputJSON));
+        //System.out.println(String.format("MarshaledObjectToString: %s", marshal));
+        //System.out.println(String.format("OutputJSON: %s", outputJSON));
+        //assertNotNull(marshal);
+        //assertEquals(inputJSON, outputJSON);
     }
 
     @Test
     public void testMarshalJSONStringIntoObject() throws JSONMarshalerException, IOException, ParserException {
-	System.out.println("marshal JSON into Object");
-	String inputJSONDocument = "{\"message\":\"Hello JSON!\", \"rah\":-69, \"id\": 585757346726,    \"flag\": true, \"innerDoubleArray\": [1], \"inner\": {\"someValue\": \"hmmmn...\", \"rah\": [5,4,3,2,1]}}";
+        System.out.println("marshal JSON into Object");
+        String inputJSONDocument = "{\"message\":\"Hello JSON!\", \"rah\":-69, \"id\": 585757346726,    \"flag\": true, \"innerDoubleArray\": [1], \"inner\": {\"someValue\": \"hmmmn...\", \"rah\": [5,4,3,2,1]}}";
 
-	JSON inputJSON = JSON.parse(inputJSONDocument);
-	Object marshal = JSONMarshaler.marshalJSON(inputJSON, JSONObject.class);
-	JSON outputJSON = JSONMarshaler.marshalObject(marshal);
-	System.out.println(String.format("InputJSON: %s", inputJSON));
-	System.out.println(String.format("MarshaledObjectToString: %s", marshal));
-	System.out.println(String.format("OutputJSON: %s", outputJSON));
+        JSON inputJSON = JSON.parse(inputJSONDocument);
+        Object marshal = JSONMarshaler.marshalJSON(inputJSON, JSONObject.class);
+        JSON outputJSON = JSONMarshaler.marshalObject(marshal);
+        System.out.println(String.format("InputJSON: %s", inputJSON));
+        System.out.println(String.format("MarshaledObjectToString: %s", marshal));
+        System.out.println(String.format("OutputJSON: %s", outputJSON));
 
-	assertNotNull(marshal);
-	assertEquals(inputJSON, outputJSON);
+        assertNotNull(marshal);
+        assertEquals(inputJSON, outputJSON);
     }
 
     @Test
     public void testMarshalHandlerClass() throws JSONMarshalerException, IOException, ParserException, IllegalAccessException, InstantiationException, ClassNotFoundException {
-	HandlerConfig<String, Class<?>> config = new HandlerConfig<String, Class<?>>();
-	config.put("http", Handler.class);
-	config.put("ftp", Handler.class);
-	
-	JavaObjectMarshaler.registerCodec(ClassJSONCodec.class);
-	JSON outputJSON = JSONMarshaler.marshalObject(config);
-	System.out.println("Output JSON:" + outputJSON);
+        System.out.println("testMarshalHandlerClass");
+        HandlerConfig<String, Class<?>> config = new HandlerConfig<String, Class<?>>();
+        config.put("http", Handler.class);
+        config.put("ftp", Handler.class);
+
+        JavaObjectMarshaler.registerCodec(ClassJSONCodec.class);
+        JSON outputJSON = JSONMarshaler.marshalObject(config);
+        System.out.println("Output JSON:" + outputJSON);
+
+        Class<?> c = Class.forName("cc.plural.jsonij.marshal.JSONDocumentMarshalerTest$Handler");
+        Handler handler = (Handler) c.newInstance();
+        assertNotNull(handler);
 
-	Class<?> c = Class.forName("cc.plural.jsonij.marshal.JSONDocumentMarshalerTest$Handler");
-	Handler handler = (Handler) c.newInstance();
-	assertNotNull(handler);
-	
-	
-	HandlerConfig<String, Class<?>> reload = (HandlerConfig<String, Class<?>>) JSONMarshaler.marshalJSON(outputJSON, config.getClass());
-	
+        HandlerConfig<String, Class<?>> reload = (HandlerConfig<String, Class<?>>) JSONMarshaler.marshalJSON(outputJSON, config.getClass());
     }
 
     public static class Handler {
 
-	public String test;
+        public String test;
 
-	public Handler() {
-	    test = "Test";
-	}
+        public Handler() {
+            test = "Test";
+        }
     }
 }

src/test/java/cc/plural/jsonij/marshal/NebulousServerTest.java

 
     @Test
     public void testMarshalConfiguration() throws JSONMarshalerException, IOException, ParserException {
-	URL url = getClass().getResource("/etc/config.json");
-	InputStream stream = url.openStream();
+        URL url = getClass().getResource("/etc/config.json");
+        InputStream stream = url.openStream();
 
-	Object object = JSONMarshaler.marshalJSON(stream, NebulousConfig.class);
 
-	JSON output = JSONMarshaler.marshalObject(object);
-	System.out.println(output.toJSON());
+        Object object = JSONMarshaler.marshalJSON(stream, NebulousConfig.class);
+
+        JSON output = JSONMarshaler.marshalObject(object);
+        System.out.println(output.toJSON());
     }
 
     public static class NebulousConfig {
 
-	HttpConfig http;
+        HttpConfig http;
 
-	public NebulousConfig() {
-	    http = null;
-	}
+        public NebulousConfig() {
+            http = null;
+        }
 
-	public HttpConfig getHttp() {
-	    return http;
-	}
+        public HttpConfig getHttp() {
+            return http;
+        }
 
-	public void setHttp(HttpConfig http) {
-	    this.http = http;
-	}
+        public void setHttp(HttpConfig http) {
+            this.http = http;
+        }
     }
 
     public static class HttpConfig {
 
-	String handler;
-	String[] index;
-	String mime;
-	String defaultMime;
-	ServerConfig server[];
+        String handler;
+
+        String[] index;
+
+        String mime;
+
+        String defaultMime;
 
-	public HttpConfig() {
-	    handler = null;
-	}
+        ServerConfig server[];
 
-	public String getHandler() {
-	    return handler;
-	}
+        public HttpConfig() {
+            handler = null;
+        }
 
-	public void setHandler(String handler) {
-	    this.handler = handler;
-	}
+        public String getHandler() {
+            return handler;
+        }
 
-	public String[] getIndex() {
-	    return index;
-	}
+        public void setHandler(String handler) {
+            this.handler = handler;
+        }
 
-	public void setIndex(String[] index) {
-	    this.index = index;
-	}
+        public String[] getIndex() {
+            return index;
+        }
 
-	public String getDefaultMime() {
-	    return defaultMime;
-	}
+        public void setIndex(String[] index) {
+            this.index = index;
+        }
 
-	public void setDefaultMime(String defaultMime) {
-	    this.defaultMime = defaultMime;
-	}
+        public String getDefaultMime() {
+            return defaultMime;
+        }
 
-	public String getMime() {
-	    return mime;
-	}
+        public void setDefaultMime(String defaultMime) {
+            this.defaultMime = defaultMime;
+        }
 
-	public void setMime(String mime) {
-	    this.mime = mime;
-	}
+        public String getMime() {
+            return mime;
+        }
 
-	public ServerConfig[] getServer() {
-	    return server;
-	}
+        public void setMime(String mime) {
+            this.mime = mime;
+        }
 
-	public void setServer(ServerConfig[] server) {
-	    this.server = server;
-	}
+        public ServerConfig[] getServer() {
+            return server;
+        }
+
+        public void setServer(ServerConfig[] server) {
+            this.server = server;
+        }
     }
 
     public static class ServerConfig {
 
-	public int[] port;
-	public LocationConfig[] location;
-	
-	public ServerConfig() {
-	    port = null;
-	    location = null;
-	}
+        public int[] port;
+
+        public LocationConfig[] location;
+
+        public ServerConfig() {
+            port = null;
+            location = null;
+        }
     }
 
     public static class LocationConfig {
 
-	public String host;
-	public String path;
-	public RendererConfig renderer;
-	public String accessLog;
-	public String errorLog;
+        public String host;
+
+        public String path;
+
+        public RendererConfig renderer;
+
+        public String accessLog;
+
+        public String errorLog;
+
     }
 
     public static class RendererConfig {
 
-	public String handler;
-	public String jar;
+        public String handler;
+
+        public String jar;
+
     }
 }