]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - server/runtime/org.argeo.server.json/src/main/java/org/argeo/server/json/GenericJsonDeserializer.java
Update license headers
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.json / src / main / java / org / argeo / server / json / GenericJsonDeserializer.java
index 38a1541b20e4021cefb595d0e4216288493cc11a..add128d1bfcf1ad8fe6655e5941924aeab8b7bf3 100644 (file)
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2007-2012 Mathieu Baudier
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *         http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.argeo.server.json;
 
 import java.io.IOException;
@@ -7,7 +22,7 @@ import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.argeo.server.ArgeoServerException;
+import org.argeo.ArgeoException;
 import org.codehaus.jackson.JsonFactory;
 import org.codehaus.jackson.JsonGenerator;
 import org.codehaus.jackson.JsonNode;
@@ -24,11 +39,13 @@ public class GenericJsonDeserializer<T> extends JsonDeserializer<T> {
 
        private JsonFactory jsonFactory = new JsonFactory();
        private ObjectCodec objectCodec = new ObjectMapper();
+       private JsonObjectFactory defaultObjectFactory = new JsonObjectFactoryImpl();
 
        private String typeField = "type";
 
        private List<JsonObjectFactory> objectFactories = new ArrayList<JsonObjectFactory>();
 
+       @SuppressWarnings("unchecked")
        @Override
        public T deserialize(JsonParser parser, DeserializationContext ctxt)
                        throws IOException, JsonProcessingException {
@@ -44,7 +61,7 @@ public class GenericJsonDeserializer<T> extends JsonDeserializer<T> {
                generator.writeTree(root);
                String str = writer.toString();
 
-               if (log.isDebugEnabled())
+               if (log.isTraceEnabled())
                        log.debug("Deserialize object of type=" + type + ", str=" + str);
 
                JsonObjectFactory objectFactory = null;
@@ -56,7 +73,10 @@ public class GenericJsonDeserializer<T> extends JsonDeserializer<T> {
                }
 
                if (objectFactory == null)
-                       throw new ArgeoServerException(
+                       objectFactory = defaultObjectFactory;
+
+               if (objectFactory == null || !objectFactory.supports(type))
+                       throw new ArgeoException(
                                        "Cannot find JSON object factory for type " + type);
 
                return (T) objectFactory.readValue(type, str);