### Question:
JsonWriter { @Nonnull public String writeAsString (@Nonnull final IJson aJson) { ValueEnforcer.notNull (aJson, "Json"); try (final NonBlockingStringWriter aWriter = new NonBlockingStringWriter (1024)) { writeToWriter (aJson, aWriter); return aWriter.getAsString (); } catch (final IOException ex) { throw new IllegalStateException ("NonBlockingStringWriter should never throw IOException!", ex); } } JsonWriter();  JsonWriter(@Nonnull final IJsonWriterSettings aSettings); @Nonnull @ReturnsMutableCopy IJsonWriterSettings getSettings(); void writeToWriter(@Nonnull final IJson aJson, @Nonnull @WillNotClose final Writer aWriter); void writeToWriterAndClose(@Nonnull final IJson aJson, @Nonnull @WillClose final Writer aWriter); @Nonnull String writeAsString(@Nonnull final IJson aJson); void writeToStream(@Nonnull final IJson aJson,
                             @Nonnull @WillNotClose final OutputStream aOS,
                             @Nonnull final Charset aCharset); void writeToStreamAndClose(@Nonnull final IJson aJson,
                                     @Nonnull @WillClose final OutputStream aOS,
                                     @Nonnull final Charset aCharset); @Nonnull byte [] writeAsByteArray(@Nonnull final IJson aJson, @Nonnull final Charset aCharset);  }### Answer:
@Test public void testIndent () { final String sCRLF = JsonWriterSettings.DEFAULT_NEWLINE_STRING; final JsonWriter aWriter = new JsonWriter (JsonWriterSettings.DEFAULT_SETTINGS_FORMATTED); assertEquals ("{}", aWriter.writeAsString (new JsonObject ())); assertEquals ("{" + sCRLF + " \"foo\":\"bar\"" + sCRLF + "}", aWriter.writeAsString (new JsonObject ().add ("foo", "bar"))); assertEquals ("{" + sCRLF + " \"foo\":[" + sCRLF + " 1," + sCRLF + " 2" + sCRLF + " ]" + sCRLF + "}", aWriter.writeAsString (new JsonObject ().addJson ("foo", new JsonArray ().add (1).add (2)))); assertEquals ("{" + sCRLF + " \"foo\":{" + sCRLF + " \"bar\":\"baz\"" + sCRLF + " }" + sCRLF + "}", aWriter.writeAsString (new JsonObject ().addJson ("foo", new JsonObject ().add ("bar", "baz")))); assertEquals ("[]", aWriter.writeAsString (new JsonArray ())); assertEquals ("[" + sCRLF + " {" + sCRLF + " \"foo\":\"bar\"" + sCRLF + " }" + sCRLF + "]", aWriter.writeAsString (new JsonArray ().add (new JsonObject ().add ("foo", "bar")))); }
@Test public void testWriteNewlineAtEnd () { final String sCRLF = JsonWriterSettings.DEFAULT_NEWLINE_STRING; assertEquals ("{\"foo\":\"bar\"}", new JsonWriter ().writeAsString (new JsonObject ().add ("foo", "bar"))); assertEquals ("{\"foo\":\"bar\"}", new JsonWriter (new JsonWriterSettings ().setWriteNewlineAtEnd (false)).writeAsString (new JsonObject ().add ("foo", "bar"))); assertEquals ("{\"foo\":\"bar\"}" + sCRLF, new JsonWriter (new JsonWriterSettings ().setWriteNewlineAtEnd (true)).writeAsString (new JsonObject ().add ("foo", "bar"))); }