Skip to content

Commit

Permalink
feat: add jackson string process module
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet committed Oct 9, 2023
1 parent c297647 commit 0022153
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.qwlabs.jackson.datatype;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;
import com.fasterxml.jackson.core.util.VersionUtil;

public class PackageVersion implements Versioned {
public static final Version VERSION = VersionUtil.parseVersion(
"0.2.0", "com.qwlabs.doraemom", "jackson");
@Override
public Version version() {
return VERSION;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.qwlabs.jackson.datatype;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.type.LogicalType;

import java.io.IOException;


@JacksonStdImpl
public class StringProcessDeserializer extends StdScalarDeserializer<String> {
public static final StringProcessDeserializer INSTANCE = new StringProcessDeserializer();

protected StringProcessDeserializer() {
super(String.class);
}

public LogicalType logicalType() {
return LogicalType.Textual;
}

public boolean isCachable() {
return true;
}

public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
return "";
}

public String deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
if (p.hasToken(JsonToken.VALUE_STRING)) {
return trim(p.getText());
}
if (p.hasToken(JsonToken.START_ARRAY)) {
return this._deserializeFromArray(p, ctxt);
}
return trim(this._parseString(p, ctxt, this));
}

private String trim(String value) {
return value == null ? null : value.trim();
}

public String deserializeWithType(JsonParser p, DeserializationContext ctxt, TypeDeserializer typeDeserializer) throws IOException {
return this.deserialize(p, ctxt);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.qwlabs.jackson.datatype;

import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.deser.Deserializers;

import java.io.Serializable;


public class StringProcessDeserializers extends Deserializers.Base implements Serializable {
public static final StringProcessDeserializers INSTANCE = new StringProcessDeserializers();

@Override
public JsonDeserializer<?> findBeanDeserializer(JavaType type,
DeserializationConfig config,
BeanDescription beanDesc) throws JsonMappingException {
if (type.hasRawClass(String.class)) {
return StringProcessDeserializer.INSTANCE;
}
return super.findBeanDeserializer(type, config, beanDesc);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.qwlabs.jackson.datatype;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.databind.module.SimpleModule;

public class StringProcessModule extends SimpleModule {
@Override
public String getModuleName() {
return "StringProcessModule";
}

@Override
public Version version() {
return PackageVersion.VERSION;
}

@Override
public void setupModule(SetupContext setupContext) {
setupContext.addDeserializers(new StringProcessDeserializers());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.qwlabs.jackson.datatype;

public @interface Trimmed {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.qwlabs.jackson.datatype;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;


class StringModuleTest {
private String leftEmpty = """
{
"name": " d",
"value": 3
}
""";

private String rightEmpty = """
{
"name": "d ",
"value": 3
}
""";

private String allEmpty = """
{
"name": " d ",
"value": 3
}
""";

@Test
void should_trim() throws JsonProcessingException {
var objectMapper = withModule();

assertThat(objectMapper.readValue(leftEmpty, TestClass.class).name, is("d"));
assertThat(objectMapper.readValue(rightEmpty, TestClass.class).name, is("d"));
assertThat(objectMapper.readValue(allEmpty, TestClass.class).name, is("d"));
}

@Test
void should_not_trim() throws JsonProcessingException {
var objectMapper = withoutModule();

assertThat(objectMapper.readValue(leftEmpty, TestClass.class).name, is(" d"));
assertThat(objectMapper.readValue(rightEmpty, TestClass.class).name, is("d "));
assertThat(objectMapper.readValue(allEmpty, TestClass.class).name, is(" d "));
}

private ObjectMapper withoutModule() {
return new ObjectMapper();
}

private ObjectMapper withModule() {
var objectMapper = new ObjectMapper();
objectMapper.registerModule(new StringProcessModule());
return objectMapper;
}

@Getter
@Setter
public static class TestClass {
@Trimmed
private String name;
private Integer value;
}
}

0 comments on commit 0022153

Please sign in to comment.