class Jackson2Helper implements Helper<Object> {

  @Override
  public Object apply(final Object context, final Options options)
      throws IOException {
    if (context == null) {
      return options.hash("default", "");
    }
    String viewName = options.hash("view", "");
    JsonGenerator generator = null;
    try {
      final ObjectWriter writer;
      // do we need to use a view?
      if (!Handlebars.Utils.isEmpty(viewName)) {
        Class<?> viewClass = alias.get(viewName);
        if (viewClass == null) {
          viewClass = getClass().getClassLoader().loadClass(viewName);
        }
        writer = mapper.writerWithView(viewClass);
      } else {
        writer = mapper.writer();
      }
      JsonFactory jsonFactory = mapper.getFactory();

      SegmentedStringWriter output = new SegmentedStringWriter(jsonFactory._getBufferRecycler());

      // creates a json generator.
      generator = jsonFactory.createJsonGenerator(output);

      Boolean escapeHtml = options.hash("escapeHTML", Boolean.FALSE);
      // do we need to escape html?
      if (escapeHtml) {
        generator.setCharacterEscapes(new HtmlEscapes());
      }

      Boolean pretty = options.hash("pretty", Boolean.FALSE);

      // write the JSON output.
      if (pretty) {
        writer.withDefaultPrettyPrinter().writeValue(generator, context);
      } else {
        writer.writeValue(generator, context);
      }

      generator.close();

      return new Handlebars.SafeString(output.getAndClear());
    } catch (ClassNotFoundException ex) {
      throw new IllegalArgumentException(viewName, ex);
    } finally {
      if (generator != null && !generator.isClosed()) {
        generator.close();
      }
    }
  }

  public  Jackson2Helper(final ObjectMapper objectMapper);
  private  Jackson2Helper();

}