-
Notifications
You must be signed in to change notification settings - Fork 514
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add jackson annotation JsonFormat support, for issue #1003
- Loading branch information
Showing
9 changed files
with
310 additions
and
1 deletion.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
adapter/src/main/java/com/alibaba/fastjson2/adapter/jackson/annotation/JsonFormat.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,13 @@ | ||
package com.alibaba.fastjson2.adapter.jackson.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, | ||
ElementType.TYPE}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface JsonFormat { | ||
String pattern() default ""; | ||
} |
100 changes: 100 additions & 0 deletions
100
adapter/src/test/java/com/alibaba/fastjson2/adapter/jackson/databind/JsonFormatTest.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,100 @@ | ||
package com.alibaba.fastjson2.adapter.jackson.databind; | ||
|
||
import com.alibaba.fastjson2.JSON; | ||
import com.alibaba.fastjson2.JSONReader; | ||
import com.alibaba.fastjson2.JSONWriter; | ||
import com.alibaba.fastjson2.adapter.jackson.annotation.JsonFormat; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.time.LocalTime; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
public class JsonFormatTest { | ||
@Test | ||
public void test() { | ||
Bean bean = new Bean(); | ||
bean.time = LocalTime.of(12, 13, 14); | ||
String str = JSON.toJSONString(bean); | ||
assertEquals("{\"time\":\"121314\"}", str); | ||
Bean bean1 = JSON.parseObject(str, Bean.class); | ||
assertEquals(bean.time, bean1.time); | ||
} | ||
|
||
public static class Bean { | ||
@JsonFormat(pattern = "HHmmss") | ||
public LocalTime time; | ||
} | ||
|
||
@Test | ||
public void test1() { | ||
Bean1 bean = new Bean1(); | ||
bean.time = LocalTime.of(12, 13, 14); | ||
String str = JSON.toJSONString(bean); | ||
assertEquals("{\"time\":\"121314\"}", str); | ||
Bean1 bean1 = JSON.parseObject(str, Bean1.class); | ||
assertEquals(bean.time, bean1.time); | ||
} | ||
|
||
public static class Bean1 { | ||
private LocalTime time; | ||
|
||
@JsonFormat(pattern = "HHmmss") | ||
public LocalTime getTime() { | ||
return time; | ||
} | ||
|
||
@JsonFormat(pattern = "HHmmss") | ||
public void setTime(LocalTime time) { | ||
this.time = time; | ||
} | ||
} | ||
|
||
@Test | ||
public void test3() { | ||
Bean3 bean = new Bean3(); | ||
bean.time = LocalTime.of(12, 13, 14); | ||
String str = JSON.toJSONString(bean); | ||
assertEquals("{\"time\":\"121314\"}", str); | ||
Bean3 bean1 = JSON.parseObject(str, Bean3.class); | ||
assertEquals(bean.time, bean1.time); | ||
} | ||
|
||
@Test | ||
public void test3F() { | ||
Bean3 bean = new Bean3(); | ||
bean.time = LocalTime.of(12, 13, 14); | ||
String str = JSON.toJSONString(bean, JSONWriter.Feature.FieldBased); | ||
assertEquals("{\"time\":\"121314\"}", str); | ||
Bean3 bean1 = JSON.parseObject(str, Bean3.class, JSONReader.Feature.FieldBased); | ||
assertEquals(bean.time, bean1.time); | ||
} | ||
|
||
@JsonFormat(pattern = "HHmmss") | ||
public static class Bean3 { | ||
public LocalTime time; | ||
} | ||
|
||
@Test | ||
public void test4() { | ||
Bean4 bean = new Bean4(); | ||
bean.time = LocalTime.of(12, 13, 14); | ||
String str = JSON.toJSONString(bean); | ||
assertEquals("{\"time\":\"121314\"}", str); | ||
Bean4 bean1 = JSON.parseObject(str, Bean4.class); | ||
assertEquals(bean.time, bean1.time); | ||
} | ||
|
||
@JsonFormat(pattern = "HHmmss") | ||
public static class Bean4 { | ||
private LocalTime time; | ||
|
||
public LocalTime getTime() { | ||
return time; | ||
} | ||
|
||
public void setTime(LocalTime time) { | ||
this.time = time; | ||
} | ||
} | ||
} |
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
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
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
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
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
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
Oops, something went wrong.