From 132177fb71c08f157fb42037f9dd26ceb2d0ba43 Mon Sep 17 00:00:00 2001 From: "priyanka.akhade" Date: Fri, 28 Feb 2020 05:47:34 +0000 Subject: Migrate to gson Issue-ID: CLI-247 Signed-off-by: priyanka.akhade Change-Id: If66134db5503382fb45210ece883402fc130ac93 --- .../org/onap/cli/main/utils/OnapCliArgsParser.java | 39 ++++++++++++++-------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'main/src') diff --git a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java index a07c08f2..3dcb74ed 100644 --- a/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java +++ b/main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java @@ -35,16 +35,21 @@ import org.onap.cli.main.error.OnapCliArgumentValueMissing; import org.onap.cli.main.error.OnapCliInvalidArgument; import org.yaml.snakeyaml.Yaml; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonElement; +import com.google.gson.reflect.TypeToken; -import net.minidev.json.JSONObject; +import java.io.Reader; +import java.io.InputStreamReader; +import java.io.FileReader; /** * Oclip CLI utilities. * */ public class OnapCliArgsParser { + private static Gson gson = new GsonBuilder().serializeNulls().create(); /** * private Constructor. @@ -193,18 +198,24 @@ public class OnapCliArgsParser { } public static String readJsonStringFromUrl(String input, String argName) throws OnapCliInvalidArgument { - ObjectMapper mapper = new ObjectMapper(); + String jsonValue; try { File file = new File(input); if (file.isFile()) { - return mapper.readValue(file, JSONObject.class).toJSONString(); + try(Reader reader = new FileReader(file)){ + jsonValue = gson.fromJson(reader, JsonElement.class).toString(); + } + return jsonValue; } else if (input.startsWith("file:") || input.startsWith("http:") || input.startsWith("ftp:")) { URL jsonUrl = new URL(input); - return mapper.readValue(jsonUrl, JSONObject.class).toJSONString(); + try(Reader reader = new InputStreamReader(jsonUrl.openStream())){ + jsonValue = gson.fromJson(reader, JsonElement.class).toString(); + } + return jsonValue; } else { - return mapper.readValue(input, JSONObject.class).toJSONString(); + return gson.fromJson(input, JsonElement.class).toString(); } - } catch (IOException e) { + } catch (Exception e) { // NOSONAR throw new OnapCliInvalidArgument(argName, e); } } @@ -255,21 +266,21 @@ public class OnapCliArgsParser { } public static List convertJsonToListString(String arg, String json) throws OnapCliInvalidArgument { - TypeReference> mapType = new TypeReference>() { + TypeToken> mapType = new TypeToken>() { }; try { - return new ObjectMapper().readValue(json, mapType); - } catch (IOException e) { + return gson.fromJson(json, mapType.getType()); + } catch (Exception e) { // NOSONAR throw new OnapCliInvalidArgument(arg, e); } } public static Map convertJsonToMapString(String arg, String json) throws OnapCliInvalidArgument { - TypeReference> mapType = new TypeReference>() { + TypeToken> mapType = new TypeToken>() { }; try { - return new ObjectMapper().readValue(json, mapType); - } catch (IOException e) { + return gson.fromJson(json, mapType.getType()); + } catch (Exception e) { // NOSONAR throw new OnapCliInvalidArgument(arg, e); } } -- cgit 1.2.3-korg