Code example for StringWriter

Methods: flushtoString

  public static <T>String toJsonString(T bean, boolean pretty)
         throws JsonGenerationException, JsonMappingException, IOException 
    JsonFactory jsonFactory = new JsonFactory(new ObjectMapper());
    StringWriter out = new StringWriter(1000);
    JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(out);
    if (pretty) jsonGenerator.useDefaultPrettyPrinter();
    return out.toString();
   * Serializes a bean as JSON. This method will not throw an exception if the serialization fails 
   * but it will instead return the string {@link #JSON_SERIALIZATION_FAILURE_STRING} 
   * @param <T>     the bean type 
   * @param bean    the bean to serialize 
   * @param pretty  a flag if the output is to be pretty printed 
Contextual code suggestions in your IDE  Get Codota for Java