Skip to content

Commit

Permalink
feat: config增加registerModule可以注册自定义模块
Browse files Browse the repository at this point in the history
  • Loading branch information
qw623577789 committed Oct 4, 2022
1 parent cecce84 commit 4f31aaa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ repositories {
}
dependencies {
implementation 'com.github.qw623577789:qtk-json:v1.10.0'
implementation 'com.github.qw623577789:qtk-json:v1.12.0'
}
```

Expand Down Expand Up @@ -64,7 +64,9 @@ dependencies {
- **String toString(boolean pretty, int spaceAmount)** 将Point实例转换为JSON字符串, ``pretty``控制是否美化输出json,``spaceAmount``可以控制美化输出时空格数量
- JSONConfig jackson库特性配置,并且使用设置的特性进行JSON操作
- **JSONConfig features(HashMap\<FormatFeature, Boolean\> features)** 控制``jackson````enable/disable``特性
- **JSONConfig serializationInclusion(JsonInclude.Include setSerializationInclusion)** 控制``jackson````esetSerializationInclusion``特性
- **JSONConfig serializationInclusion(JsonInclude.Include setSerializationInclusion)** 控制``jackson````setSerializationInclusion``特性
- **JSONConfig registerModule(com.fasterxml.jackson.databind.Module... module)**``jackson``库注册模块
- **JSONConfig confirmToCreateMapper()** 最终生成``jackson````ObjectMapper``,应用于后续的操作
- **JSON new JSON(boolean isObject)** 创建一个JSON实例, true/false控制创建出来是**JSON对象**还是**JSON数组**
- **JSON new JSON(JsonNode jacksonNode)** 将com.fasterxml.jackson的``JsonNode``转化为JSON实例
- **JSON parse(Object object)** 可将大部分Java对象转换为JSON实例
Expand Down Expand Up @@ -575,6 +577,7 @@ Assertions.assertEquals(
}
)
.serializationInclusion(JsonInclude.Include.NON_NULL)
.confirmToCreateMapper()
.parse("{a:1}")
.point()
.get()
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugins {
}

group = "team.qtk"
version = "1.11.1"
version = "1.12.0"
sourceCompatibility = 11
targetCompatibility = 11

Expand Down
38 changes: 30 additions & 8 deletions src/main/java/team/qtk/json/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ public class JSON {

private JsonNode json;

/**
* 自定义配置ObjectMapper
*/
public static JSONConfig config() {
JsonMapper.Builder customJacksonBuilder = JsonMapper
.builder()
.nodeFactory(JsonNodeFactory.withExactBigDecimals(true)); //修复bigDecimal 1.0 转化后丢失.0问题
.nodeFactory(JsonNodeFactory.withExactBigDecimals(true)) //修复bigDecimal 1.0 转化后丢失.0问题
.addModule(new JavaTimeModule().addDeserializer(LocalDateTime.class, new JsonDateTimeParser()));

return new JSONConfig(customJacksonBuilder);
}
Expand Down Expand Up @@ -724,6 +728,8 @@ public static class JSONConfig {

private JsonMapper.Builder customJacksonBuilder;

private ObjectMapper customJacksonMapper;

public JSONConfig(JsonMapper.Builder customJacksonBuilder) {
this.customJacksonBuilder = customJacksonBuilder;
}
Expand All @@ -747,32 +753,48 @@ public JSONConfig serializationInclusion(JsonInclude.Include setSerializationInc
return this;
}

/**
* 增加注册模块
*/
public JSONConfig registerModule(com.fasterxml.jackson.databind.Module... module) {
customJacksonBuilder.addModules(module);
return this;
}

/**
* 最终生成ObjectMapper
*/
public JSONConfig confirmToCreateMapper() {
customJacksonMapper = customJacksonBuilder.build();
return this;
}

public JSON JSON(JsonNode jacksonNode) {
return new JSON(jacksonNode, customJacksonBuilder.build());
return new JSON(jacksonNode, customJacksonMapper);
}

public JSON JSON(boolean isObject) {
return new JSON(isObject, customJacksonBuilder.build());
return new JSON(isObject, customJacksonMapper);
}

public JSON missingNode() {
return JSON.missingNode(customJacksonBuilder.build());
return JSON.missingNode(customJacksonMapper);
}

public JSON nullNode() {
return JSON.nullNode(customJacksonBuilder.build());
return JSON.nullNode(customJacksonMapper);
}

public JSON sPut(String id, Object value) {
return JSON.sPut(customJacksonBuilder.build(), id, value);
return JSON.sPut(customJacksonMapper, id, value);
}

public JSON sAdd(Object... value) {
return JSON.sAdd(customJacksonBuilder.build(), value);
return JSON.sAdd(customJacksonMapper, value);
}

public JSON parse(Object object) {
return JSON.parse(customJacksonBuilder.build(), object);
return JSON.parse(customJacksonMapper, object);
}
}
}
1 change: 1 addition & 0 deletions src/test/java/team/qtk/json/JSONTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2798,6 +2798,7 @@ void config() {
}
)
.serializationInclusion(JsonInclude.Include.NON_NULL)
.confirmToCreateMapper()
.parse("{a:1}")
.getJacksonNode() instanceof ObjectNode);
}
Expand Down

0 comments on commit 4f31aaa

Please sign in to comment.