Browse Source

Using Jackson instead of Fastjson

pull/2/head
simon 5 years ago
parent
commit
bb089288c4
  1. 117
      dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/JSONUtilsTest.java
  2. 14
      dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java
  3. 79
      dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java

117
dolphinscheduler-alert/src/test/java/org/apache/dolphinscheduler/alert/utils/JSONUtilsTest.java

@ -1,117 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.dolphinscheduler.alert.utils;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.apache.dolphinscheduler.common.utils.*;
import static org.junit.Assert.*;
public class JSONUtilsTest {
private static final Logger logger = LoggerFactory.getLogger(JSONUtilsTest.class);
public List<LinkedHashMap<String, Object>> list = new ArrayList<>();
public String expected = null;
@Before
public void setUp() throws Exception {
//Define expected json string
expected = "[{\"mysql service name\":\"mysql200\",\"mysql address\":\"192.168.xx.xx\",\"port\":\"3306\",\"no index of number\":\"80\",\"database client connections\":\"190\"}]";
//Initial map
LinkedHashMap<String, Object> map = new LinkedHashMap<>();
map.put("mysql service name", "mysql200");
map.put("mysql address", "192.168.xx.xx");
map.put("port", "3306");
map.put("no index of number", "80");
map.put("database client connections", "190");
//Add map into list
list.add(map);
}
/**
* Test toJsonString
*/
@Test
public void testToJsonString() {
//Invoke toJsonString
String result = JSONUtils.toJsonString(list);
logger.info(result);
//Equal result with expected string
assertEquals(result, expected);
// assertEquals(result, "[{\"database client connections\":\"190\",\"mysql address\":\"192.168.xx.xx\",\"mysql service name\":\"mysql200\",\"no index of number\":\"80\",\"port\":\"3306\"}]");
//If param is null, then return null string
result = JSONUtils.toJsonString(null);
logger.info(result);
assertEquals("null", result);
}
/**
* Test toList
*/
@Test
public void testToList() {
//Invoke toList
List<LinkedHashMap> result = JSONUtils.toList(expected, LinkedHashMap.class);
//Equal list size=1
assertEquals(1, result.size());
//Transform entity to LinkedHashMap<String, Object>
LinkedHashMap<String, Object> entity = result.get(0);
//Equal expected values
assertEquals("mysql200", entity.get("mysql service name"));
assertEquals("192.168.xx.xx", entity.get("mysql address"));
assertEquals("3306", entity.get("port"));
assertEquals("80", entity.get("no index of number"));
assertEquals("190", entity.get("database client connections"));
//If param is null, then return empty list
result = JSONUtils.toList(null, LinkedHashMap.class);
assertNotNull(result);
assertTrue(result.isEmpty());
//If param is incorrect, then return empty list and log error message
result = JSONUtils.toList("}{", LinkedHashMap.class);
assertNotNull(result);
assertTrue(result.isEmpty());
}
}

14
dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java

@ -108,20 +108,6 @@ public class JSONUtils {
return null; return null;
} }
public static <T> T parseObject(String json, TypeReference typeReference) {
if (StringUtils.isEmpty(json)) {
return null;
}
try {
return objectMapper.readValue(json, typeReference);
} catch (Exception e) {
logger.error("parse object exception!", e);
}
return null;
}
/** /**
* json to list * json to list
* *

79
dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java

@ -16,6 +16,7 @@
*/ */
package org.apache.dolphinscheduler.common.utils; package org.apache.dolphinscheduler.common.utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory; import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
@ -29,20 +30,66 @@ import java.util.*;
public class JSONUtilsTest { public class JSONUtilsTest {
@Test
public void createArrayNodeTest() {
Property property = new Property();
property.setProp("ds");
property.setDirect(Direct.IN);
property.setType(DataType.VARCHAR);
property.setValue("sssssss");
String str = "[{\"prop\":\"ds\",\"direct\":\"IN\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"},{\"prop\":\"ds\",\"direct\":\"IN\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"}]";
JsonNode jsonNode = JSONUtils.toJsonNode(property);
ArrayNode arrayNode = JSONUtils.createArrayNode();
ArrayList<JsonNode> objects = new ArrayList<>();
objects.add(jsonNode);
objects.add(jsonNode);
ArrayNode jsonNodes = arrayNode.addAll(objects);
String s = JSONUtils.toJsonString(jsonNodes);
Assert.assertEquals(s, str);
}
@Test
public void toJsonNodeTest() {
Property property = new Property();
property.setProp("ds");
property.setDirect(Direct.IN);
property.setType(DataType.VARCHAR);
property.setValue("sssssss");
String str = "{\"prop\":\"ds\",\"direct\":\"IN\",\"type\":\"VARCHAR\",\"value\":\"sssssss\"}";
JsonNode jsonNodes = JSONUtils.toJsonNode(property);
String s = JSONUtils.toJsonString(jsonNodes);
Assert.assertEquals(s, str);
}
@Test
public void createObjectNodeTest() {
String jsonStr = "{\"a\":\"b\",\"b\":\"d\"}";
ObjectNode objectNode = JSONUtils.createObjectNode();
objectNode.put("a","b");
objectNode.put("b","d");
String s = JSONUtils.toJsonString(objectNode);
Assert.assertEquals(s, jsonStr);
}
@Test @Test
public void toMap() { public void toMap() {
String jsonStr = "{\"id\":\"1001\",\"name\":\"Jobs\"}"; String jsonStr = "{\"id\":\"1001\",\"name\":\"Jobs\"}";
Map<String,String> models = JSONUtils.toMap(jsonStr); Map<String, String> models = JSONUtils.toMap(jsonStr);
Assert.assertEquals("1001", models.get("id")); Assert.assertEquals("1001", models.get("id"));
Assert.assertEquals("Jobs", models.get("name")); Assert.assertEquals("Jobs", models.get("name"));
} }
@Test @Test
public void convert2Property(){ public void convert2Property() {
Property property = new Property(); Property property = new Property();
property.setProp("ds"); property.setProp("ds");
property.setDirect(Direct.IN); property.setDirect(Direct.IN);
@ -56,7 +103,7 @@ public class JSONUtilsTest {
@Test @Test
public void String2MapTest(){ public void String2MapTest() {
String str = list2String(); String str = list2String();
List<LinkedHashMap> maps = JSONUtils.toList(str, List<LinkedHashMap> maps = JSONUtils.toList(str,
@ -70,31 +117,21 @@ public class JSONUtilsTest {
Assert.assertEquals("190", maps.get(0).get("database client connections")); Assert.assertEquals("190", maps.get(0).get("database client connections"));
} }
public String list2String(){ public String list2String() {
LinkedHashMap<String, String> map1 = new LinkedHashMap<>(); LinkedHashMap<String, String> map1 = new LinkedHashMap<>();
map1.put("mysql service name","mysql200"); map1.put("mysql service name", "mysql200");
map1.put("mysql address","192.168.xx.xx"); map1.put("mysql address", "192.168.xx.xx");
map1.put("port","3306"); map1.put("port", "3306");
map1.put("no index of number","80"); map1.put("no index of number", "80");
map1.put("database client connections","190"); map1.put("database client connections", "190");
List<LinkedHashMap<String, String>> maps = new ArrayList<>(); List<LinkedHashMap<String, String>> maps = new ArrayList<>();
maps.add(0,map1); maps.add(0, map1);
String resultJson = JSONUtils.toJsonString(maps); String resultJson = JSONUtils.toJsonString(maps);
return resultJson; return resultJson;
} }
@Test
public void testToJson() {
Map<String, String> map = new HashMap<>();
map.put("foo","bar");
Assert.assertEquals("{\"foo\":\"bar\"}", JSONUtils.toJsonString(map));
Assert.assertEquals(
String.valueOf((Object) null), JSONUtils.toJsonString(null));
}
@Test @Test
public void testParseObject() { public void testParseObject() {
Assert.assertNull(JSONUtils.parseObject("")); Assert.assertNull(JSONUtils.parseObject(""));
@ -124,7 +161,7 @@ public class JSONUtilsTest {
@Test @Test
public void testToMap() { public void testToMap() {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
map.put("foo","bar"); map.put("foo", "bar");
Assert.assertTrue(map.equals(JSONUtils.toMap( Assert.assertTrue(map.equals(JSONUtils.toMap(
"{\n" + "\"foo\": \"bar\"\n" + "}"))); "{\n" + "\"foo\": \"bar\"\n" + "}")));

Loading…
Cancel
Save