JsonUtils for JSON format.
Issue-ID: SDC-1079
Change-Id: I8879652751a0c0ae9d2597e33997fc32a9baa816
Signed-off-by: YuanHu <yuan.hu1@zte.com.cn>
import org.onap.sdc.workflowdesigner.resources.entity.ExtActivityDisplayInfo;
import org.onap.sdc.workflowdesigner.resources.entity.ExtendActivity;
import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;
+import org.onap.sdc.workflowdesigner.utils.JsonUtils;
import org.onap.sdc.workflowdesigner.utils.RestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.codahale.metrics.annotation.Timed;
-import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
*/
private ExtendActivity[] retriveExtActivites(String sence) throws IOException {
String json = FileCommonUtils.readString(EXT_ACTIVITIES_FILE_NAME);
- Gson gson = new Gson();
- return gson.fromJson(json, ExtendActivity[].class);
+ return JsonUtils.fromJson(json, ExtendActivity[].class);
}
*/
private ExtActivityDisplayInfo retriveDisplayInfo(String sence) throws IOException {
String json = FileCommonUtils.readString(EXT_ACTIVITIES_DISPLAY_INFO_FILE_NAME);
- Gson gson = new Gson();
- return gson.fromJson(json, ExtActivityDisplayInfo.class);
+ return JsonUtils.fromJson(json, ExtActivityDisplayInfo.class);
}
}
\r
private String zh_CN;\r
\r
+ /**\r
+ * \r
+ */\r
+ public I18nString() {\r
+ super();\r
+ }\r
+\r
+ /**\r
+ * @param en_US\r
+ * @param zh_CN\r
+ */\r
+ public I18nString(String en_US, String zh_CN) {\r
+ super();\r
+ this.en_US = en_US;\r
+ this.zh_CN = zh_CN;\r
+ }\r
+\r
/**\r
* @return the en_US\r
*/\r
--- /dev/null
+/**\r
+ * Copyright (c) 2018 ZTE Corporation.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the Apache License, Version 2.0\r
+ * and the Eclipse Public License v1.0 which both accompany this distribution,\r
+ * and are available at http://www.eclipse.org/legal/epl-v10.html\r
+ * and http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Contributors:\r
+ * ZTE - initial API and implementation and/or initial documentation\r
+ */\r
+package org.onap.sdc.workflowdesigner.utils;\r
+\r
+import com.google.gson.Gson;\r
+\r
+/**\r
+ *\r
+ */\r
+public class JsonUtils {\r
+ /**\r
+ * \r
+ * @param json\r
+ * @param clazz\r
+ * @return\r
+ */\r
+ public static <T> T fromJson(String json, Class<T> clazz) {\r
+ Gson gson = new Gson();\r
+ return gson.fromJson(json, clazz);\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @param t\r
+ * @return\r
+ */\r
+ public static <T> String toJson(T t) {\r
+ Gson gson = new Gson();\r
+ return gson.toJson(t);\r
+ }\r
+ \r
+}\r
import org.junit.Test;\r
import org.onap.sdc.workflowdesigner.resources.entity.ExtendActivity;\r
import org.onap.sdc.workflowdesigner.utils.FileCommonUtils;\r
-\r
-import com.google.gson.Gson;\r
+import org.onap.sdc.workflowdesigner.utils.JsonUtils;\r
\r
/**\r
*\r
try {\r
Response response = resource.getExtActivities("test");\r
ExtendActivity[] extActivities = (ExtendActivity[]) response.getEntity();\r
- Gson gson = new Gson();\r
- FileCommonUtils.write("test.json", gson.toJson(extActivities));\r
+ FileCommonUtils.write("test.json", JsonUtils.toJson(extActivities));\r
assertEquals(extActivities.length == 0, false);\r
} catch (Exception e) {\r
e.printStackTrace();\r
--- /dev/null
+/**\r
+ * Copyright (c) 2018 ZTE Corporation.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the Apache License, Version 2.0\r
+ * and the Eclipse Public License v1.0 which both accompany this distribution,\r
+ * and are available at http://www.eclipse.org/legal/epl-v10.html\r
+ * and http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Contributors:\r
+ * ZTE - initial API and implementation and/or initial documentation\r
+ */\r
+package org.onap.sdc.workflowdesigner.utils;\r
+\r
+import static org.junit.Assert.*;\r
+\r
+import org.junit.After;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.onap.sdc.workflowdesigner.resources.entity.I18nString;\r
+\r
+/**\r
+ *\r
+ */\r
+public class JsonUtilsTest {\r
+\r
+ /**\r
+ * @throws java.lang.Exception\r
+ */\r
+ @Before\r
+ public void setUp() throws Exception {}\r
+\r
+ /**\r
+ * @throws java.lang.Exception\r
+ */\r
+ @After\r
+ public void tearDown() throws Exception {}\r
+\r
+ /**\r
+ * Test method for {@link org.onap.sdc.workflowdesigner.utils.JsonUtils#fromJson(java.lang.String, java.lang.Class)}.\r
+ */\r
+ @Test\r
+ public void testFromJson() {\r
+ String i18n = "{\"en_US\":\"Service Task\",\"zh_CN\":\"Service Task\"}";\r
+ I18nString i18nString = JsonUtils.fromJson(i18n, I18nString.class);\r
+ assertNotNull(i18nString);\r
+ }\r
+\r
+ /**\r
+ * Test method for {@link org.onap.sdc.workflowdesigner.utils.JsonUtils#toJson(java.lang.Object)}.\r
+ */\r
+ @Test\r
+ public void testToJson() {\r
+ I18nString i18nString = new I18nString("Service Task", "Service Task");\r
+ String i18n = JsonUtils.toJson(i18nString);\r
+ String expect = "{\"en_US\":\"Service Task\",\"zh_CN\":\"Service Task\"}";\r
+ assertEquals(expect, i18n);\r
+ }\r
+\r
+}\r