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 
Stop searching for code, let great code find you!  Add Codota to your java IDE