File tree Expand file tree Collapse file tree 3 files changed +69
-1
lines changed
main/java/org/hswebframework/ezorm/core
test/java/org/hswebframework/ezorm/core Expand file tree Collapse file tree 3 files changed +69
-1
lines changed Original file line number Diff line number Diff line change 1+ package org .hswebframework .ezorm .core ;
2+
3+ import com .fasterxml .jackson .annotation .JsonAnyGetter ;
4+ import com .fasterxml .jackson .annotation .JsonAnySetter ;
5+ import com .fasterxml .jackson .annotation .JsonIgnore ;
6+ import lombok .Setter ;
7+
8+ import java .util .Collections ;
9+ import java .util .HashMap ;
10+ import java .util .Map ;
11+
12+ @ Setter
13+ public class DefaultExtensible implements Extensible {
14+
15+ private Map <String , Object > extensions ;
16+
17+ @ JsonIgnore
18+ public Map <String , Object > getExtensions () {
19+ return extensions ;
20+ }
21+
22+ @ Override
23+ @ JsonAnyGetter
24+ public Map <String , Object > extensions () {
25+ return extensions == null ? Collections .emptyMap () : extensions ;
26+ }
27+
28+ @ Override
29+ @ JsonAnySetter
30+ public synchronized void setExtension (String property , Object value ) {
31+ if (extensions == null ) {
32+ extensions = new HashMap <>();
33+ }
34+ extensions .put (property , value );
35+ }
36+ }
Original file line number Diff line number Diff line change 11package org .hswebframework .ezorm .core ;
22
3+ import com .fasterxml .jackson .annotation .JsonAnyGetter ;
34import com .fasterxml .jackson .annotation .JsonAnySetter ;
45
56import java .util .Map ;
@@ -17,7 +18,7 @@ public interface Extensible {
1718 *
1819 * @return 扩展属性
1920 */
20- @ JsonAnySetter
21+ @ JsonAnyGetter
2122 Map <String , Object > extensions ();
2223
2324 /**
Original file line number Diff line number Diff line change 1+ package org .hswebframework .ezorm .core ;
2+
3+ import com .fasterxml .jackson .databind .ObjectMapper ;
4+ import lombok .SneakyThrows ;
5+ import org .junit .Test ;
6+
7+ import static org .junit .Assert .*;
8+
9+ public class DefaultExtensibleTest {
10+
11+
12+ @ Test
13+ @ SneakyThrows
14+ public void testJson () {
15+ DefaultExtensible entity = new DefaultExtensible ();
16+
17+ entity .setExtension ("extName" , "test" );
18+
19+ ObjectMapper mapper = new ObjectMapper ();
20+
21+ String json = mapper .writerFor (DefaultExtensible .class ).writeValueAsString (entity );
22+
23+ System .out .println (json );
24+ DefaultExtensible decoded = mapper .readerFor (DefaultExtensible .class ).readValue (json );
25+
26+ assertNotNull (decoded .getExtension ("extName" ));
27+
28+ assertEquals (entity .getExtension ("extName" ), decoded .getExtension ("extName" ));
29+ }
30+
31+ }
You can’t perform that action at this time.
0 commit comments