diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-02-02 18:49:14 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-02-05 21:52:04 +0100 |
commit | 937824b05e114fa21af572e24e36eeeda29842ef (patch) | |
tree | 689890784c603bd8d756280e0d75916f07d031cb /sonar-plugin-api | |
parent | 2c0f324f86c3c6b2e7eb45f363f12e0fa511541f (diff) | |
download | sonarqube-937824b05e114fa21af572e24e36eeeda29842ef.tar.gz sonarqube-937824b05e114fa21af572e24e36eeeda29842ef.zip |
Improve doc and usages of JsonWriter
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/text/JsonWriter.java | 38 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/utils/text/JsonWriterTest.java | 9 |
2 files changed, 26 insertions, 21 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 a098a2388a4..98a84dbc8b4 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 @@ -32,30 +32,34 @@ import org.sonar.api.utils.DateUtils; * <p> * <h3>How to use</h3> * <pre> - * StringWriter json = new StringWriter(); - * JsonWriter writer = JsonWriter.of(json); - * writer - * .beginObject() - * .prop("aBoolean", true) - * .prop("aInt", 123) - * .prop("aString", "foo") - * .beginObject().name("aList") - * .beginArray() - * .beginObject().prop("key", "ABC").endObject() - * .beginObject().prop("key", "DEF").endObject() - * .endArray() - * .endObject() - * .close(); + * try (JsonWriter jsonWriter = JsonWriter.of(writer)) { + * jsonWriter + * .beginObject() + * .prop("aBoolean", true) + * .prop("aInt", 123) + * .prop("aString", "foo") + * .beginObject().name("aList") + * .beginArray() + * .beginObject().prop("key", "ABC").endObject() + * .beginObject().prop("key", "DEF").endObject() + * .endArray() + * .endObject() + * } * </pre> * * <p> * By default, null objects are not serialized. To enable {@code null} serialization, * use {@link #setSerializeNulls(boolean)}. - * + * </p> * <p> - * By default, emptry strings are serialized. To disable empty string serialization, + * By default, empty strings are serialized. To disable empty string serialization, * use {@link #setSerializeEmptys(boolean)}. - * + * </p> + * <p> + * {@link JsonWriter} implements {@link AutoCloseable} since version 6.3. The + * method {@link #close()} closes the underlying writer. + * </p> + * * * @since 4.2 */ 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 0ed59a44c00..97ce288e814 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 @@ -43,8 +43,8 @@ public class JsonWriterTest { @Rule public ExpectedException thrown = ExpectedException.none(); - StringWriter json = new StringWriter(); - JsonWriter writer = JsonWriter.of(json); + private StringWriter json = new StringWriter(); + private JsonWriter writer = JsonWriter.of(json); private void expect(String s) { assertThat(json.toString()).isEqualTo(s); @@ -170,7 +170,8 @@ public class JsonWriterTest { .name("anEnum").valueObject(ColorEnum.GREEN) .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\"],\"anEnum\":\"GREEN\",\"aMap\":{\"hello\":\"world\",\"good\":\"bye\"}}"); + expect( + "{\"aString\":\"stringValue\",\"aBoolean\":true,\"aInt\":42,\"aFloat\":3.14,\"aLong\":42,\"aList\":[\"one\",2,\"three\"],\"anEnum\":\"GREEN\",\"aMap\":{\"hello\":\"world\",\"good\":\"bye\"}}"); } @Test @@ -212,7 +213,7 @@ public class JsonWriterTest { new JsonWriter(gson).beginArray(); } - private static enum ColorEnum { + private enum ColorEnum { RED, GREEN } } |