aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2014-05-05 10:22:43 +0200
committerSimon Brandhof <simon.brandhof@gmail.com>2014-05-05 10:22:43 +0200
commit819e6501f876e8d7792d27ab5117d42bcd7671c8 (patch)
treeb79c29f67d292f0e00ab166b4621ea9f19e52728 /sonar-plugin-api
parenta2a2caeb55a26f5966368b5e2b34eced3fc444fb (diff)
downloadsonarqube-819e6501f876e8d7792d27ab5117d42bcd7671c8.tar.gz
sonarqube-819e6501f876e8d7792d27ab5117d42bcd7671c8.zip
Add JsonWriter#valueObject(Object)
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java52
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java33
2 files changed, 84 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 f3b20a91368..5450a63e22a 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
@@ -24,11 +24,12 @@ import org.sonar.api.utils.DateUtils;
import javax.annotation.Nullable;
import java.io.Writer;
import java.util.Date;
+import java.util.Map;
/**
* Writes JSON as a stream. This class allows plugins to not directly depend
* on the underlying JSON library.
- *
+ * <p/>
* <h3>How to use</h3>
* <pre>
* StringWriter json = new StringWriter();
@@ -179,6 +180,55 @@ public class JsonWriter {
}
/**
+ * Encodes an object that can be a :
+ * <ul>
+ * <li>primitive types: String, Number, Boolean</li>
+ * <li>java.util.Date: encoded as datetime (see {@link #valueDateTime(java.util.Date)}</li>
+ * <li><code>Map<Object, Object></code>. Method toString is called for the key.</li>
+ * <li>Iterable</li>
+ * </ul>
+ *
+ * @throws org.sonar.api.utils.text.WriterException on any failure
+ */
+ public JsonWriter valueObject(@Nullable Object value) {
+ try {
+ if (value == null) {
+ stream.nullValue();
+ } else {
+ if (value instanceof String) {
+ stream.value((String) value);
+ } else if (value instanceof Number) {
+ stream.value((Number) value);
+ } else if (value instanceof Boolean) {
+ stream.value((Boolean) value);
+ } else if (value instanceof Date) {
+ valueDateTime((Date) value);
+ } else if (value instanceof Map) {
+ stream.beginObject();
+ for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) value).entrySet()) {
+ stream.name(entry.getKey().toString());
+ valueObject(entry.getValue());
+ }
+ stream.endObject();
+ } else if (value instanceof Iterable) {
+ stream.beginArray();
+ for (Object o : (Iterable<Object>) value) {
+ valueObject(o);
+ }
+ stream.endArray();
+ } else {
+ throw new IllegalArgumentException(getClass() + " does not support encoding of type: " + value.getClass());
+ }
+ }
+ return this;
+ } catch (IllegalArgumentException e) {
+ throw e;
+ } catch (Exception e) {
+ throw rethrow(e);
+ }
+ }
+
+ /**
* Write a list of values in an array, for example:
* <pre>
* writer.beginArray().values(myValues).endArray();
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 d5f2cb7b429..41ae3399029 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
@@ -19,6 +19,7 @@
*/
package org.sonar.api.utils.text;
+import com.google.common.collect.ImmutableMap;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
@@ -28,9 +29,11 @@ import java.io.IOException;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Date;
+import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import static org.fest.assertions.Assertions.assertThat;
+import static org.fest.assertions.Fail.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -116,7 +119,37 @@ public class JsonWriterTest {
.prop("foo", "<hello \"world\">")
.endObject().close();
expect("{\"foo\":\"<hello \\\"world\\\">\"}");
+ }
+
+ @Test
+ public void valueObject() throws Exception {
+ writer.beginObject()
+ .name("aString").valueObject("stringValue")
+ .name("aBoolean").valueObject(true)
+ .name("aInt").valueObject(42)
+ .name("aFloat").valueObject(3.14)
+ .name("aLong").valueObject(42L)
+ .name("aList").valueObject(Arrays.asList("one", 2, "three"))
+ .name("aMap").valueObject(ImmutableMap.of("hello", "world", "good", "bye"))
+ .endObject().close();
+ expect("{\"aString\":\"stringValue\",\"aBoolean\":true,\"aInt\":42,\"aFloat\":3.14,\"aLong\":42,\"aList\":[\"one\",2,\"three\"],\"aMap\":{\"hello\":\"world\",\"good\":\"bye\"}}");
+ }
+ @Test
+ public void valueObject_recursive() throws Exception {
+ Map map = ImmutableMap.of("a", ImmutableMap.of("b", "c"));
+ writer.valueObject(map).close();
+ expect("{\"a\":{\"b\":\"c\"}}");
+ }
+
+ @Test
+ public void valueObject_unsupported_type() throws Exception {
+ try {
+ writer.beginObject().valueObject(new StringWriter()).endObject().close();
+ fail();
+ } catch (IllegalArgumentException e) {
+ assertThat(e).hasMessage("class org.sonar.api.utils.text.JsonWriter does not support encoding of type: class java.io.StringWriter");
+ }
}
@Test