diff options
author | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-02-28 05:47:34 +0000 |
---|---|---|
committer | priyanka.akhade <priyanka.akhade@huawei.com> | 2020-02-29 11:26:08 +0000 |
commit | 132177fb71c08f157fb42037f9dd26ceb2d0ba43 (patch) | |
tree | f47d3c4d950e98d5bcf104aa8952f39a84e3ba6d /main | |
parent | 4f1d279e42a3326cdc97e1eacaf0fdb58ee944e7 (diff) |
Migrate to gson
Issue-ID: CLI-247
Signed-off-by: priyanka.akhade <priyanka.akhade@huawei.com>
Change-Id: If66134db5503382fb45210ece883402fc130ac93
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); } } |