增加ConvertEnumValue功能
This commit is contained in:
61
src/test/java/org/redkale/test/convert/EnumBeanTest.java
Normal file
61
src/test/java/org/redkale/test/convert/EnumBeanTest.java
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package org.redkale.test.convert;
|
||||
|
||||
import org.redkale.convert.ConvertEnumValue;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.redkale.convert.json.JsonConvert;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class EnumBeanTest {
|
||||
|
||||
private boolean main;
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
EnumBeanTest test = new EnumBeanTest();
|
||||
test.main = true;
|
||||
test.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void run() throws Exception {
|
||||
EnumBean bean = new EnumBean();
|
||||
bean.v1 = EnumKey.TWO;
|
||||
bean.v2 = 5;
|
||||
String expect = "{\"v1\":2,\"v2\":5}";
|
||||
String json = JsonConvert.root().convertTo(bean);
|
||||
if (!main) Assertions.assertEquals(expect, json);
|
||||
System.out.println(json);
|
||||
EnumBean b = JsonConvert.root().convertFrom(EnumBean.class, json);
|
||||
String js = JsonConvert.root().convertTo(b);
|
||||
System.out.println(js);
|
||||
if (!main) Assertions.assertEquals(expect, js);
|
||||
}
|
||||
|
||||
public static class EnumBean {
|
||||
|
||||
public EnumKey v1;
|
||||
|
||||
public int v2;
|
||||
}
|
||||
|
||||
@ConvertEnumValue("code")
|
||||
public static enum EnumKey {
|
||||
ONE(1), TWO(2);
|
||||
|
||||
private final int code;
|
||||
|
||||
private EnumKey(int v) {
|
||||
this.code = v;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user