aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java42
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java8
2 files changed, 49 insertions, 1 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 b5d1ebb1d96..2ba0281f0f4 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
@@ -19,8 +19,11 @@
*/
package org.sonar.api.utils.text;
+import org.sonar.api.utils.DateUtils;
+
import javax.annotation.Nullable;
import java.io.Writer;
+import java.util.Date;
/**
* @since 4.2
@@ -152,6 +155,27 @@ public class JsonWriter {
/**
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
+ public JsonWriter valueDate(@Nullable Date value) {
+ try {
+ stream.value(value==null ? null : DateUtils.formatDate(value));
+ return this;
+ } catch (Exception e) {
+ throw rethrow(e);
+ }
+ }
+
+ public JsonWriter valueDateTime(@Nullable Date value) {
+ try {
+ stream.value(value==null ? null : DateUtils.formatDateTime(value));
+ return this;
+ } catch (Exception e) {
+ throw rethrow(e);
+ }
+ }
+
+ /**
+ * @throws org.sonar.api.utils.text.WriterException on any failure
+ */
public JsonWriter value(long value) {
try {
stream.value(value);
@@ -182,6 +206,24 @@ public class JsonWriter {
}
/**
+ * Encodes the property name and date value (ISO format).
+ * Output is for example <code>"theDate":"2013-01-24"</code>.
+ * @throws org.sonar.api.utils.text.WriterException on any failure
+ */
+ public JsonWriter propDate(String name, @Nullable Date value) {
+ return name(name).valueDate(value);
+ }
+
+ /**
+ * Encodes the property name and datetime value (ISO format).
+ * Output is for example <code>"theDate":"2013-01-24T13:12:45+01"</code>.
+ * @throws org.sonar.api.utils.text.WriterException on any failure
+ */
+ public JsonWriter propDateTime(String name, @Nullable Date value) {
+ return name(name).valueDateTime(value);
+ }
+
+ /**
* @throws org.sonar.api.utils.text.WriterException on any failure
*/
public JsonWriter prop(String name, @Nullable String 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 4fe227d8be4..dbea3ac2ce3 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
@@ -22,11 +22,13 @@ package org.sonar.api.utils.text;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.api.utils.text.WriterException;
import java.io.IOException;
import java.io.StringWriter;
+import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;
import static org.fest.assertions.Assertions.assertThat;
@@ -77,6 +79,7 @@ public class JsonWriterTest {
@Test
public void type_of_values() throws Exception {
+ Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100");
writer.beginObject()
.prop("aBoolean", true)
.prop("aInt", 123)
@@ -84,8 +87,9 @@ public class JsonWriterTest {
.prop("aDouble", 3.14)
.prop("aNumber", new AtomicInteger(123456789))
.prop("aString", "bar")
+ .propDate("aDate", date)
.endObject().close();
- expect("{\"aBoolean\":true,\"aInt\":123,\"aLong\":1000,\"aDouble\":3.14,\"aNumber\":123456789,\"aString\":\"bar\"}");
+ expect("{\"aBoolean\":true,\"aInt\":123,\"aLong\":1000,\"aDouble\":3.14,\"aNumber\":123456789,\"aString\":\"bar\",\"aDate\":\"2010-05-18\"}");
}
@Test
@@ -95,6 +99,8 @@ public class JsonWriterTest {
.prop("nullString", (String) null)
.name("nullNumber").value((Number) null)
.name("nullString").value((String) null)
+ .name("nullDate").valueDate(null)
+ .name("nullDateTime").valueDate(null)
.endObject().close();
expect("{}");
}