Skip to content

Commit 4ba1d7c

Browse files
committed
refactor: 优化
1 parent acf6453 commit 4ba1d7c

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
}

hsweb-easy-orm-core/src/main/java/org/hswebframework/ezorm/core/Extensible.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.hswebframework.ezorm.core;
22

3+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
34
import com.fasterxml.jackson.annotation.JsonAnySetter;
45

56
import 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
/**
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)