diff options
author | 2020-03-19 11:59:02 +0000 | |
---|---|---|
committer | 2020-03-19 11:59:02 +0000 | |
commit | 3e90fc1056791e629916a29b12b7b14bcb1e2f15 (patch) | |
tree | 7ee7eadcdc665396f5456537a17f01b064841b06 /vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java | |
parent | 2b913c12ea8ff1108782132f540333219537c837 (diff) | |
parent | 427fb7539233ece7759e27e04fa3b11d17b66ea7 (diff) |
Merge "Migrate to gson"
Diffstat (limited to 'vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java')
-rw-r--r-- | vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java | 44 |
1 files changed, 19 insertions, 25 deletions
diff --git a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java index ce6ae72e..7f5ad88e 100644 --- a/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java +++ b/vnfmarket-be/vnf-sdk-marketplace/src/main/java/org/onap/vnfsdk/marketplace/common/FileUtil.java @@ -30,13 +30,10 @@ import java.util.zip.ZipFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.fasterxml.jackson.core.JsonGenerationException; -import com.fasterxml.jackson.core.JsonParseException; -/** note jackson has security vulnerabilities */ -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; - +import com.google.gson.Gson; +import com.google.gson.stream.JsonReader; +import java.io.FileWriter; +import java.io.FileReader; public final class FileUtil { @@ -45,6 +42,7 @@ public final class FileUtil { private static final int BUFFER_SIZE = 2 * 1024 * 1024; private static final int MAX_PACKAGE_SIZE = 50 * 1024 * 1024; + private static Gson gson = new Gson(); private FileUtil() { //Empty constructor @@ -150,16 +148,11 @@ public final class FileUtil { deleteFile(fileAbsPath); } - ObjectMapper mapper = new ObjectMapper(); - try { - mapper.writeValue(new File(fileAbsPath), obj); + try(FileWriter writer = new FileWriter(new File(fileAbsPath))) { + gson.toJson(obj, writer); bResult = true; - } catch (JsonGenerationException e) { - logger.info("JsonGenerationException Exception: writeJsonDatatoFile-->" + fileAbsPath, e); - } catch (JsonMappingException e) { - logger.info("JsonMappingException Exception: writeJsonDatatoFile-->" + fileAbsPath, e); - } catch (IOException e) { - logger.info("IOException Exception: writeJsonDatatoFile-->" + fileAbsPath, e); + } catch (Exception e) { //NOSONAR + logger.info("Exception: writeJsonDatatoFile-->" + fileAbsPath, e); } return bResult; } @@ -173,15 +166,16 @@ public final class FileUtil { logger.info("read JsonData from file : {}" , fileAbsPath); T obj = null; - ObjectMapper mapper = new ObjectMapper(); - mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); - try { - obj = mapper.readValue(new File(fileAbsPath), clazz); - } catch (JsonParseException e1) { - logger.info("JsonParseException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1); - } catch (JsonMappingException e1) { - logger.info("JsonMappingException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1); - } catch (IOException e1) { + /* + Gson will ignore the unknown fields and simply match the fields that it's able to. + ref: https://www.baeldung.com/gson-deserialization-guide + + By default, Gson just ignores extra JSON elements that do not have matching Java fields. + ref: https://programmerbruce.blogspot.com/2011/06/gson-v-jackson.html + */ + try(JsonReader jsonReader = new JsonReader(new FileReader(fileAbsPath))) { + obj = gson.fromJson(jsonReader, clazz); + } catch (Exception e1) { //NOSONAR logger.info("IOException Exception: writeJsonDatatoFile-->" + fileAbsPath, e1); } return obj; |