/***************************************************************************
* Copyright (c) 2014-2015 VMware, Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
package com.vmware.bdd.software.mgmt.plugin.utils;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* Author: Xiaoding Bian
* Date: 5/23/14
* Time: 3:50 PM
*/
public class SerialUtils {
public static String dataFromFile(String filePath) throws IOException,
FileNotFoundException {
StringBuffer dataStringBuffer = new StringBuffer();
FileInputStream fis = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
fis = new FileInputStream(filePath);
inputStreamReader = new InputStreamReader(fis, "UTF-8");
bufferedReader = new BufferedReader(inputStreamReader);
String line = "";
while ((line = bufferedReader.readLine()) != null) {
dataStringBuffer.append(line);
dataStringBuffer.append("\n");
}
} finally {
if (fis != null) {
fis.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (bufferedReader != null) {
bufferedReader.close();
}
}
return dataStringBuffer.toString();
}
public static <T> T getObjectByJsonString(Class<T> entityType, String jsonString) throws JsonParseException,
JsonMappingException, IOException {
ObjectMapper mapper = getMapper();
T mappedObject = null;
mappedObject = mapper.readValue(jsonString, entityType);
return mappedObject;
}
private static ObjectMapper getMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
return mapper;
}
}