]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3676 add flag to not serialize empty strings to JSON
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 17 Apr 2015 08:29:24 +0000 (10:29 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 17 Apr 2015 14:46:14 +0000 (16:46 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java

index 8d231bcc1a5507816ab39bbb1fd6f89c22336bf5..ccec2fabb6c13eef03bd1d12e89fb45721b75008 100644 (file)
@@ -22,7 +22,6 @@ package org.sonar.api.utils.text;
 import org.sonar.api.utils.DateUtils;
 
 import javax.annotation.Nullable;
-
 import java.io.Writer;
 import java.util.Date;
 import java.util.Map;
@@ -30,7 +29,7 @@ import java.util.Map;
 /**
  * Writes JSON as a stream. This class allows plugins to not directly depend
  * on the underlying JSON library.
- * <p/>
+ * <p>
  * <h3>How to use</h3>
  * <pre>
  *   StringWriter json = new StringWriter();
@@ -48,17 +47,28 @@ import java.util.Map;
  *     .endObject()
  *     .close();
  * </pre>
+ * </p>
+ * <p>
+ * By default, null objects are not serialized. To enable {@code null} serialization,
+ * use {@link #setSerializeNulls(boolean)}.
+ * </p>
+ * <p>
+ * By default, emptry strings are serialized. To disable empty string serialization,
+ * use {@link #setSerializeEmptys(boolean)}.
+ * </p>
  *
  * @since 4.2
  */
 public class JsonWriter {
 
   private final com.google.gson.stream.JsonWriter stream;
+  private boolean serializeEmptyStrings;
 
   private JsonWriter(Writer writer) {
     this.stream = new com.google.gson.stream.JsonWriter(writer);
     this.stream.setSerializeNulls(false);
     this.stream.setLenient(false);
+    this.serializeEmptyStrings = true;
   }
 
   // for unit testing
@@ -75,6 +85,14 @@ public class JsonWriter {
     return this;
   }
 
+  /**
+   * Enable/disable serialization of properties which value is an empty String.
+   */
+  public JsonWriter setSerializeEmptys(boolean serializeEmptyStrings) {
+    this.serializeEmptyStrings = serializeEmptyStrings;
+    return this;
+  }
+
   /**
    * Begins encoding a new array. Each call to this method must be paired with
    * a call to {@link #endArray}. Output is <code>[</code>.
@@ -178,7 +196,7 @@ public class JsonWriter {
    */
   public JsonWriter value(@Nullable String value) {
     try {
-      stream.value(value);
+      stream.value(serializeEmptyStrings ? value : emptyToNull(value));
       return this;
     } catch (Exception e) {
       throw rethrow(e);
@@ -202,7 +220,7 @@ public class JsonWriter {
         stream.nullValue();
       } else {
         if (value instanceof String) {
-          stream.value((String) value);
+          stream.value(serializeEmptyStrings ? (String) value : emptyToNull((String) value));
         } else if (value instanceof Number) {
           stream.value((Number) value);
         } else if (value instanceof Boolean) {
@@ -210,7 +228,7 @@ public class JsonWriter {
         } else if (value instanceof Date) {
           valueDateTime((Date) value);
         } else if (value instanceof Enum) {
-          stream.value(((Enum)value).name());
+          stream.value(((Enum) value).name());
         } else if (value instanceof Map) {
           stream.beginObject();
           for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
@@ -368,4 +386,12 @@ public class JsonWriter {
     // stacktrace is not helpful
     throw new WriterException("Fail to write JSON: " + e.getMessage());
   }
+
+  @Nullable
+  private static String emptyToNull(@Nullable String value) {
+    if (value == null || value.isEmpty()) {
+      return null;
+    }
+    return value;
+  }
 }
index f0de3de04342518713f93acb3f7787205d3fedfc..e0b945ddbe66a28321a18a79d54b3b55509fdd68 100644 (file)
@@ -39,6 +39,8 @@ import static org.mockito.Mockito.when;
 
 public class JsonWriterTest {
 
+  private static final String EMPTY_STRING = "";
+
   @Rule
   public ExpectedException thrown = ExpectedException.none();
 
@@ -127,6 +129,28 @@ public class JsonWriterTest {
     expect("{\"nullNumber\":null,\"nullString\":null,\"nullNumber\":null,\"nullString\":null,\"nullDate\":null,\"nullDateTime\":null}");
   }
 
+  @Test
+  public void serialize_empty_strings_by_default() throws Exception {
+    writer.beginObject()
+      .prop("emptyString", EMPTY_STRING)
+      .name("emptyStringAsObject").valueObject(EMPTY_STRING)
+      .endObject().close();
+    expect("{" +
+      "\"emptyString\":\"\"," +
+      "\"emptyStringAsObject\":\"\"" +
+      "}");
+  }
+
+  @Test
+  public void ignore_empty_strings_when_requested() throws Exception {
+    writer.setSerializeEmptys(false)
+      .beginObject()
+      .prop("emptyString", EMPTY_STRING)
+      .name("emptyStringAsObject").valueObject(EMPTY_STRING)
+      .endObject().close();
+    expect("{}");
+  }
+
   @Test
   public void escape_values() throws Exception {
     writer.beginObject()