mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-24 18:04:54 +08:00
fix code
This commit is contained in:
parent
3ebaf1a783
commit
af1f14b439
@ -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;
|
@ -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;
|
@ -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);
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
|
@ -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> {
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -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));
|
@ -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;
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
@ -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));
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -18,11 +18,13 @@ package org.dromara.hutool.json.engine;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class BeanWithDate {
|
||||
private Date date1;
|
||||
|
@ -0,0 +1,28 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class BeanWithLocalDate {
|
||||
private LocalDate date;
|
||||
}
|
@ -16,9 +16,6 @@
|
||||
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -39,30 +36,4 @@ public class FastJSONTest {
|
||||
" \"gender\":true\n" +
|
||||
"}", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("fastjson");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":1704042741000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("fastjson");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
@ -42,28 +42,16 @@ public class GsonTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
void writeLocalDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final BeanWithLocalDate bean = new BeanWithLocalDate(TimeUtil.of(date).toLocalDate());
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("gson");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":1704042741000}", jsonString);
|
||||
Assertions.assertEquals("{\"date\":1704038400000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
Assertions.assertEquals("{\"date\":\"2024-01-01 00:00:00\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("gson");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,6 @@
|
||||
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -36,30 +33,4 @@ public class HutoolJSONTest {
|
||||
" \"gender\": true\n" +
|
||||
"}", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("hutool");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":1704042741000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("hutool");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
@ -20,7 +20,9 @@ import com.alibaba.fastjson2.annotation.JSONField;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.dromara.hutool.json.engine.fastjson.FastJSON2Engine;
|
||||
import org.dromara.hutool.json.engine.gson.GsonEngine;
|
||||
import org.dromara.hutool.json.engine.jackson.JacksonEngine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.StringReader;
|
||||
|
@ -16,5 +16,66 @@
|
||||
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class JSONEngineTest {
|
||||
|
||||
private final String[] engineNames = {"jackson", "gson", "fastjson", "hutool"};
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
Arrays.stream(engineNames).forEach(this::assertWriteDateFormat);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
Arrays.stream(engineNames).forEach(this::assertWriteNull);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeLocalDateFormatTest() {
|
||||
Arrays.stream(engineNames).forEach(this::assertWriteLocalDateFormat);
|
||||
}
|
||||
|
||||
private void assertWriteDateFormat(final String engineName) {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine(engineName);
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":1704042741000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01 01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
void assertWriteLocalDateFormat(final String engineName) {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithLocalDate bean = new BeanWithLocalDate(TimeUtil.of(date).toLocalDate());
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine(engineName);
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date\":1704038400000}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date\":\"2024-01-01 00:00:00\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
private void assertWriteNull(final String engineName) {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine(engineName);
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,6 @@
|
||||
|
||||
package org.dromara.hutool.json.engine;
|
||||
|
||||
import org.dromara.hutool.core.date.DateTime;
|
||||
import org.dromara.hutool.core.date.DateUtil;
|
||||
import org.dromara.hutool.core.date.TimeUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -42,31 +39,4 @@ public class JacksonTest {
|
||||
" \"gender\" : true\n" +
|
||||
"}", jsonString);
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeDateFormatTest() {
|
||||
final DateTime date = DateUtil.parse("2024-01-01 01:12:21");
|
||||
final BeanWithDate bean = new BeanWithDate(date, TimeUtil.of(date));
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("jackson");
|
||||
|
||||
final String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":1704042741000,\"date2\":[2024,1,1,1,12,21]}", jsonString);
|
||||
|
||||
//TODO LocalDateTime的格式化未解决
|
||||
engine.init(JSONEngineConfig.of().setDateFormat("yyyy-MM-dd HH:mm:ss"));
|
||||
Assertions.assertEquals("{\"date1\":\"2024-01-01 01:12:21\",\"date2\":\"2024-01-01T01:12:21\"}", engine.toJsonString(bean));
|
||||
}
|
||||
|
||||
@Test
|
||||
void writeNullTest() {
|
||||
final BeanWithDate bean = new BeanWithDate(null, null);
|
||||
final JSONEngine engine = JSONEngineFactory.createEngine("jackson");
|
||||
|
||||
String jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{}", jsonString);
|
||||
|
||||
engine.init(JSONEngineConfig.of().setIgnoreNullValue(false));
|
||||
jsonString = engine.toJsonString(bean);
|
||||
Assertions.assertEquals("{\"date1\":null,\"date2\":null}", jsonString);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user