-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add jackson string process module
- Loading branch information
pipinet
committed
Oct 9, 2023
1 parent
c297647
commit 0022153
Showing
6 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
jackson/src/main/java/com/qwlabs/jackson/datatype/PackageVersion.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
jackson/src/main/java/com/qwlabs/jackson/datatype/StringProcessDeserializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
jackson/src/main/java/com/qwlabs/jackson/datatype/StringProcessDeserializers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
jackson/src/main/java/com/qwlabs/jackson/datatype/StringProcessModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
jackson/src/main/java/com/qwlabs/jackson/datatype/Trimmed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package com.qwlabs.jackson.datatype; | ||
|
||
public @interface Trimmed { | ||
} |
70 changes: 70 additions & 0 deletions
70
jackson/src/test/java/com/qwlabs/jackson/datatype/StringModuleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |