]> source.dussan.org Git - sonarqube.git/commitdiff
Add JsonWriter#values(Iterable<String>)
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 29 Apr 2014 22:39:48 +0000 (00:39 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Tue, 29 Apr 2014 22:39:48 +0000 (00:39 +0200)
sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java
sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java

index 6d929a28daafed899967b7f66c05fd0339dfcd85..f3b20a91368477605efe16fdd8aa8412c9d93ece 100644 (file)
@@ -178,6 +178,21 @@ public class JsonWriter {
     }
   }
 
+  /**
+   * Write a list of values in an array, for example:
+   * <pre>
+   *   writer.beginArray().values(myValues).endArray();
+   * </pre>
+   *
+   * @throws org.sonar.api.utils.text.WriterException on any failure
+   */
+  public JsonWriter values(Iterable<String> values) {
+    for (String value : values) {
+      value(value);
+    }
+    return this;
+  }
+
   /**
    * @throws org.sonar.api.utils.text.WriterException on any failure
    */
index ea9de6560d5e36833e073bbb5b2230567086af30..d5f2cb7b429a000e8082d4c321d140d35834359b 100644 (file)
@@ -26,6 +26,7 @@ import org.sonar.api.utils.DateUtils;
 
 import java.io.IOException;
 import java.io.StringWriter;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -75,6 +76,12 @@ public class JsonWriterTest {
     expect("{\"issues\":[{\"key\":\"ABC\"},{\"key\":\"DEF\"}]}");
   }
 
+  @Test
+  public void array_values() throws Exception {
+    writer.beginArray().values(Arrays.asList("foo", "bar", "baz")).endArray().close();
+    expect("[\"foo\",\"bar\",\"baz\"]");
+  }
+
   @Test
   public void type_of_values() throws Exception {
     Date date = DateUtils.parseDateTime("2010-05-18T15:50:45+0100");