Skip to content

Commit

Permalink
support jackson annotation JsonFormat#shape=OBJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
wenshao committed Jun 6, 2024
1 parent 5cbbe92 commit 82f1cfc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/util/BeanUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2794,6 +2794,8 @@ public static void processJacksonJsonFormat(BeanInfo beanInfo, Annotation annota
String shape = ((Enum) result).name();
if ("NUMBER".equals(shape)) {
beanInfo.format = "millis";
} else if ("OBJECT".equals(shape)) {
beanInfo.writeEnumAsJavaBean = true;
}
} else if ("locale".equals(name)) {
String locale = (String) result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,31 @@ public static class Bean6 {
@JsonFormat(pattern = "yyyy-MM-dd", locale = "zh-CN", timezone = "Asia/Shanghai")
private Date time;
}

@Test
public void test7() throws Exception {
Level level = Level.LEVEL1;

ObjectMapper objectMapper = new ObjectMapper();
String fastjson = JSON.toJSONString(level);
String jackson = objectMapper.writeValueAsString(level);
assertEquals(jackson, fastjson);
}

@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum Level {
LEVEL1("level1"),
LEVEL2("level2"),
LEVEL3("level3");

String label;

Level(String label) {
this.label = label;
}

public String getLabel() {
return label;
}
}
}

0 comments on commit 82f1cfc

Please sign in to comment.