Commits

John Marsden  committed 949f8c4

Updates to Marshaller

  • Participants
  • Parent commits c9033b3

Comments (0)

Files changed (7)

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

 
     public Object marshalJSONDocument(Value value, Class<?> objectClass) throws JSONMarshalerException {
         Object resultObject = null;
-        if (JSONMarshaler.hasCodec(objectClass)) {
-            JSONValueCodecHelper codecHelper = JSONMarshaler.getCodecHelper(objectClass);
-            try {
-                resultObject = codecHelper.decode(value, objectClass);
-            } catch (IllegalAccessException ex) {
-                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-            } catch (IllegalArgumentException ex) {
-                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-            } catch (InvocationTargetException ex) {
-                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-            }
-        } else {
-            ReflectType type = ReflectType.inspectObjectType(objectClass);
-            switch (type) {
-                case OBJECT:
-                    if (value.type() == Value.TYPE.OBJECT) {
-                        JSON.Object<CharSequence, Value> jsonObjectRoot = (JSON.Object<CharSequence, Value>) value;
-                        resultObject = marshalJSONDocumentObject(jsonObjectRoot, objectClass);
-                    } else {
-                        // TODO
-                    }
-                    break;
-                case ARRAY:
-                case ARRAY_BOOLEAN:
-                case ARRAY_BYTE:
-                case ARRAY_SHORT:
-                case ARRAY_INTEGER:
-                case ARRAY_FLOAT:
-                case ARRAY_DOUBLE:
-                case ARRAY_LONG:
-                case ARRAY_STRING:
-                case ARRAY_ENUM:
-                    if (value.type() == Value.TYPE.ARRAY) {
-                        JSON.Array<Value> jsonArrayRoot = (JSON.Array<Value>) value;
-                        resultObject = marshalJSONDocumentArray(jsonArrayRoot, objectClass);
-                    } else {
-                        // TODO: Strict
-                    }
-                    break;
 
-                case STRING:
-                    if (value.type() == Value.TYPE.STRING) {
-                        return value.toString();
-                    }
-                    break;
-                case BOOLEAN:
-                    if (value.type() == Value.TYPE.TRUE) {
-                        return true;
-                    } else if (value.type() == Value.TYPE.FALSE) {
-                        return false;
-                    } else {
-                    }
-                    break;
-                case BYTE:
-                    if (value.type() == Value.TYPE.NUMERIC) {
-                        return value.getNumber().byteValue();
-                    } else {
-                        // TODO: Strict
-                    }
-                    break;
-                case SHORT:
-                    if (value.type() == Value.TYPE.NUMERIC) {
-                        return value.getNumber().shortValue();
-                    } else {
-                        // TODO: Strict
-                    }
-                    break;
-                case INTEGER:
-                    if (objectClass == BigInteger.class) {
-                        return new BigInteger(value.getString());
-                    } else if (value.type() == Value.TYPE.NUMERIC) {
-                        return value.getNumber().intValue();
-                    } else {
-                        // TODO: Strict
-                    }
-                    break;
-                case DOUBLE:
-                    if (objectClass == BigDecimal.class) {
-                        return new BigDecimal(value.getString());
-                    } else if (value.type() == Value.TYPE.NUMERIC) {
-                        return value.getNumber().doubleValue();
-                    } else {
-                        // TODO: Strict
-                    }
-                    break;
-                case FLOAT:
-                    if (value.type() == Value.TYPE.NUMERIC) {
-                        return value.getNumber().floatValue();
-                    } else {
-                        // TODO: Strict
-                    }
-                    break;
-                case LONG:
-                    if (value.type() == Value.TYPE.NUMERIC) {
-                        return value.getNumber().longValue();
-                    } else {
-                        // TODO: Strict
-                    }
-                    break;
-                case UNKOWN:
-                default:
-                    System.out.println("Unknown Type:" + type + " " + value);
-                    throw new RuntimeException("Unhandled Type " + type + " " + value);
-            }
+        ReflectType type = ReflectType.inspectObjectType(objectClass);
+        switch (type) {
+            case OBJECT:
+                if (value.type() == Value.TYPE.OBJECT) {
+                    JSON.Object<CharSequence, Value> jsonObjectRoot = (JSON.Object<CharSequence, Value>) value;
+                    resultObject = marshalJSONDocumentObject(jsonObjectRoot, objectClass);
+                } else {
+                    // TODO
+                }
+                break;
+            case ARRAY:
+            case ARRAY_BOOLEAN:
+            case ARRAY_BYTE:
+            case ARRAY_SHORT:
+            case ARRAY_INTEGER:
+            case ARRAY_FLOAT:
+            case ARRAY_DOUBLE:
+            case ARRAY_LONG:
+            case ARRAY_STRING:
+            case ARRAY_ENUM:
+                if (value.type() == Value.TYPE.ARRAY) {
+                    JSON.Array<Value> jsonArrayRoot = (JSON.Array<Value>) value;
+                    resultObject = marshalJSONDocumentArray(jsonArrayRoot, objectClass);
+                } else {
+                    // TODO: Strict
+                }
+                break;
+
+            case STRING:
+                if (value.type() == Value.TYPE.STRING) {
+                    return value.toString();
+                }
+                break;
+            case BOOLEAN:
+                if (value.type() == Value.TYPE.TRUE) {
+                    return true;
+                } else if (value.type() == Value.TYPE.FALSE) {
+                    return false;
+                } else {
+                }
+                break;
+            case BYTE:
+                if (value.type() == Value.TYPE.NUMERIC) {
+                    return value.getNumber().byteValue();
+                } else {
+                    // TODO: Strict
+                }
+                break;
+            case SHORT:
+                if (value.type() == Value.TYPE.NUMERIC) {
+                    return value.getNumber().shortValue();
+                } else {
+                    // TODO: Strict
+                }
+                break;
+            case INTEGER:
+                if (objectClass == BigInteger.class) {
+                    return new BigInteger(value.getString());
+                } else if (value.type() == Value.TYPE.NUMERIC) {
+                    return value.getNumber().intValue();
+                } else {
+                    // TODO: Strict
+                }
+                break;
+            case DOUBLE:
+                if (objectClass == BigDecimal.class) {
+                    return new BigDecimal(value.getString());
+                } else if (value.type() == Value.TYPE.NUMERIC) {
+                    return value.getNumber().doubleValue();
+                } else {
+                    // TODO: Strict
+                }
+                break;
+            case FLOAT:
+                if (value.type() == Value.TYPE.NUMERIC) {
+                    return value.getNumber().floatValue();
+                } else {
+                    // TODO: Strict
+                }
+                break;
+            case LONG:
+                if (value.type() == Value.TYPE.NUMERIC) {
+                    return value.getNumber().longValue();
+                } else {
+                    // TODO: Strict
+                }
+                break;
+            case CLASS:
+                if (value.type() == Value.TYPE.STRING) {
+                    try {
+                        return Class.forName(value.toString());
+                    } catch (ClassNotFoundException ex) { }
+                }
+            case UNKOWN:
+            default:
+                System.out.println("Unknown Type:" + type + " " + value);
+                throw new RuntimeException("Unhandled Type " + type + " " + value);
         }
+
         return resultObject;
     }
 
     public Object marshalJSONDocumentObject(JSON.Object<CharSequence, Value> jsonObject, Class<?> objectClass) throws JSONMarshalerException {
         Object object = null;
 
-        try {
-            object = objectClass.newInstance();
-        } catch (InstantiationException ex) {
-            Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-            throw new JSONMarshalerException("newInstance");
-        } catch (IllegalAccessException ex) {
-            Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-            throw new JSONMarshalerException("illegalAccess");
-        }
+        if (JSONMarshaler.hasCodec(objectClass)) {
+            JSONValueCodecHelper codecHelper = JSONMarshaler.getCodecHelper(objectClass);
+            try {
+                object = codecHelper.decode(jsonObject, objectClass);
+            } catch (IllegalAccessException ex) {
+                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (IllegalArgumentException ex) {
+                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+            } catch (InvocationTargetException ex) {
+                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+            }
+        } else {
 
-        ReflectType type = ReflectType.inspectObjectType(objectClass);
-        Inspection inspection = ReflectType.getInspection(objectClass);
+            try {
+                object = objectClass.newInstance();
+            } catch (InstantiationException ex) {
+                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                throw new JSONMarshalerException("newInstance");
+            } catch (IllegalAccessException ex) {
+                Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                throw new JSONMarshalerException("illegalAccess");
+            }
 
-        boolean hasCollector = inspection.hasCollectors();
-        Map<CharSequence, Value> collector = null;
-        Map<CharSequence, Value> innerObjectCollector = null;
-        List<Value> innerArrayCollector = null;
+            ReflectType type = ReflectType.inspectObjectType(objectClass);
+            Inspection inspection = ReflectType.getInspection(objectClass);
 
-        if (hasCollector) {
-            collector = new HashMap<CharSequence, Value>();
-        }
-        if (inspection.hasInnerMap()) {
-            innerObjectCollector = new HashMap<CharSequence, Value>();
-        }
-        if (inspection.hasInnerList()) {
-            innerArrayCollector = new ArrayList<Value>();
-        }
+            boolean hasCollector = inspection.hasCollectors();
+            Map<CharSequence, Value> collector = null;
+            Map<CharSequence, Value> innerObjectCollector = null;
+            List<Value> innerArrayCollector = null;
 
-        for (Entry<CharSequence, Value> documentPropertyEntry : jsonObject.entrySet()) {
-            CharSequence key = documentPropertyEntry.getKey();
-            if (inspection.hasProperty(key.toString())) {
-                ClassProperty inspectorProperty = inspection.getProperty(key.toString());
-                Value documentValue = documentPropertyEntry.getValue();
-                try {
-                    marshalJSONValue(documentValue, object, inspectorProperty);
-                } catch (IllegalAccessException ex) {
-                    Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-                } catch (IllegalArgumentException ex) {
-                    Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-                } catch (InvocationTargetException ex) {
-                    Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
-                }
-            } else {
-                if (hasCollector) {
-                    collector.put(key, documentPropertyEntry.getValue());
-                }
-                if (inspection.hasInnerMap()) {
-                    innerObjectCollector.put(key, documentPropertyEntry.getValue());
+            if (hasCollector) {
+                collector = new HashMap<CharSequence, Value>();
+            }
+            if (inspection.hasInnerMap()) {
+                innerObjectCollector = new HashMap<CharSequence, Value>();
+            }
+            if (inspection.hasInnerList()) {
+                innerArrayCollector = new ArrayList<Value>();
+            }
+
+            for (Entry<CharSequence, Value> documentPropertyEntry : jsonObject.entrySet()) {
+                CharSequence key = documentPropertyEntry.getKey();
+                if (inspection.hasProperty(key.toString())) {
+                    ClassProperty inspectorProperty = inspection.getProperty(key.toString());
+                    Value documentValue = documentPropertyEntry.getValue();
+                    try {
+                        marshalJSONValue(documentValue, object, inspectorProperty);
+                    } catch (IllegalAccessException ex) {
+                        Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                    } catch (IllegalArgumentException ex) {
+                        Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                    } catch (InvocationTargetException ex) {
+                        Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                    }
+                } else {
+                    if (hasCollector) {
+                        collector.put(key, documentPropertyEntry.getValue());
+                    }
+                    if (inspection.hasInnerMap()) {
+                        innerObjectCollector.put(key, documentPropertyEntry.getValue());
+                    }
+                    if (inspection.hasInnerList()) {
+                        innerArrayCollector.add(documentPropertyEntry.getValue());
+                    }
                 }
-                if (inspection.hasInnerList()) {
-                    innerArrayCollector.add(documentPropertyEntry.getValue());
+            }
+
+            if (hasCollector) {
+                for (ClassProperty collectorProperty : inspection.getCollectors()) {
+                    try {
+                        collectorProperty.getMutator().fire(object, collector);
+                    } catch (IllegalAccessException ex) {
+                        Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                    } catch (IllegalArgumentException ex) {
+                        Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                    } catch (InvocationTargetException ex) {
+                        Logger.getLogger(JSONDocumentMarshaler.class.getName()).log(Level.SEVERE, null, ex);
+                    }
                 }
             }
+            if (inspection.hasInnerMap()) {
+            }
+            if (inspection.hasInnerList()) {
+            }
         }
         return object;
     }
     }
 
     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) {
+                if (parameterTypeArray[0] instanceof Class) {
                     keyClass = (Class<?>) parameterTypeArray[0];
                 }
                 Class<?> valueClass = null;
-                if(parameterTypeArray[1] instanceof Class) {
+                if (parameterTypeArray[1] instanceof Class) {
                     valueClass = (Class<?>) parameterTypeArray[1];
-                } else if(parameterTypeArray[1] instanceof ParameterizedType) {
+                } 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()) {
-                    
+                while (keySetIterator.hasNext()) {
+
                     CharSequence keyValue = keySetIterator.next();
                     Value value = jsonMap.get(keyValue);
-                    
+
                     Object o = marshalJSONDocument(value, valueClass);
                     returnMap.put(keyValue, o);
                 }
-                
+
                 return returnMap;
 
             } else {

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

 
 import cc.plural.jsonij.JSON;
 import cc.plural.jsonij.Value;
-import cc.plural.jsonij.marshal.codec.DateJSONValueCodec;
 import cc.plural.jsonij.marshal.codec.JSONValueCodec;
 import cc.plural.jsonij.marshal.codec.JSONValueCodecStore;
 import cc.plural.jsonij.parser.ParserException;
 import java.io.IOException;
 import java.io.InputStream;
-import java.util.Date;
 
 /**
  *
 
     protected static final JavaMarshaler JAVA_MARSHALLER;
     protected static final JSONDocumentMarshaler JSON_DOCUMENT_MARSHALER;
+    public static boolean ALWAYS_SPECIFY_CLASS;
+    public static String CLASS_PROPERTY;
     public static boolean ALWAYS_USE_INNER_PROPERTY;
     public static String INNER_ARRAY_PROPERTY;
     public static String INNER_OBJECT_PROPERTY;
     static JSONValueCodecStore CODEC_STORE;
     static int CYCLE_LEVELS;
 
-
     static {
         JAVA_MARSHALLER = new JavaMarshaler();
         JSON_DOCUMENT_MARSHALER = new JSONDocumentMarshaler();
+        ALWAYS_SPECIFY_CLASS = false;
+        CLASS_PROPERTY = "$class";
         ALWAYS_USE_INNER_PROPERTY = false;
         INNER_ARRAY_PROPERTY = "$innerArray";
         INNER_OBJECT_PROPERTY = "$innerObject";

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

         }
         Value marshaledObject = null;
         Class<?> objectClass = o.getClass();
-        
-                // Check for JSONCodec
+        // Check for JSONCodec
         if (JSONMarshaler.CODEC_STORE != null && JSONMarshaler.CODEC_STORE.hasCodec(objectClass)) {
             JSONValueCodecStore.JSONValueCodecHelper codecHelper = JSONMarshaler.CODEC_STORE.getCodecHelper(objectClass);
             Value value = null;
             }
             return value;
         }
-        
         ReflectType objectType = ReflectType.inspectObjectType(objectClass);
         switch (objectType) {
             case BOOLEAN:
                 marshaledObject = marshalJavaMap(o, cycleDetector);
                 break;
             case CLASS:
-                marshaledObject = marshalJavaObject(o, cycleDetector);
+                marshaledObject = new JSON.String(o.getClass().getCanonicalName());
+                break;
+            case JSON_VALUE:
+                marshaledObject = (Value) o;
                 break;
             case UNKOWN:
                 marshaledObject = new JSON.String(o.toString());
 
     protected Value marshalJavaString(Object o) {
         Value value = null;
-        String marshaledString = (String) o;
+        String marshaledString = o.toString();
         if (marshaledString != null) {
             value = new JSON.String(marshaledString);
         } else {

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

         Value value;
         int propCount = 0;
         for (ClassProperty property : properties) {
+//            if(property.isCollector()) {
+//                continue;
+//            }
             ClassPropertyAccessor accessor = property.getAccessor();
             if (accessor == null || !accessor.canAccess()) {
                 continue;
             }
+
             if (accessor.fieldType()) {
                 try {
                     Field field = property.getAccessor().getField();
                         continue;
                     }
                 } catch (Exception ex) {
+                    ex.printStackTrace();
                     value = new JSON.String(ex.toString());
                 }
             } else if (accessor.methodType()) {
                 String key = keySetIterator.next();
                 marshaledObject.put(new JSON.String(key), valueCollector.get(key));
             }
+            if (inspection.hasCollectors()) {
+                for(ClassProperty collector: inspection.getCollectors()){
+                    //collector.getMutator().fire(o, valueCollector);
+                }
+            }
             return marshaledObject;
         }
+
     }
 
     protected Value marshalObjectMethodValue(Method method, Object o, CycleDetector cycleDetector) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, JSONMarshalerException {
         }
         return value;
     }
-
-    
 }

File src/main/java/cc/plural/jsonij/reflect/ClassProperty.java

     private String propertyName;
     private ClassPropertyAccessor accessor;
     private ClassPropertyMutator mutator;
-
+    private boolean collector;
+    
     public ClassProperty() {
+        klass = null;
+        propertyName = null;
+        accessor = null;
+        mutator = null;
+        collector = false;
     }
 
     public ClassPropertyAccessor getAccessor() {
         this.propertyName = propertyName;
     }
 
+    public boolean isCollector() {
+        return collector;
+    }
+
+    public void setCollector(boolean collector) {
+        this.collector = collector;
+    }
+
     public void registerAccessor(Field field) {
         if (accessor == null) {
             accessor = new ClassPropertyAccessor();

File src/main/java/cc/plural/jsonij/reflect/InspectionFactory.java

  */
 package cc.plural.jsonij.reflect;
 
+import cc.plural.jsonij.marshal.annotation.JSONCollector;
 import cc.plural.jsonij.marshal.annotation.JSONName;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
             if (property.isValid() && !classProperties.contains(property)) {
                 classProperties.add(property);
             }
+            if(property.isValid() && property.isCollector() && !collectorProperties.contains(property)) {
+                collectorProperties.add(property);
+            }
         }
 
         Inspection inspection = new Inspection();
                 JSONName jsonName = (JSONName) field.getAnnotation(JSONName.class);
                 propertyName = jsonName.value();
             }
+            if (field.isAnnotationPresent(JSONCollector.class)) {
+                property.setCollector(true);
+            }
             property.setPropertyName(propertyName);
             property.registerAccessor(field);
             property.registorMutator(field);
             char ch;
             String propertyName = null;
             String methodName = method.getName();
-
+            boolean collector = false;
+            
             Class<?> returnType = method.getReturnType();
             if (methodName.length() > IS_PREFIX.length() && methodName.startsWith(IS_PREFIX) && returnType == boolean.class && Character.isUpperCase(ch = methodName.charAt(IS_PREFIX.length()))) {
                 if (method.isAnnotationPresent(JSONName.class)) {
                 } else {
                     propertyName = Character.toLowerCase(ch) + methodName.substring(IS_PREFIX.length() + 1, methodName.length());
                 }
+                if (method.isAnnotationPresent(JSONCollector.class)) {
+                    collector = true;
+                }
                 ClassProperty property = null;
                 if (propertyMap.containsKey(propertyName)) {
                     property = propertyMap.get(propertyName);
                     property.setPropertyName(propertyName);
                     property.registerAccessor(method);
                     propertyMap.put(propertyName, property);
+                    property.setCollector(collector);
                 }
             } else if (methodName.length() > SET_PREFIX.length() && methodName.startsWith(SET_PREFIX) && Character.isUpperCase(ch = methodName.charAt(SET_PREFIX.length()))) {
                 propertyName = Character.toLowerCase(ch) + methodName.substring(SET_PREFIX.length() + 1, methodName.length());

File src/main/java/cc/plural/jsonij/reflect/ReflectType.java

  */
 package cc.plural.jsonij.reflect;
 
+import cc.plural.jsonij.Value;
 import java.lang.reflect.Array;
 import java.util.HashMap;
 import java.util.List;
     ARRAY_MAP,
     ARRAY_ARRAY,
     CLASS,
+    JSON_VALUE,
     UNKOWN;
     boolean primitive;
     JavaArrayType arrayType;
             type = getArrayType(c);
             return type;
         }
-        
+
         if(c == Class.class) {
             return CLASS;
         }
         Class<?> currentClass = c;
 
         do {
+            if(currentClass == Value.class) {
+                type = JSON_VALUE;
+                break;
+            }
             interfaces = currentClass.getInterfaces();
             for (int i = 0; i < Array.getLength(interfaces); i++) {
                 if (interfaces[i] == List.class) {
                     type = LIST;
+                    break;
                 } else if (interfaces[i] == Map.class) {
                     type = MAP;
+                    break;
                 } else if (interfaces[i] == CharSequence.class) {
                     type = STRING;
+                    break;
+                } else if (interfaces[i] == Value.class){
+                    type = JSON_VALUE;
+                    break;
                 }
             }
         } while ((currentClass = currentClass.getSuperclass()) != null);