From 62ad128a8caeae516ccbbe6d92547a3ff213a6eb Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Fri, 17 Apr 2015 10:29:24 +0200 Subject: [PATCH] SONAR-3676 add flag to not serialize empty strings to JSON --- .../org/sonar/api/utils/text/JsonWriter.java | 36 ++++++++++++++++--- .../sonar/api/utils/text/JsonWriterTest.java | 24 +++++++++++++ 2 files changed, 55 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. - *

+ *

*

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 entry : ((Map) 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; + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java index f0de3de0434..e0b945ddbe6 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java @@ -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() -- 2.39.5