cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t..@apache.org
Subject [39/39] git commit: updated refs/heads/bvt to f428608
Date Wed, 27 Mar 2013 13:32:49 GMT
Exclusion: Api Serializer excludes fields when empty

Exclude fields that are empty in the JSON response.

Signed-off-by: Prasanna Santhanam <tsp@apache.org>


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

Branch: refs/heads/bvt
Commit: f4286088ba106dc57d47bd97a60e4809bccc6959
Parents: 1e12bc9
Author: Prasanna Santhanam <tsp@apache.org>
Authored: Wed Mar 27 19:01:10 2013 +0530
Committer: Prasanna Santhanam <tsp@apache.org>
Committed: Wed Mar 27 19:01:10 2013 +0530

----------------------------------------------------------------------
 .../cloud/api/response/ApiResponseSerializer.java  |   36 ++++++--------
 .../api/response/EmptyFieldExclusionStrategy.java  |   40 +++++++++++++++
 2 files changed, 55 insertions(+), 21 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f4286088/server/src/com/cloud/api/response/ApiResponseSerializer.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/response/ApiResponseSerializer.java b/server/src/com/cloud/api/response/ApiResponseSerializer.java
index 3b1d9a6..4eaf3d5 100644
--- a/server/src/com/cloud/api/response/ApiResponseSerializer.java
+++ b/server/src/com/cloud/api/response/ApiResponseSerializer.java
@@ -16,33 +16,26 @@
 // under the License.
 package com.cloud.api.response;
 
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Date;
-import java.util.List;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
-
-import org.apache.cloudstack.api.response.ListResponse;
-import org.apache.cloudstack.api.response.*;
-import org.apache.log4j.Logger;
-
-import org.apache.cloudstack.api.ApiConstants;
 import com.cloud.api.ApiDBUtils;
 import com.cloud.api.ApiResponseGsonHelper;
 import com.cloud.api.ApiServer;
-import org.apache.cloudstack.api.BaseCmd;
-import org.apache.cloudstack.api.ResponseObject;
 import com.cloud.utils.encoding.URLEncoder;
 import com.cloud.utils.exception.CloudRuntimeException;
-import com.cloud.uuididentity.dao.IdentityDao;
-import com.cloud.uuididentity.dao.IdentityDaoImpl;
+import com.google.gson.ExclusionStrategy;
 import com.google.gson.Gson;
 import com.google.gson.annotations.SerializedName;
+import org.apache.cloudstack.api.ApiConstants;
+import org.apache.cloudstack.api.BaseCmd;
+import org.apache.cloudstack.api.ResponseObject;
+import org.apache.cloudstack.api.response.*;
+import org.apache.log4j.Logger;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.*;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 public class ApiResponseSerializer {
     private static final Logger s_logger = Logger.getLogger(ApiResponseSerializer.class.getName());
@@ -69,7 +62,8 @@ public class ApiResponseSerializer {
 
     public static String toJSONSerializedString(ResponseObject result) {
         if (result != null) {
-            Gson gson = ApiResponseGsonHelper.getBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).create();
+            ExclusionStrategy excludes = new EmptyFieldExclusionStrategy();
+            Gson gson = ApiResponseGsonHelper.getBuilder().excludeFieldsWithModifiers(Modifier.TRANSIENT).setExclusionStrategies(excludes).create();
 
             StringBuilder sb = new StringBuilder();
 

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f4286088/server/src/com/cloud/api/response/EmptyFieldExclusionStrategy.java
----------------------------------------------------------------------
diff --git a/server/src/com/cloud/api/response/EmptyFieldExclusionStrategy.java b/server/src/com/cloud/api/response/EmptyFieldExclusionStrategy.java
new file mode 100644
index 0000000..3099d83
--- /dev/null
+++ b/server/src/com/cloud/api/response/EmptyFieldExclusionStrategy.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 com.cloud.api.response;
+
+import com.cloud.serializer.Param;
+import com.google.gson.ExclusionStrategy;
+import com.google.gson.FieldAttributes;
+
+public class EmptyFieldExclusionStrategy implements ExclusionStrategy {
+
+    @Override
+    public boolean shouldSkipField(FieldAttributes fieldAttributes) {
+        if (fieldAttributes.getAnnotation(Param.class) != null) {
+                return true;
+        }
+        return false;
+    }
+
+    @Override
+    public boolean shouldSkipClass(Class<?> aClass) {
+        return false;
+    }
+}


Mime
View raw message