Skip to content

Commit

Permalink
💥 fastjson/jackson compatibility #251
Browse files Browse the repository at this point in the history
  • Loading branch information
trydofor committed Jun 6, 2024
1 parent 48815dd commit 89cf397
Show file tree
Hide file tree
Showing 51 changed files with 1,432 additions and 520 deletions.
12 changes: 7 additions & 5 deletions WingsBoot.t.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ Use `t.md` as local [Test Management](https://www.jetbrains.com/help/idea/test-m
* 13007 SmartFormatterTest: print date format of pattern
* 13008 SmartFormatterTest: parse LocalDateTime of smart pattern
* 13009 SmartFormatterTest: parse LocalDateTime by smart pattern with timezone
* 13010 FastJsonHelperTest: json object by default
* 13011 FastJsonHelperTest: json number as string
* 13012 FastJsonHelperTest: json number as string with thousands
* 13013 FastJsonHelperTest: json number as string issue 1537
* 13014 JacksonHelperTest: json and xml mapper
* 13010 FastJsonTest: json object by default
* 13011 FastJsonTest: json number as string
* 13012 FastJsonTest: json number as string with thousands
* 13013 FastJsonTest: json number as string issue 1537
* 13014 JacksonTest: json and xml mapper
* 13015 JsonConversionTest: can convert to TypeDescriptor
* 13016 JsonConversionTest: convert Dto, Map, List
* 13017 JsonConversionTest: parse with generics
Expand Down Expand Up @@ -317,6 +317,8 @@ Use `t.md` as local [Test Management](https://www.jetbrains.com/help/idea/test-m
* 13121 BootAdminServerTest: servlet mapping order
* 13122 AsyncHelperTest: async ttl decorator
* 13123 DoubleKillTest: bad SpEL, check null
* 13124 JsonHelperCompatibleTest: fastjson basic type compatible
* 13125 JsonHelperCompatibleTest: jackson basic type compatible

## 14 Warlock

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<mirana.version>2.7.3-SNAPSHOT</mirana.version> <!-- https://github.com/trydofor/pro.fessional.mirana -->
<meepo.version>1.5.1</meepo.version> <!-- https://github.com/trydofor/pro.fessional.meepo -->
<kaptcha.version>2.3.3</kaptcha.version> <!-- https://github.com/trydofor/kaptcha -->
<fastjson2.version>2.0.47</fastjson2.version> <!-- https://github.com/alibaba/fastjson2/releases -->
<fastjson2.version>2.0.51</fastjson2.version> <!-- https://github.com/alibaba/fastjson2/releases -->
<fastjson.version>${fastjson2.version}</fastjson.version> <!-- https://github.com/alibaba/fastjson/releases -->
<kryo.version>5.6.0</kryo.version> <!-- https://github.com/EsotericSoftware/kryo/releases -->
<transmittable.version>2.14.5</transmittable.version> <!-- https://github.com/alibaba/transmittable-thread-local/releases -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class MessageHelper {
@NotNull
public static String get(MessageSource ms, CodeEnum code, Object... args) {
try {
Locale locale = LocaleZoneIdUtil.LocaleNonnull.get();
Locale locale = LocaleZoneIdUtil.LocaleNonnull();
return ms.getMessage(code.getI18nCode(), args, locale);
}
catch (NoSuchMessageException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,47 +111,62 @@ public Jackson2ObjectMapperBuilderCustomizer customizeJacksonDatetime(SlardarDat
final AutoZoneType autoLocal = AutoZoneType.valueOf(prop.getDatetime().isAuto());
JacksonLocalDateTimeSerializer.defaultFormatter = full;
JacksonLocalDateTimeSerializer.defaultAutoZone = autoLocal;
builder.serializerByType(LocalDateTime.class, new JacksonLocalDateTimeSerializer(full, autoLocal));
var jacksonLocalDateTimeSerializer = new JacksonLocalDateTimeSerializer(full, autoLocal);
builder.serializerByType(LocalDateTime.class, jacksonLocalDateTimeSerializer);

var fullPsr = prop.getDatetime()
.getSupport()
.stream()
.map(DateTimeFormatter::ofPattern)
.collect(Collectors.toList());
builder.deserializerByType(LocalDateTime.class, new JacksonLocalDateTimeDeserializer(full, fullPsr, autoLocal));
var jacksonLocalDateTimeDeserializer = new JacksonLocalDateTimeDeserializer(full, fullPsr, autoLocal);
builder.deserializerByType(LocalDateTime.class, jacksonLocalDateTimeDeserializer);
log.info("SlardarWebmvc conf Jackson2ObjectMapperBuilderCustomizer LocalDateTime");

// auto zoned
DateTimeFormatter zoned = DateTimeFormatter.ofPattern(prop.getZoned().getFormat());
final AutoZoneType autoZone = AutoZoneType.valueOf(prop.getZoned().isAuto());
JacksonZonedDateTimeSerializer.defaultFormatter = zoned;
JacksonZonedDateTimeSerializer.defaultAutoZone = autoZone;
builder.serializerByType(ZonedDateTime.class, new JacksonZonedDateTimeSerializer(zoned, autoZone));
var jacksonZonedDateTimeSerializer = new JacksonZonedDateTimeSerializer(zoned, autoZone);
builder.serializerByType(ZonedDateTime.class, jacksonZonedDateTimeSerializer);

var zonePsr = prop.getZoned()
.getSupport()
.stream()
.map(DateTimeFormatter::ofPattern)
.collect(Collectors.toList());

builder.deserializerByType(ZonedDateTime.class, new JacksonZonedDateTimeDeserializer(zoned, zonePsr, autoZone));
var jacksonZonedDateTimeDeserializer = new JacksonZonedDateTimeDeserializer(zoned, zonePsr, autoZone);
builder.deserializerByType(ZonedDateTime.class, jacksonZonedDateTimeDeserializer);
log.info("SlardarWebmvc conf Jackson2ObjectMapperBuilderCustomizer ZonedDateTime");

// auto offset
DateTimeFormatter offset = DateTimeFormatter.ofPattern(prop.getOffset().getFormat());
final AutoZoneType autoOffset = AutoZoneType.valueOf(prop.getOffset().isAuto());
JacksonOffsetDateTimeSerializer.defaultFormatter = offset;
JacksonOffsetDateTimeSerializer.defaultAutoZone = autoOffset;
builder.serializerByType(OffsetDateTime.class, new JacksonOffsetDateTimeSerializer(offset, autoOffset));
var jacksonOffsetDateTimeSerializer = new JacksonOffsetDateTimeSerializer(offset, autoOffset);
builder.serializerByType(OffsetDateTime.class, jacksonOffsetDateTimeSerializer);

var offPsr = prop.getZoned()
.getSupport()
.stream()
.map(DateTimeFormatter::ofPattern)
.collect(Collectors.toList());

builder.deserializerByType(OffsetDateTime.class, new JacksonOffsetDateTimeDeserializer(offset, offPsr, autoOffset));
var jacksonOffsetDateTimeDeserializer = new JacksonOffsetDateTimeDeserializer(offset, offPsr, autoOffset);
builder.deserializerByType(OffsetDateTime.class, jacksonOffsetDateTimeDeserializer);
log.info("SlardarWebmvc conf Jackson2ObjectMapperBuilderCustomizer OffsetDateTime");

new JacksonHelper() {{
JacksonHelper.localDateTimeSerializer = jacksonLocalDateTimeSerializer;
JacksonHelper.localDateTimeDeserializer = jacksonLocalDateTimeDeserializer;
JacksonHelper.zonedDateTimeSerializer = jacksonZonedDateTimeSerializer;
JacksonHelper.zonedDateTimeDeserializer = jacksonZonedDateTimeDeserializer;
JacksonHelper.offsetDateTimeSerializer = jacksonOffsetDateTimeSerializer;
JacksonHelper.offsetDateTimeDeserializer = jacksonOffsetDateTimeDeserializer;
}};
};
}

Expand Down Expand Up @@ -256,10 +271,27 @@ public ApplicationStartedEventRunner jacksonHelperRunner(Jackson2ObjectMapperBui
log.info("SlardarWebmvc spring-runs jacksonHelperRunner");
return new ApplicationStartedEventRunner(WingsOrdered.Lv1Config, ignored -> {
log.info("SlardarWebmvc spring-conf JacksonHelper.initGlobal");
JacksonHelper.initGlobal(
builder.createXmlMapper(false).build(),
builder.createXmlMapper(true).build()
);
new JacksonHelper() {{
// auto off
if (localDateTimeSerializer != null) builder.serializerByType(LocalDateTime.class, localDateTimeSerializer.autoOff());
if (localDateTimeDeserializer != null) builder.deserializerByType(LocalDateTime.class, localDateTimeDeserializer.autoOff());
if (zonedDateTimeSerializer != null) builder.serializerByType(ZonedDateTime.class, zonedDateTimeSerializer.autoOff());
if (zonedDateTimeDeserializer != null) builder.deserializerByType(ZonedDateTime.class, zonedDateTimeDeserializer.autoOff());
if (offsetDateTimeSerializer != null) builder.serializerByType(OffsetDateTime.class, offsetDateTimeSerializer.autoOff());
if (offsetDateTimeDeserializer != null) builder.deserializerByType(OffsetDateTime.class, offsetDateTimeDeserializer.autoOff());

XmlWings = builder.createXmlMapper(true).build();
JsonWings = builder.createXmlMapper(false).build(); // restore false

// restore
if (localDateTimeSerializer != null) builder.serializerByType(LocalDateTime.class, localDateTimeSerializer);
if (localDateTimeDeserializer != null) builder.deserializerByType(LocalDateTime.class, localDateTimeDeserializer);
if (zonedDateTimeSerializer != null) builder.serializerByType(ZonedDateTime.class, zonedDateTimeSerializer);
if (zonedDateTimeDeserializer != null) builder.deserializerByType(ZonedDateTime.class, zonedDateTimeDeserializer);
if (offsetDateTimeSerializer != null) builder.serializerByType(OffsetDateTime.class, offsetDateTimeSerializer);
if (offsetDateTimeDeserializer != null) builder.deserializerByType(OffsetDateTime.class, offsetDateTimeDeserializer);

}};
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,19 @@ public void infoFastjsonAndJackson() {
Bad bad = new Bad();
bad.setSsStr("ssStr");
bad.setSStr("sStr");
final String j1 = JSON.toJSONString(bad, FastJsonHelper.DefaultWriter());
final String j1 = JSON.toJSONString(bad, FastJsonHelper.WingsWriter);
log.info("fastjson:{}", j1);

final String j2 = objectMapper.writeValueAsString(bad);
log.info("jackson:{}", j2);

//
final Bad o1 = JSON.parseObject(j1, Bad.class, FastJsonHelper.DefaultReader());
final Bad o1 = JSON.parseObject(j1, Bad.class, FastJsonHelper.WingsReader);
log.info("fastjson-fastjson:{}", o1);
final Bad o2 = objectMapper.readValue(j1, Bad.class);
log.info("fastjson-jackson:{}", o2);

final Bad o3 = JSON.parseObject(j2, Bad.class, FastJsonHelper.DefaultReader());
final Bad o3 = JSON.parseObject(j2, Bad.class, FastJsonHelper.WingsReader);
log.info("jackson-fastjson:{}", o3);
final Bad o4 = objectMapper.readValue(j2, Bad.class);
log.info("jackson-jackson:{}", o4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.time.LocalDate;
import java.util.TreeMap;
import java.util.LinkedHashMap;

import static org.junit.jupiter.api.Assertions.assertEquals;

Expand All @@ -31,21 +31,21 @@
* @since 2021-07-05
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = {
"wings.slardar.number.decimal.separator=_",
"wings.slardar.number.floats.format=#.00",
"wings.slardar.number.decimal.format=#.00",
})
properties = {
"wings.slardar.number.decimal.separator=_",
"wings.slardar.number.floats.format=#.00",
"wings.slardar.number.decimal.format=#.00",
})
@Slf4j
public class DecimalFormatTest {

@Setter(onMethod_ = {@Autowired})
@Setter(onMethod_ = { @Autowired })
private ObjectMapper objectMapper;

@Setter(onMethod_ = {@Autowired})
@Setter(onMethod_ = { @Autowired })
private RestTemplate restTemplate;

@Setter(onMethod_ = {@Value("http://localhost:${local.server.port}")})
@Setter(onMethod_ = { @Value("http://localhost:${local.server.port}") })
private String domain;


Expand Down Expand Up @@ -148,35 +148,37 @@ public static class DecRaw {
public void testDecStr() throws JsonProcessingException {
final String decStr = objectMapper.writeValueAsString(new DecStr());
log.info(decStr);
Assertions.assertEquals("{\"intVal\":123456,"
+ "\"intObj\":123456,"
+ "\"longVal\":123456,"
+ "\"longObj\":123456,"
+ "\"floatVal\":\"123456.78\","
+ "\"floatObj\":\"123456.78\","
+ "\"doubleVal\":\"123456.78\","
+ "\"doubleObj\":\"123456.78\","
+ "\"decimalObj\":\"123456.78\","
+ "\"integerObj\":\"123456789.00\"}"
, decStr);
Assertions.assertEquals(
"{\"intVal\":123456,"
+ "\"intObj\":123456,"
+ "\"longVal\":123456,"
+ "\"longObj\":123456,"
+ "\"floatVal\":\"123456.78\","
+ "\"floatObj\":\"123456.78\","
+ "\"doubleVal\":\"123456.78\","
+ "\"doubleObj\":\"123456.78\","
+ "\"decimalObj\":\"123456.78\","
+ "\"integerObj\":\"123456789.00\"}"
, decStr);
}

@Test
@TmsLink("C13062")
public void testDecRaw() throws JsonProcessingException {
final String decRaw = objectMapper.writeValueAsString(new DecRaw());
log.info(decRaw);
Assertions.assertEquals("{\"intVal\":123456,"
+ "\"intObj\":123456,"
+ "\"longVal\":123456,"
+ "\"longObj\":123456,"
+ "\"floatVal\":123456.79,"
+ "\"floatObj\":123456.79,"
+ "\"doubleVal\":123456.789,"
+ "\"doubleObj\":123456.789,"
+ "\"decimalObj\":123456.789,"
+ "\"integerObj\":123456789}"
, decRaw);
Assertions.assertEquals(
"{\"intVal\":123456,"
+ "\"intObj\":123456,"
+ "\"longVal\":123456,"
+ "\"longObj\":123456,"
+ "\"floatVal\":123456.79,"
+ "\"floatObj\":123456.79,"
+ "\"doubleVal\":123456.789,"
+ "\"doubleObj\":123456.789,"
+ "\"decimalObj\":123456.789,"
+ "\"integerObj\":123456789}"
, decRaw);

}

Expand All @@ -185,40 +187,44 @@ public void testDecRaw() throws JsonProcessingException {
public void testDecFmt() throws JsonProcessingException {
final String decFmt = objectMapper.writeValueAsString(new DecFmt());
log.info(decFmt);
Assertions.assertEquals("{\"intVal\":12,34,56.0,"
+ "\"intObj\":12,34,56.0,"
+ "\"longVal\":123,456.0,"
+ "\"longObj\":123,456.0,"
+ "\"floatVal\":\"12,3456.7\","
+ "\"floatObj\":\"12,3456.7\","
+ "\"doubleVal\":\"12,3456.7\","
+ "\"doubleObj\":\"12,3456.7\","
+ "\"decimalObj\":\"¥12_3456.7\","
+ "\"decimalShp\":\"¥12,3456.7\","
+ "\"integerObj\":\"¥1_2345_6789.0\","
+ "\"integerShp\":\"¥1,2345,6789.0\"}"
, decFmt);
Assertions.assertEquals(
"{\"intVal\":12,34,56.0,"
+ "\"intObj\":12,34,56.0,"
+ "\"longVal\":123,456.0,"
+ "\"longObj\":123,456.0,"
+ "\"floatVal\":\"12,3456.7\","
+ "\"floatObj\":\"12,3456.7\","
+ "\"doubleVal\":\"12,3456.7\","
+ "\"doubleObj\":\"12,3456.7\","
+ "\"decimalObj\":\"¥12_3456.7\","
+ "\"decimalShp\":\"¥12,3456.7\","
+ "\"integerObj\":\"¥1_2345_6789.0\","
+ "\"integerShp\":\"¥1,2345,6789.0\"}"
, decFmt);
}

@Test
@TmsLink("C13064")
public void testJsSafe() throws JsonProcessingException {
TreeMap<String, Long> js = new TreeMap<>();
js.put("maxSafe0", 9007199254740990L);
js.put("maxSafe1", 9007199254740991L);
js.put("maxSafe2", 9007199254740992L);
js.put("minSafe0", -9007199254740990L);
js.put("minSafe1", -9007199254740991L);
js.put("minSafe2", -9007199254740992L);
LinkedHashMap<String, Long> js = new LinkedHashMap<>();
js.put("maxSafeOk", 9007199254740990L);
js.put("maxSafe", 9007199254740991L);
js.put("maxSafeNg", 9007199254740992L);
js.put("minSafeOk", -9007199254740990L);
js.put("minSafe", -9007199254740991L);
js.put("minSafeNg", -9007199254740992L);
final String jsFmt = objectMapper.writeValueAsString(js);
log.info(jsFmt);
Assertions.assertEquals("{\"maxSafe0\":9007199254740990,"
+ "\"maxSafe1\":\"9007199254740991\","
+ "\"maxSafe2\":\"9007199254740992\","
+ "\"minSafe0\":-9007199254740990,"
+ "\"minSafe1\":\"-9007199254740991\","
+ "\"minSafe2\":\"-9007199254740992\"}"
, jsFmt);
Assertions.assertEquals(
"{"
+ "\"maxSafeOk\":9007199254740990,"
+ "\"maxSafe\":9007199254740991,"
+ "\"maxSafeNg\":\"9007199254740992\","
+ "\"minSafeOk\":-9007199254740990,"
+ "\"minSafe\":-9007199254740991,"
+ "\"minSafeNg\":\"-9007199254740992\""
+ "}"
, jsFmt);
}

@Data
Expand Down
Loading

0 comments on commit 89cf397

Please sign in to comment.