Skip to content

Commit

Permalink
refactor: 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Dec 20, 2024
1 parent acf6453 commit 4ba1d7c
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.hswebframework.ezorm.core;

import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;

import java.util.Map;
Expand All @@ -17,7 +18,7 @@ public interface Extensible {
*
* @return 扩展属性
*/
@JsonAnySetter
@JsonAnyGetter
Map<String, Object> extensions();

/**
Expand Down
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"));
}

}

0 comments on commit 4ba1d7c

Please sign in to comment.