diff options
Diffstat (limited to 'main')
-rw-r--r-- | main/src/main/java/org/onap/cli/main/utils/OnapCliArgsParser.java | 39 |
1 files changed, 25 insertions, 14 deletions
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<String> convertJsonToListString(String arg, String json) throws OnapCliInvalidArgument { - TypeReference<List<String>> mapType = new TypeReference<List<String>>() { + TypeToken<List<String>> mapType = new TypeToken<List<String>>() { }; 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<String, String> convertJsonToMapString(String arg, String json) throws OnapCliInvalidArgument { - TypeReference<Map<String, String>> mapType = new TypeReference<Map<String, String>>() { + TypeToken<Map<String, String>> mapType = new TypeToken<Map<String, String>>() { }; 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); } } |