You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2014/10/27 15:59:31 UTC

[2/3] git commit: ISIS-934: only pretty print JSON representations if in prototype mode.

ISIS-934: only pretty print JSON representations if in prototype mode.


Project: http://git-wip-us.apache.org/repos/asf/isis/repo
Commit: http://git-wip-us.apache.org/repos/asf/isis/commit/e7a81573
Tree: http://git-wip-us.apache.org/repos/asf/isis/tree/e7a81573
Diff: http://git-wip-us.apache.org/repos/asf/isis/diff/e7a81573

Branch: refs/heads/master
Commit: e7a81573359eba5f598b1c6baa0684f90b0b3f17
Parents: 3569b5e
Author: Dan Haywood <da...@haywood-associates.co.uk>
Authored: Mon Oct 27 12:33:15 2014 +0000
Committer: Dan Haywood <da...@haywood-associates.co.uk>
Committed: Mon Oct 27 12:33:15 2014 +0000

----------------------------------------------------------------------
 .../restfulobjects/applib/util/JsonMapper.java  | 77 +++++++++-----------
 .../rendering/util/JsonWriterUtil.java          |  9 ++-
 2 files changed, 43 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/isis/blob/e7a81573/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/JsonMapper.java
----------------------------------------------------------------------
diff --git a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/JsonMapper.java b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/JsonMapper.java
index ee03862..7f130a5 100644
--- a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/JsonMapper.java
+++ b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/JsonMapper.java
@@ -24,36 +24,25 @@ import java.util.ArrayList;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
-
+import java.util.concurrent.ConcurrentHashMap;
 import javax.ws.rs.core.Response;
-
-import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
-import org.codehaus.jackson.JsonGenerationException;
-import org.codehaus.jackson.JsonGenerator;
-import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.JsonParseException;
-import org.codehaus.jackson.JsonParser;
-import org.codehaus.jackson.JsonProcessingException;
-import org.codehaus.jackson.Version;
-import org.codehaus.jackson.map.BeanProperty;
-import org.codehaus.jackson.map.DeserializationConfig;
-import org.codehaus.jackson.map.DeserializationContext;
-import org.codehaus.jackson.map.DeserializerProvider;
-import org.codehaus.jackson.map.JsonDeserializer;
-import org.codehaus.jackson.map.JsonMappingException;
-import org.codehaus.jackson.map.JsonSerializer;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.map.SerializationConfig;
-import org.codehaus.jackson.map.SerializerProvider;
+import org.codehaus.jackson.*;
+import org.codehaus.jackson.map.*;
 import org.codehaus.jackson.map.deser.BeanDeserializerFactory;
 import org.codehaus.jackson.map.deser.JsonNodeDeserializer;
 import org.codehaus.jackson.map.deser.StdDeserializerProvider;
 import org.codehaus.jackson.map.module.SimpleModule;
 import org.codehaus.jackson.type.JavaType;
 import org.jboss.resteasy.client.ClientResponse;
+import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
 
 public final class JsonMapper {
 
+    public enum PrettyPrinting {
+        ENABLE,
+        DISABLE
+    }
+
     /**
      * Provides polymorphic deserialization to any subtype of
      * {@link JsonRepresentation}.
@@ -107,7 +96,7 @@ public final class JsonMapper {
         }
     }
 
-    private static ObjectMapper createObjectMapper() {
+    private static ObjectMapper createObjectMapper(PrettyPrinting prettyPrinting) {
         // it's a shame that the serialization and deserialization mechanism
         // used aren't symmetric... but it works.
         final DeserializerProvider deserializerProvider = new StdDeserializerProvider(new JsonRepresentationDeserializerFactory());
@@ -116,22 +105,38 @@ public final class JsonMapper {
         jsonModule.addSerializer(JsonRepresentation.class, new JsonRepresentationSerializer());
         objectMapper.registerModule(jsonModule);
 
-        objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
+        if(prettyPrinting == PrettyPrinting.ENABLE) {
+            objectMapper.configure(SerializationConfig.Feature.INDENT_OUTPUT, true);
+        }
         objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
         return objectMapper;
     }
 
-    private static JsonMapper instance = new JsonMapper();
+    private static Map<PrettyPrinting, JsonMapper> instanceByConfig = new ConcurrentHashMap();
 
-    // threadsafe
+    /**
+     * Returns a {@link org.apache.isis.viewer.restfulobjects.applib.util.JsonMapper.PrettyPrinting#ENABLE pretty-printing enabled} JSON mapper.
+     */
     public final static JsonMapper instance() {
-        return instance;
+        return instance(PrettyPrinting.ENABLE);
+    }
+
+    public final static JsonMapper instance(final PrettyPrinting prettyPrinting) {
+        final JsonMapper jsonMapper = instanceByConfig.get(prettyPrinting);
+        if (jsonMapper != null) {
+            return jsonMapper;
+        }
+        // there could be a race-condition here, but it doesn't matter; last one wins.
+        final JsonMapper mapper = new JsonMapper(prettyPrinting);
+        instanceByConfig.put(prettyPrinting, mapper);
+
+        return mapper;
     }
 
     private final ObjectMapper objectMapper;
 
-    private JsonMapper() {
-        objectMapper = createObjectMapper();
+    private JsonMapper(PrettyPrinting prettyPrinting) {
+        objectMapper = createObjectMapper(prettyPrinting);
     }
 
     @SuppressWarnings("unchecked")
@@ -152,23 +157,11 @@ public final class JsonMapper {
     }
 
     public <T> T read(final Response response, final Class<T> requiredType) throws JsonParseException, JsonMappingException, IOException {
-        final ClientResponse<?> clientResponse = (ClientResponse<?>) response; // a
-                                                                               // shame,
-                                                                               // but
-                                                                               // needed
-                                                                               // if
-                                                                               // calling
-                                                                               // resources
-                                                                               // directly
-        final Object entityObj = clientResponse.getEntity(String.class);
-        if (entityObj == null) {
+        final ClientResponse<?> clientResponse = (ClientResponse<?>) response; // a shame, but needed if calling resources directly.
+        final String entity = clientResponse.getEntity(String.class);
+        if (entity == null) {
             return null;
         }
-        if (!(entityObj instanceof String)) {
-            throw new IllegalArgumentException("response entity must be a String (was " + entityObj.getClass().getName() + ")");
-        }
-        final String entity = (String) entityObj;
-
         return read(entity, requiredType);
     }
 

http://git-wip-us.apache.org/repos/asf/isis/blob/e7a81573/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/util/JsonWriterUtil.java
----------------------------------------------------------------------
diff --git a/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/util/JsonWriterUtil.java b/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/util/JsonWriterUtil.java
index df90789..51ea87b 100644
--- a/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/util/JsonWriterUtil.java
+++ b/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/util/JsonWriterUtil.java
@@ -21,6 +21,8 @@ package org.apache.isis.viewer.restfulobjects.rendering.util;
 import java.io.IOException;
 import org.codehaus.jackson.JsonGenerationException;
 import org.codehaus.jackson.map.JsonMappingException;
+import org.apache.isis.core.runtime.system.DeploymentType;
+import org.apache.isis.core.runtime.system.context.IsisContext;
 import org.apache.isis.viewer.restfulobjects.applib.util.JsonMapper;
 
 public final class JsonWriterUtil {
@@ -28,8 +30,9 @@ public final class JsonWriterUtil {
     private JsonWriterUtil(){}
 
     public static String jsonFor(final Object object) {
+        final JsonMapper.PrettyPrinting prettyPrinting = inferPrettyPrinting(IsisContext.getDeploymentType());
         try {
-            return JsonMapper.instance().write(object);
+            return JsonMapper.instance(prettyPrinting).write(object);
         } catch (final JsonGenerationException e) {
             throw new RuntimeException(e);
         } catch (final JsonMappingException e) {
@@ -38,4 +41,8 @@ public final class JsonWriterUtil {
             throw new RuntimeException(e);
         }
     }
+
+    private static JsonMapper.PrettyPrinting inferPrettyPrinting(final DeploymentType deploymentType) {
+        return deploymentType.isProduction() ? JsonMapper.PrettyPrinting.DISABLE : JsonMapper.PrettyPrinting.ENABLE;
+    }
 }