Skip to content

Commit

Permalink
support jackson annotation JsonFormat#locale
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed May 31, 2024
1 parent 6704cbd commit 951dda6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
14 changes: 13 additions & 1 deletion core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2763,6 +2763,13 @@ public static void processJacksonJsonFormat(FieldInfo fieldInfo, Annotation anno
}
break;
}
case "locale": {
String locale = (String) result;
if (!locale.isEmpty() && !"##default".equals(locale)) {
fieldInfo.locale = Locale.forLanguageTag(locale);
}
break;
}
default:
break;
}
Expand All @@ -2780,14 +2787,19 @@ public static void processJacksonJsonFormat(BeanInfo beanInfo, Annotation annota
Object result = m.invoke(annotation);
if ("pattern".equals(name)) {
String pattern = (String) result;
if (pattern.length() != 0) {
if (!pattern.isEmpty()) {
beanInfo.format = pattern;
}
} else if ("shape".equals(name)) {
String shape = ((Enum) result).name();
if ("NUMBER".equals(shape)) {
beanInfo.format = "millis";
}
} else if ("locale".equals(name)) {
String locale = (String) result;
if (!locale.isEmpty() && !"##default".equals(locale)) {
beanInfo.locale = Locale.forLanguageTag(locale);
}
}
} catch (Throwable ignored) {
// ignored
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,25 @@ public static class Bean5 {
@JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Date time;
}

@Test
public void test6() throws Exception {
Bean6 bean = new Bean6();
bean.time = new Date();

ObjectMapper objectMapper = new ObjectMapper();
String fastjson = JSON.toJSONString(bean);
String jackson = objectMapper.writeValueAsString(bean);
assertEquals(jackson, fastjson);
//
// Bean6 parsed0 = objectMapper.readValue(jackson, Bean6.class);
// Bean6 parsed1 = JSON.parseObject(fastjson, Bean6.class);
// assertEquals(parsed0.time.getTime(), parsed1.time.getTime());
}

@Data
public static class Bean6 {
@JsonFormat(pattern = "yyyy-MM-dd", locale = "zh_CN")
private Date time;
}
}

0 comments on commit 951dda6

Please sign in to comment.