Commits

John Marsden  committed c9033b3

Basic Document Parsing into java.util.Map

  • Participants
  • Parent commits 66d4b86

Comments (0)

Files changed (6)

File src/main/java/cc/plural/jsonij/marshal/JSONDocumentMarshaler.java

 import java.io.InputStream;
 import java.lang.reflect.Array;
 import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.ArrayList;
                 } else if (componentType == ReflectType.STRING) {
                     Array.set(array, i, jsonArray.get(i).getString());
                 } else {
-                    //
                 }
             }
             return array;
         return null;
     }
 
-    
-    public Map<?, ?> marshalJSONDocumentMap(JSON.Object<CharSequence, Value> jsonMap, Class<?> objectClass) {
-        System.out.println(objectClass);
+    public Map<?, ?> marshalJSONDocumentMap(JSON.Object<CharSequence, Value> jsonMap, Type type) throws JSONMarshalerException {
+        
+        if (type instanceof ParameterizedType) {
+            ParameterizedType parameterizedType = (ParameterizedType) type;
+
+            Type[] parameterTypeArray = parameterizedType.getActualTypeArguments();
+            if (parameterTypeArray != null && parameterTypeArray.length == 2) {
+                Class<?> keyClass = null;
+                if(parameterTypeArray[0] instanceof Class) {
+                    keyClass = (Class<?>) parameterTypeArray[0];
+                }
+                Class<?> valueClass = null;
+                if(parameterTypeArray[1] instanceof Class) {
+                    valueClass = (Class<?>) parameterTypeArray[1];
+                } else if(parameterTypeArray[1] instanceof ParameterizedType) {
+                    ParameterizedType valueType = (ParameterizedType) parameterTypeArray[1];
+                    valueClass = (Class<?>) valueType.getRawType();
+                }
+                
+//                if(keyClass == null || valueClass == null) {
+//                    return null;
+//                }
+//                
+                Map<CharSequence, Object> returnMap = new HashMap();
+                
+                Iterator<CharSequence> keySetIterator = jsonMap.keySet().iterator();
+                while(keySetIterator.hasNext()) {
+                    
+                    CharSequence keyValue = keySetIterator.next();
+                    Value value = jsonMap.get(keyValue);
+                    
+                    Object o = marshalJSONDocument(value, valueClass);
+                    returnMap.put(keyValue, o);
+                }
+                
+                return returnMap;
+
+            } else {
+                throw new RuntimeException("Beef this up");
+            }
+        }
+
         System.out.println(jsonMap);
 
         Iterator<CharSequence> keySet = jsonMap.keySet().iterator();
-        while(keySet.hasNext()) {
+        while (keySet.hasNext()) {
             CharSequence key = keySet.next();
             Value v = jsonMap.get(key);
-            
-            
+
+
         }
-        
+
 
         return null;
     }
                 mutator.fire(object, propertyObject);
                 return;
             case MAP:
-                Map<?, ?> propertyMap = marshalJSONDocumentMap((JSON.Object<CharSequence, Value>) value, mutator.getType());
+                Map<?, ?> propertyMap = marshalJSONDocumentMap((JSON.Object<CharSequence, Value>) value, mutator.getGenericType());
                 mutator.fire(object, propertyMap);
                 return;
             case UNKOWN:

File src/main/java/cc/plural/jsonij/marshal/JavaMarshaler.java

 
 import cc.plural.jsonij.JSON;
 import cc.plural.jsonij.Value;
+import cc.plural.jsonij.marshal.codec.JSONValueCodecStore;
 import cc.plural.jsonij.reflect.ReflectType;
 import java.lang.reflect.InvocationTargetException;
 import java.util.logging.Level;
         }
         Value marshaledObject = null;
         Class<?> objectClass = o.getClass();
+        
+                // Check for JSONCodec
+        if (JSONMarshaler.CODEC_STORE != null && JSONMarshaler.CODEC_STORE.hasCodec(objectClass)) {
+            JSONValueCodecStore.JSONValueCodecHelper codecHelper = JSONMarshaler.CODEC_STORE.getCodecHelper(objectClass);
+            Value value = null;
+            try {
+                value = codecHelper.encode(o);
+            } catch (IllegalAccessException ex) {
+                Logger.getLogger(JavaMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (IllegalArgumentException ex) {
+                Logger.getLogger(JavaMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (InvocationTargetException ex) {
+                Logger.getLogger(JavaMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+            }
+            return value;
+        }
+        
         ReflectType objectType = ReflectType.inspectObjectType(objectClass);
         switch (objectType) {
             case BOOLEAN:

File src/main/java/cc/plural/jsonij/marshal/JavaMarshalerObjects.java

             }
         }
 
-        // Check for JSONCodec
-        if (JSONMarshaler.CODEC_STORE != null && JSONMarshaler.CODEC_STORE.hasCodec(objectClass)) {
-            JSONValueCodecHelper codecHelper = JSONMarshaler.CODEC_STORE.getCodecHelper(objectClass);
-            Value value = codecHelper.encode(o);
-            return value;
-        }
+
 
         // Find an object inspector
         Inspection inspection = ReflectType.getInspection(objectClass);

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

         System.out.println("testMarshalHandlerClass");
         HandlerConfiguration config = new HandlerConfiguration();
         Map<String, Class<?>> handlers = new HashMap<String, Class<?>>();
-        handlers.put("http", Handler.class);
-        handlers.put("ftp", Handler.class);
+        handlers.put("http", JSON.class);
+        handlers.put("ftp", JSONMarshaler.class);
         config.setHandlers(handlers);
         
         JSON outputJSON = JSONMarshaler.marshalObject(config);
 
         HandlerConfiguration reload = (HandlerConfiguration) JSONMarshaler.marshalJSON(outputJSON, config.getClass());
         assertNotNull(reload);
-//        assertEquals(handlers, reload);
-    }
-
-    public static class Handler {
-
-        @JSONName("name")
-        public String test;
-
-        public Handler() {
-            test = "Test";
-        }
+        assertEquals(config.getHandlers(), reload.getHandlers());
     }
 }

File src/test/java/cc/plural/jsonij/marshal/helpers/HandlerConfiguration.java

 package cc.plural.jsonij.marshal.helpers;
 
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.Map;
 
 /**
     public void setHandlers(Map<String, Class<?>> handlers) {
         this.handlers = handlers;
     }
+
+    @Override
+    public int hashCode() {
+        int hash = 3;
+        Iterator<String> handlerIterator = handlers.keySet().iterator();
+        while (handlerIterator.hasNext()) {
+            String key = handlerIterator.next();
+            Class<?> clazz = handlers.get(key);
+            hash = 89 * hash + (key != null ? key.hashCode() : 0);
+            hash = 7 * hash + (clazz != null ? clazz.hashCode() : 0);
+        }
+        return hash;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final HandlerConfiguration other = (HandlerConfiguration) obj;
+        if (other.handlers == null && handlers == null) {
+            return true;
+        } else if (other.handlers != null && handlers != null && other.handlers.size() == handlers.size()) {
+            Iterator<String> handlerIterator = handlers.keySet().iterator();
+            while (handlerIterator.hasNext()) {
+                String key = handlerIterator.next();
+                Class<?> class1 = handlers.get(key);
+                Class<?> class2 = other.handlers.get(key);
+
+                if (class1 == null && class2 == null) {
+                    continue;
+                } else if (class1.equals(class2)) {
+                    continue;
+                }
+            }
+        }
+        return false;
+    }
+
+    @Override
+    public String toString() {
+        return "HandlerConfiguration{" + "handlers=" + handlers + '}';
+    }
+    
+    
 }

File src/test/java/jsonij/legacy/JSONStringParserTest.java

  **/
 package jsonij.legacy;
 
+import cc.plural.jsonij.JSON;
+import cc.plural.jsonij.JSONReader;
+import cc.plural.jsonij.ReaderJSONReader;
+import cc.plural.jsonij.StringJSONReader;
+import cc.plural.jsonij.ThreadSafeJSONParser;
+import cc.plural.jsonij.parser.ParserException;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.Locale;
 import org.junit.After;
 import org.junit.AfterClass;
+import static org.junit.Assert.*;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-import cc.plural.jsonij.JSON;
-import cc.plural.jsonij.ThreadSafeJSONParser;
-import cc.plural.jsonij.JSONReader;
-import cc.plural.jsonij.ReaderJSONReader;
-import cc.plural.jsonij.StringJSONReader;
-import cc.plural.jsonij.parser.ParserException;
-import static org.junit.Assert.*;
-
 /**
  * @author jmarsden
  *