Removed thed use of deprecated "readFileToString"
[cli.git] / main / src / main / java / org / onap / cli / main / utils / OnapCliArgsParser.java
index a07c08f..53e9066 100644 (file)
@@ -19,6 +19,7 @@ package org.onap.cli.main.utils;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -33,18 +34,23 @@ import org.onap.cli.fw.input.OnapCommandParameter;
 import org.onap.cli.fw.input.OnapCommandParameterType;
 import org.onap.cli.main.error.OnapCliArgumentValueMissing;
 import org.onap.cli.main.error.OnapCliInvalidArgument;
-import org.yaml.snakeyaml.Yaml;
+import com.esotericsoftware.yamlbeans.YamlReader;
 
-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 +199,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);
         }
     }
@@ -213,7 +225,7 @@ public class OnapCliArgsParser {
         try {
             File file = new File(input);
             if (file.isFile()) {
-                return FileUtils.readFileToString(file);
+                return FileUtils.readFileToString(file, (Charset) null);
             } else {
                 return input;
             }
@@ -227,8 +239,9 @@ public class OnapCliArgsParser {
         try {
             File file = new File(input);
             if (file.isFile()) {
-                String value = FileUtils.readFileToString(file);
-                new Yaml().load(value);
+                String value = FileUtils.readFileToString(file, (Charset) null);
+                YamlReader reader = new YamlReader(value);
+                value = (String) reader.read();
                 return value;
             } else {
                 return input;
@@ -255,21 +268,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);
         }
     }