Skip to content

Commit

Permalink
java modules: convert missing jackson module
Browse files Browse the repository at this point in the history
- rename jackson module
  • Loading branch information
edgar-espina-wpp committed Mar 4, 2024
1 parent 0864c16 commit d8ff863
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 51 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion handlebars-jackson2/pom.xml → handlebars-jackson/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>

<modelVersion>4.0.0</modelVersion>
<artifactId>handlebars-jackson2</artifactId>
<artifactId>handlebars-jackson</artifactId>

<name>JSON Jackson helpers</name>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

import static java.util.Objects.requireNonNull;

Expand All @@ -18,6 +18,9 @@
import com.fasterxml.jackson.core.io.SegmentedStringWriter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Helper;
import com.github.jknack.handlebars.Options;

/**
* A Jackson 2.x helper.
Expand Down Expand Up @@ -69,7 +72,7 @@
* @author edgar.espina
* @since 0.4.0
*/
public class Jackson2Helper implements Helper<Object> {
public class JacksonHelper implements Helper<Object> {

/**
* Escape HTML chars from JSON content. See
Expand Down Expand Up @@ -106,8 +109,8 @@ public SerializableString getEscapeSequence(final int ch) {
}
}

/** A singleton version of {@link Jackson2Helper}. */
public static final Helper<Object> INSTANCE = new Jackson2Helper();
/** A singleton version of {@link JacksonHelper}. */
public static final Helper<Object> INSTANCE = new JacksonHelper();

/** The JSON parser. */
private final ObjectMapper mapper;
Expand All @@ -116,16 +119,16 @@ public SerializableString getEscapeSequence(final int ch) {
private final Map<String, Class<?>> alias = new HashMap<String, Class<?>>();

/**
* Creates a new {@link Jackson2Helper}.
* Creates a new {@link JacksonHelper}.
*
* @param objectMapper The object's mapper. Required.
*/
public Jackson2Helper(final ObjectMapper objectMapper) {
public JacksonHelper(final ObjectMapper objectMapper) {
mapper = requireNonNull(objectMapper, "The object mapper is required.");
}

/** Creates a new {@link Jackson2Helper}. */
private Jackson2Helper() {
/** Creates a new {@link JacksonHelper}. */
private JacksonHelper() {
this(new ObjectMapper());
}

Expand Down Expand Up @@ -189,7 +192,7 @@ public Object apply(final Object context, final Options options) throws IOExcept
* @param viewClass The view class. Required.
* @return This helper.
*/
public Jackson2Helper viewAlias(final String alias, final Class<?> viewClass) {
public JacksonHelper viewAlias(final String alias, final Class<?> viewClass) {
this.alias.put(
requireNonNull(alias, "A view alias is required."),
requireNonNull(viewClass, "A view class is required."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

import java.util.AbstractMap;
import java.util.Collections;
Expand All @@ -27,6 +27,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.POJONode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.github.jknack.handlebars.ValueResolver;

/**
* Resolve a context stack value from {@link JsonNode}.
Expand Down
8 changes: 8 additions & 0 deletions handlebars-jackson/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module com.github.jknack.handlebars.jackson {
exports com.github.jknack.handlebars.jackson;

requires org.slf4j;
requires com.github.jknack.handlebars;
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
}
12 changes: 12 additions & 0 deletions handlebars-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars-jackson</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
Expand Down Expand Up @@ -56,6 +62,12 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

public class Comment {
private String author;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.context.MapValueResolver;

public class Issue260 extends AbstractTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars.i280;
package com.github.jknack.handlebars.jackson;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.JsonNodeValueResolver;

public class Issue280 extends AbstractTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars.i368;
package com.github.jknack.handlebars.jackson;

import java.io.IOException;

Expand All @@ -13,7 +13,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.JsonNodeValueResolver;
import com.github.jknack.handlebars.context.MapValueResolver;

public class Issue368 extends AbstractTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.context.MapValueResolver;

public class Issue412 extends AbstractTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.AbstractTest;
import com.github.jknack.handlebars.Context;
import com.github.jknack.handlebars.context.MapValueResolver;

public class Issue598 extends AbstractTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
* Copyright (c) 2012 Edgar Espina
*/
package com.github.jknack.handlebars;
package com.github.jknack.handlebars.jackson;

import static com.github.jknack.handlebars.IgnoreWindowsLineMatcher.equalsToStringIgnoringWindowsNewLine;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -17,20 +17,23 @@

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.jknack.handlebars.Blog.Views.Public;
import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.HandlebarsException;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.jackson.Blog.Views.Public;

/**
* Unit test for {@link Jackson2Helper}.
* Unit test for {@link JacksonHelper}.
*
* @author edgar.espina
* @since 0.1.0
*/
public class Jackson2HelperTest {
public class JacksonHelperTest {

@Test
public void toJSON() throws IOException {
Handlebars handlebars = new Handlebars();
handlebars.registerHelper("@json", Jackson2Helper.INSTANCE);
handlebars.registerHelper("@json", JacksonHelper.INSTANCE);

Template template = handlebars.compileInline("{{@json this}}");

Expand All @@ -43,7 +46,7 @@ public void toJSON() throws IOException {
@Test
public void toPrettyJSON() throws IOException {
Handlebars handlebars = new Handlebars();
handlebars.registerHelper("@json", Jackson2Helper.INSTANCE);
handlebars.registerHelper("@json", JacksonHelper.INSTANCE);

Template template = handlebars.compileInline("{{@json this pretty=true}}");

Expand All @@ -61,11 +64,11 @@ public void toPrettyJSON() throws IOException {
public void toJSONViewInclusive() throws IOException {
Handlebars handlebars = new Handlebars();

handlebars.registerHelper("@json", Jackson2Helper.INSTANCE);
handlebars.registerHelper("@json", JacksonHelper.INSTANCE);

Template template =
handlebars.compileInline(
"{{@json this view=\"com.github.jknack.handlebars.Blog$Views$Public\"}}");
"{{@json this view=\"com.github.jknack.handlebars.jackson.Blog$Views$Public\"}}");

assertThat(
template.apply(new Blog("First Post", "...")),
Expand All @@ -80,11 +83,11 @@ public void toJSONViewExclusive() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

handlebars.registerHelper("@json", new Jackson2Helper(mapper));
handlebars.registerHelper("@json", new JacksonHelper(mapper));

Template template =
handlebars.compileInline(
"{{@json this view=\"com.github.jknack.handlebars.Blog$Views$Public\"}}");
"{{@json this view=\"com.github.jknack.handlebars.jackson.Blog$Views$Public\"}}");

assertThat(
template.apply(new Blog("First Post", "...")),
Expand All @@ -98,8 +101,7 @@ public void toJSONAliasViewExclusive() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

handlebars.registerHelper(
"@json", new Jackson2Helper(mapper).viewAlias("myView", Public.class));
handlebars.registerHelper("@json", new JacksonHelper(mapper).viewAlias("myView", Public.class));

Template template = handlebars.compileInline("{{@json this view=\"myView\"}}");

Expand All @@ -118,7 +120,7 @@ public void jsonViewNotFound() throws IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);

handlebars.registerHelper("@json", new Jackson2Helper(mapper));
handlebars.registerHelper("@json", new JacksonHelper(mapper));

Template template = handlebars.compileInline("{{@json this view=\"missing.ViewClass\"}}");

Expand All @@ -131,7 +133,7 @@ public void jsonViewNotFound() throws IOException {
@Test
public void escapeHtml() throws IOException {
Handlebars handlebars = new Handlebars();
handlebars.registerHelper("@json", Jackson2Helper.INSTANCE);
handlebars.registerHelper("@json", JacksonHelper.INSTANCE);

Map<String, String> model = new HashMap<String, String>();
model.put("script", "<script text=\"text/javascript\"></script>");
Expand Down
Loading

0 comments on commit d8ff863

Please sign in to comment.