com.github.jknack.handlebars.Jackson2Helper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of handlebars-jackson2 Show documentation
Show all versions of handlebars-jackson2 Show documentation
A JSON Helper base on Jackson2
/**
* Copyright (c) 2012-2015 Edgar Espina
*
* This file is part of Handlebars.java.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.jknack.handlebars;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.SerializableString;
import com.fasterxml.jackson.core.io.CharacterEscapes;
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import static java.util.Objects.requireNonNull;
/**
* A Jackson 2.x helper.
*
* Basic usage:
*
*
*
* Handlebars hbs = new Handlebars();
*
* hbs.registerHelper("json", Jackson2Helper.INSTANCE);
*
* ...
*
* {{json model}}
*
*
* If model
is null an empty string is returned.
*
*
* You can change this using the default
option:
*
*
*
* {{json model default="{}"}}
*
*
*
* Using a view class:
*
*
*
* {{json model view="foo.MyView"}}
*
*
* Using alias for views:
*
*
*
* {{json model view="myView"}}
*
*
*
* Escape HTML chars:
*
*
*
* {{json model escapeHtml=true}}
*
*
*
* Pretty printer:
*
*
*
* {{json model pretty=true}}
*
*
* @author edgar.espina
* @since 0.4.0
*/
public class Jackson2Helper implements Helper
© 2015 - 2025 Weber Informatics LLC | Privacy Policy