Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

支持配置JSON,并自动转成相应类型 #339

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.HashSet;
import java.util.Set;

import com.google.gson.Gson;

/**
* 类工具
*
Expand Down Expand Up @@ -114,7 +116,7 @@ public static Object getValeByType(Class<?> type, Object value)
}

// trim
String dataValue = (String) value;
String dataValue = String.valueOf(value);
dataValue = dataValue.trim();

// process
Expand All @@ -124,15 +126,15 @@ public static Object getValeByType(Class<?> type, Object value)
if (typeName.equals("int") || typeName.equals("java.lang.integer")) {

if (value.equals("")) {
value = "0";
return 0;
}

return Integer.valueOf(dataValue);

} else if (typeName.equals("long") || typeName.equals("java.lang.long")) {

if (value.equals("")) {
value = "0";
return 0L;
}

return Long.valueOf(dataValue);
Expand All @@ -141,7 +143,7 @@ public static Object getValeByType(Class<?> type, Object value)
|| typeName.equals("java.lang.boolean")) {

if (value.equals("")) {
value = "false";
return false;
}

return Boolean.valueOf(dataValue);
Expand All @@ -150,14 +152,26 @@ public static Object getValeByType(Class<?> type, Object value)
|| typeName.equals("java.lang.double")) {

if (value.equals("")) {
value = "0.0";
return 0.0D;
}

return Double.valueOf(dataValue);

} else {

return value;
} else if (typeName.equals("string")
|| typeName.equals("java.lang.string")) {

return dataValue;
}
else {
try {
// 其他类型尝试JSON解析
Gson gson = new Gson();
return gson.fromJson(value.toString(), type);
} catch (Exception ex) {
ex.printStackTrace();
}
// JSON解析异常的情况下,返回null,防止待会转换继续抛异常
return null;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baidu.disconf.client.test.support.utils;

import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.baidu.disconf.client.support.utils.ClassUtils;

public class ClassUtilsTest {

@SuppressWarnings("unchecked")
@Test
public void testGetValeByType() throws Exception {
assertTrue(ClassUtils.getValeByType(Integer.class, "10086").equals(new Integer(10086)));
assertTrue(ClassUtils.getValeByType(Integer.class, "").equals(new Integer(0)));
assertTrue(ClassUtils.getValeByType(Integer.class, 0).equals(new Integer(0)));
assertTrue(ClassUtils.getValeByType(Long.class, "1688").equals(new Long(1688)));
assertTrue(ClassUtils.getValeByType(Long.class, "").equals(new Long(0)));
assertTrue(ClassUtils.getValeByType(Boolean.class, "true").equals(new Boolean(true)));
assertTrue(ClassUtils.getValeByType(Boolean.class, "").equals(new Boolean(false)));
assertTrue(ClassUtils.getValeByType(Double.class, "234.4").equals(new Double(234.4)));
assertTrue(ClassUtils.getValeByType(Double.class, "").equals(new Double(0.0)));
assertTrue(ClassUtils.getValeByType(String.class, "234.4").equals(String.valueOf(234.4)));
List<Integer> list = (List<Integer>) ClassUtils.getValeByType(List.class, "[100,200,300]");
assertTrue(list.contains(new Double(100)));
Map<String, String> map = (Map<String, String>) ClassUtils.getValeByType(Map.class, "{\"name\":\"zxh\",\"city\":\"shenzhen\"}");
assertTrue(map.get("name").equals("zxh"));
assertTrue(ClassUtils.getValeByType(List.class, "234.4,123") == null);
}

@Test
public void testGetFieldNameByGetMethodName() {
assertTrue(ClassUtils.getFieldNameByGetMethodName("getOrderNo").equals("orderNo"));
assertTrue(ClassUtils.getFieldNameByGetMethodName("isSended").equals("sended"));
assertTrue(ClassUtils.getFieldNameByGetMethodName("isA").equals("a"));
assertTrue(ClassUtils.getFieldNameByGetMethodName("someMethod") == null);
}

}