aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-04-17 10:29:24 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-04-17 16:46:14 +0200
commit62ad128a8caeae516ccbbe6d92547a3ff213a6eb (patch)
tree1ee68d5aeddf732a156428d53eddb4d312471b63 /sonar-plugin-api/src/main
parent58495187b8991249e3c203430178c21f101bb115 (diff)
downloadsonarqube-62ad128a8caeae516ccbbe6d92547a3ff213a6eb.tar.gz
sonarqube-62ad128a8caeae516ccbbe6d92547a3ff213a6eb.zip
SONAR-3676 add flag to not serialize empty strings to JSON
Diffstat (limited to 'sonar-plugin-api/src/main')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java36
1 files changed, 31 insertions, 5 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
index 8d231bcc1a5..ccec2fabb6c 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
@@ -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
@@ -76,6 +86,14 @@ public class JsonWriter {
}
/**
+ * 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;
+ }
}