-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
hsweb-easy-orm-core/src/main/java/org/hswebframework/ezorm/core/DefaultExtensible.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,36 @@ | ||
package org.hswebframework.ezorm.core; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAnyGetter; | ||
import com.fasterxml.jackson.annotation.JsonAnySetter; | ||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import lombok.Setter; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Setter | ||
public class DefaultExtensible implements Extensible { | ||
|
||
private Map<String, Object> extensions; | ||
|
||
@JsonIgnore | ||
public Map<String, Object> getExtensions() { | ||
return extensions; | ||
} | ||
|
||
@Override | ||
@JsonAnyGetter | ||
public Map<String, Object> extensions() { | ||
return extensions == null ? Collections.emptyMap() : extensions; | ||
} | ||
|
||
@Override | ||
@JsonAnySetter | ||
public synchronized void setExtension(String property, Object value) { | ||
if (extensions == null) { | ||
extensions = new HashMap<>(); | ||
} | ||
extensions.put(property, value); | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
hsweb-easy-orm-core/src/test/java/org/hswebframework/ezorm/core/DefaultExtensibleTest.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,31 @@ | ||
package org.hswebframework.ezorm.core; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.SneakyThrows; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
public class DefaultExtensibleTest { | ||
|
||
|
||
@Test | ||
@SneakyThrows | ||
public void testJson() { | ||
DefaultExtensible entity = new DefaultExtensible(); | ||
|
||
entity.setExtension("extName", "test"); | ||
|
||
ObjectMapper mapper = new ObjectMapper(); | ||
|
||
String json = mapper.writerFor(DefaultExtensible.class).writeValueAsString(entity); | ||
|
||
System.out.println(json); | ||
DefaultExtensible decoded = mapper.readerFor(DefaultExtensible.class).readValue(json); | ||
|
||
assertNotNull(decoded.getExtension("extName")); | ||
|
||
assertEquals(entity.getExtension("extName"), decoded.getExtension("extName")); | ||
} | ||
|
||
} |