From: Sébastien Lesaint
Date: Fri, 17 Apr 2015 08:29:24 +0000 (+0200)
Subject: SONAR-3676 add flag to not serialize empty strings to JSON
X-Git-Tag: 5.2-RC1~2219
X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=62ad128a8caeae516ccbbe6d92547a3ff213a6eb;p=sonarqube.git
SONAR-3676 add flag to not serialize empty strings to JSON
---
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.
- *
+ *
*
How to use
*
* StringWriter json = new StringWriter();
@@ -48,17 +47,28 @@ import java.util.Map;
* .endObject()
* .close();
*
+ *
+ *
+ * By default, null objects are not serialized. To enable {@code null} serialization,
+ * use {@link #setSerializeNulls(boolean)}.
+ *
+ *
+ * By default, emptry strings are serialized. To disable empty string serialization,
+ * use {@link #setSerializeEmptys(boolean)}.
+ *
*
* @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 [
.
@@ -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