-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix jackson annotation JsonFormat compatible, for issue #2836
- Loading branch information
Showing
2 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
core/src/test/java/com/alibaba/fastjson2/issues_2800/Issue2836.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.alibaba.fastjson2.issues_2800; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.Serializable; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class Issue2836 { | ||
@Getter | ||
@JsonFormat(shape = JsonFormat.Shape.OBJECT) | ||
public enum Type | ||
implements Serializable { | ||
A("1", "1"), | ||
B("2", "2"), | ||
C("3", "3"); | ||
private final String code; | ||
private final String msg; | ||
|
||
Type(String code, String msg) { | ||
this.code = code; | ||
this.msg = msg; | ||
} | ||
} | ||
|
||
@AllArgsConstructor | ||
@Data | ||
public static class User | ||
implements Serializable { | ||
private String name; | ||
private Type type; | ||
} | ||
|
||
@Test | ||
public void test() throws Exception { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
User user = new User("test", Type.B); | ||
String test = JSON.toJSONString(user); | ||
String jacksonResult = objectMapper.writeValueAsString(user); | ||
assertEquals(jacksonResult, test); | ||
String jsonString = JSON.toJSONString(Type.A); | ||
assertEquals( | ||
objectMapper.writeValueAsString(Type.A), | ||
jsonString); | ||
} | ||
} |