diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/HutoolJSONSerializer.java b/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/HutoolJSONSerializer.java new file mode 100644 index 000000000..a34dfbf4e --- /dev/null +++ b/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/HutoolJSONSerializer.java @@ -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 { + 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(); + } + } +} diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/TemporalModule.java b/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/HutoolModule.java similarity index 87% rename from hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/TemporalModule.java rename to hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/HutoolModule.java index d5c7910da..e573b104b 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/TemporalModule.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/HutoolModule.java @@ -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()); } } diff --git a/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/JacksonEngine.java b/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/JacksonEngine.java index 50aee205c..8e2f66119 100644 --- a/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/JacksonEngine.java +++ b/hutool-json/src/main/java/org/dromara/hutool/json/engine/jackson/JacksonEngine.java @@ -140,7 +140,7 @@ public class JacksonEngine extends AbstractJSONEngine implements Wrapper + * 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); + } }