This commit is contained in:
Looly
2024-09-06 01:45:22 +08:00
parent 3ebaf1a783
commit af1f14b439
19 changed files with 448 additions and 123 deletions

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.dromara.hutool.json.engine;
package org.dromara.hutool.json.engine.fastjson;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONReader;
@@ -24,6 +24,8 @@ import com.alibaba.fastjson2.writer.ObjectWriter;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.engine.AbstractJSONEngine;
import org.dromara.hutool.json.engine.JSONEngineConfig;
import java.io.Reader;
import java.io.Writer;

View File

@@ -0,0 +1,24 @@
/*
* 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.
*/
/**
* Fastjson2引擎实现<br>
* https://github.com/alibaba/fastjson2
*
* @author Looly
* @since 6.0.0
*/
package org.dromara.hutool.json.engine.fastjson;

View File

@@ -0,0 +1,64 @@
/*
* 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.gson;
import com.google.gson.*;
import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.text.StrUtil;
import java.lang.reflect.Type;
import java.util.Date;
/**
* 日期序列化描述<br>
* 参考https://stackoverflow.com/questions/41979086/how-to-serialize-date-to-long-using-gson
*
* @author Looly
* @since 6.0.0
*/
public class DateSerDesc implements GsonSerDesc<Date> {
/**
* 默认日期格式化描述默认为null表示使用时间戳
*/
public static final DateSerDesc INSTANCE = new DateSerDesc(null);
private final String dateFormat;
/**
* 构造
*
* @param dateFormat 日期格式
*/
public DateSerDesc(final String dateFormat) {
this.dateFormat = dateFormat;
}
@Override
public JsonElement serialize(final Date src, final Type typeOfSrc, final JsonSerializationContext context) {
return StrUtil.isEmpty(dateFormat) ?
new JsonPrimitive(src.getTime()) :
new JsonPrimitive(DateUtil.format(src, dateFormat));
}
@Override
public Date deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
return StrUtil.isEmpty(dateFormat) ?
DateUtil.date(json.getAsLong()) :
DateUtil.parse(json.getAsString(), dateFormat);
}
}

View File

@@ -16,10 +16,9 @@
package org.dromara.hutool.json.engine.gson;
import com.google.gson.*;
import org.dromara.hutool.core.date.TimeUtil;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.JSONException;
import org.dromara.hutool.json.engine.AbstractJSONEngine;
@@ -28,7 +27,9 @@ import org.dromara.hutool.json.engine.JSONEngineConfig;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Type;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
/**
@@ -86,23 +87,28 @@ public class GsonEngine extends AbstractJSONEngine {
if (config.isPrettyPrint()) {
builder.setPrettyPrinting();
}
final String dateFormat = config.getDateFormat();
if (StrUtil.isNotEmpty(dateFormat)) {
builder.setDateFormat(dateFormat);
builder.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, typeOfT, context) -> TimeUtil.parse(json.getAsString(), dateFormat));
builder.registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (date, type, jsonSerializationContext) -> new JsonPrimitive(TimeUtil.format(date, dateFormat)));
} else {
// 无自定义格式,则默认输出时间戳
// https://stackoverflow.com/questions/41979086/how-to-serialize-date-to-long-using-gson
builder.registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsJsonPrimitive().getAsLong()));
builder.registerTypeAdapter(Date.class, (JsonSerializer<Date>) (date, type, jsonSerializationContext) -> new JsonPrimitive(date.getTime()));
builder.registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json, typeOfT, context) -> TimeUtil.of(json.getAsJsonPrimitive().getAsLong()));
builder.registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (date, type, jsonSerializationContext) -> new JsonPrimitive(TimeUtil.toEpochMilli(date)));
}
registerDate(builder, dateFormat);
if(!config.isIgnoreNullValue()){
builder.serializeNulls();
}
this.gson = builder.create();
}
/**
* 注册日期相关序列化描述<br>
* 参考https://stackoverflow.com/questions/41979086/how-to-serialize-date-to-long-using-gson
*
* @param builder Gson构造器
* @param dateFormat 日期格式
*/
private void registerDate(final GsonBuilder builder, final String dateFormat){
builder.registerTypeAdapter(Date.class, new DateSerDesc(dateFormat));
builder.registerTypeAdapter(LocalDateTime.class, new TemporalSerDesc(LocalDateTime.class, dateFormat));
builder.registerTypeAdapter(LocalDate.class, new TemporalSerDesc(LocalDate.class, dateFormat));
builder.registerTypeAdapter(LocalTime.class, new TemporalSerDesc(LocalTime.class, dateFormat));
}
}

View File

@@ -0,0 +1,30 @@
/*
* 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.gson;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonSerializer;
/**
* Gson序列化描述接口用于自定义序列化和反序列化
*
* @param <T> 序列化对象类型
* @author Looly
* @since 6.0.0
*/
public interface GsonSerDesc<T> extends JsonSerializer<T>, JsonDeserializer<T> {
}

View File

@@ -0,0 +1,63 @@
/*
* 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.gson;
import com.google.gson.*;
import org.dromara.hutool.core.convert.ConvertUtil;
import org.dromara.hutool.core.date.TimeUtil;
import org.dromara.hutool.core.text.StrUtil;
import java.lang.reflect.Type;
import java.time.temporal.TemporalAccessor;
/**
* 时间相关对象序列化描述
*
* @author Looly
* @since 6.0.0
*/
public class TemporalSerDesc implements GsonSerDesc<TemporalAccessor> {
private final Class<? extends TemporalAccessor> type;
private final String dateFormat;
/**
* 构造
*
* @param type 时间类型
* @param dateFormat 日期格式
*/
public TemporalSerDesc(final Class<? extends TemporalAccessor> type, final String dateFormat) {
this.type = type;
this.dateFormat = dateFormat;
}
@Override
public JsonElement serialize(final TemporalAccessor src, final Type typeOfSrc, final JsonSerializationContext context) {
return StrUtil.isEmpty(dateFormat) ?
new JsonPrimitive(TimeUtil.toEpochMilli(src)) :
new JsonPrimitive(TimeUtil.format(src, dateFormat));
}
@Override
public TemporalAccessor deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
return StrUtil.isEmpty(dateFormat) ?
ConvertUtil.convert(this.type, json.getAsLong()) :
ConvertUtil.convert(this.type, TimeUtil.parse(json.getAsString(), dateFormat));
}
}

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package org.dromara.hutool.json.engine;
package org.dromara.hutool.json.engine.jackson;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
@@ -30,6 +30,8 @@ import org.dromara.hutool.core.reflect.ConstructorUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.core.util.ObjUtil;
import org.dromara.hutool.json.JSONException;
import org.dromara.hutool.json.engine.AbstractJSONEngine;
import org.dromara.hutool.json.engine.JSONEngineConfig;
import java.io.IOException;
import java.io.Reader;
@@ -122,6 +124,8 @@ public class JacksonEngine extends AbstractJSONEngine {
mapper.enable(SerializationFeature.INDENT_OUTPUT);
}
final String dateFormat = config.getDateFormat();
// 用于处理java.time库中对象的序列化和反序列化
mapper.registerModule(new TemporalModule(dateFormat));
if(StrUtil.isNotEmpty(dateFormat)){
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
mapper.setDateFormat(DateUtil.newSimpleFormat(dateFormat));

View File

@@ -0,0 +1,42 @@
/*
* 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.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import java.io.IOException;
import java.time.temporal.TemporalAccessor;
public class JacksonTemporalDeserializer extends StdDeserializer<TemporalAccessor> {
private final Class<? extends TemporalAccessor> type;
private final String dateFormat;
public JacksonTemporalDeserializer(final Class<? extends TemporalAccessor> type, final String dateFormat) {
super(TemporalAccessor.class);
this.type = type;
this.dateFormat = dateFormat;
}
@Override
public TemporalAccessor deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException, JacksonException {
return null;
}
}

View File

@@ -0,0 +1,58 @@
/*
* 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.date.TimeUtil;
import org.dromara.hutool.core.text.StrUtil;
import java.io.IOException;
import java.time.temporal.TemporalAccessor;
/**
* Jackson处理{@link TemporalAccessor}相关对象序列化描述
*
* @author Looly
* @since 6.0.0
*/
public class JacksonTemporalSerializer extends StdSerializer<TemporalAccessor> {
private static final long serialVersionUID = 1L;
private final String format;
/**
* 构造
*
* @param format 日期格式null表示使用时间戳
*/
public JacksonTemporalSerializer(final String format) {
super(TemporalAccessor.class);
this.format = format;
}
@Override
public void serialize(final TemporalAccessor value, final JsonGenerator gen, final SerializerProvider serializers) throws IOException {
if (StrUtil.isEmpty(this.format)) {
final long epochMilli = TimeUtil.toEpochMilli(value);
gen.writeNumber(epochMilli);
} else {
gen.writeString(TimeUtil.format(value, this.format));
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.databind.module.SimpleModule;
/**
* 时间相关序列化模块
*
* @author Looly
* @since 6.0.0
*/
public class TemporalModule extends SimpleModule {
private static final long serialVersionUID = 1L;
/**
* 构造
*
* @param dateFormat 日期格式null表示使用时间戳
*/
public TemporalModule(final String dateFormat) {
super();
this.addSerializer(new JacksonTemporalSerializer(dateFormat));
}
}

View File

@@ -14,7 +14,7 @@
# limitations under the License.
#
org.dromara.hutool.json.engine.JacksonEngine
org.dromara.hutool.json.engine.jackson.JacksonEngine
org.dromara.hutool.json.engine.gson.GsonEngine
org.dromara.hutool.json.engine.FastJSON2Engine
org.dromara.hutool.json.engine.fastjson.FastJSON2Engine
org.dromara.hutool.json.engine.HutoolJSONEngine