add HutoolJSONSerializer

This commit is contained in:
Looly 2024-11-11 19:35:39 +08:00
parent 7d8b95f108
commit 3dc6314b56
4 changed files with 91 additions and 3 deletions

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2024 Hutool Team and hutool.cn
*
* 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 org.dromara.hutool.json.engine.jackson;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import org.dromara.hutool.core.io.IORuntimeException;
import org.dromara.hutool.json.JSON;
import org.dromara.hutool.json.JSONArray;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONPrimitive;
import java.io.IOException;
/**
* Hutool JSON序列化器
*
* @author looly
* @since 6.0.0
*/
public class HutoolJSONSerializer extends StdSerializer<JSON> {
private static final long serialVersionUID = 1L;
protected HutoolJSONSerializer() {
super(JSON.class);
}
@Override
public void serialize(final JSON json, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException {
if(json instanceof JSONPrimitive){
jsonGenerator.writeObject(((JSONPrimitive) json).getValue());
}else if(json instanceof JSONObject){
jsonGenerator.writeStartObject();
json.asJSONObject().forEach((k, v)->{
try {
jsonGenerator.writeObjectField(k, v);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
});
jsonGenerator.writeEndObject();
}else if(json instanceof JSONArray){
jsonGenerator.writeStartArray();
json.asJSONArray().forEach(v->{
try {
jsonGenerator.writeObject(v);
} catch (final IOException e) {
throw new IORuntimeException(e);
}
});
jsonGenerator.writeEndArray();
}
}
}

View File

@ -24,7 +24,7 @@ import com.fasterxml.jackson.databind.module.SimpleModule;
* @author Looly
* @since 6.0.0
*/
public class TemporalModule extends SimpleModule {
public class HutoolModule extends SimpleModule {
private static final long serialVersionUID = 1L;
/**
@ -32,8 +32,9 @@ public class TemporalModule extends SimpleModule {
*
* @param dateFormat 日期格式null表示使用时间戳
*/
public TemporalModule(final String dateFormat) {
public HutoolModule(final String dateFormat) {
super();
this.addSerializer(new JacksonTemporalSerializer(dateFormat));
this.addSerializer(new HutoolJSONSerializer());
}
}

View File

@ -140,7 +140,7 @@ public class JacksonEngine extends AbstractJSONEngine implements Wrapper<ObjectM
}
final String dateFormat = config.getDateFormat();
// 用于处理java.time库中对象的序列化和反序列化
mapper.registerModule(new TemporalModule(dateFormat));
mapper.registerModule(new HutoolModule(dateFormat));
if(StrUtil.isNotEmpty(dateFormat)){
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setDateFormat(DateUtil.newSimpleFormat(dateFormat));

View File

@ -17,6 +17,8 @@
package org.dromara.hutool.json.engine;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.json.JSONObject;
import org.dromara.hutool.json.JSONUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@ -39,4 +41,20 @@ public class JacksonTest {
" \"gender\" : true\n" +
"}", jsonString);
}
/**
* https://gitee.com/dromara/hutool/issues/IB3GM4<br>
* JSON和Jackson兼容
*/
@Test
void toJsonStringOfHutoolJsonTest() {
final JSONObject jsonObject = JSONUtil.ofObj()
.putValue("name", "张三")
.putValue("age", 18)
.putValue("sub", JSONUtil.ofObj()
.putValue("aaa", "aa1").putValue("bbb", "bb1"));
final JSONEngine engine = JSONEngineFactory.createEngine("jackson");
final String jsonString = engine.toJsonString(jsonObject);
Assertions.assertEquals("{\"name\":\"张三\",\"age\":18,\"sub\":{\"aaa\":\"aa1\",\"bbb\":\"bb1\"}}", jsonString);
}
}