com.qmetry.qaf.automation.util.JSONUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of qaf Show documentation
Show all versions of qaf Show documentation
Functional test automation framework for web, mobile-web, mobile native and web-service
/*******************************************************************************
* QMetry Automation Framework provides a powerful and versatile platform to
* author
* Automated Test Cases in Behavior Driven, Keyword Driven or Code Driven
* approach
* Copyright 2016 Infostretch Corporation
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT
* OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE
* You should have received a copy of the GNU General Public License along with
* this program in the name of LICENSE.txt in the root folder of the
* distribution. If not, see https://opensource.org/licenses/gpl-3.0.html
* See the NOTICE.TXT file in root folder of this source files distribution
* for additional information regarding copyright ownership and licenses
* of other open source software / files used by QMetry Automation Framework.
* For any inquiry or need additional information, please contact
* [email protected]
*******************************************************************************/
package com.qmetry.qaf.automation.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.qmetry.qaf.automation.core.AutomationError;
import com.qmetry.qaf.automation.keys.ApplicationProperties;
/**
* com.qmetry.qaf.automation.util.JSONUtil.java
*
* @author chirag.jayswal
*/
public class JSONUtil {
private static final Log logger = LogFactoryImpl.getLog(JSONUtil.class);
/**
*
* @param json
* @return
* @throws JSONException
*/
public static Map toMap(String json) throws JSONException {
@SuppressWarnings("unchecked")
Map map = new Gson().fromJson(json, Map.class);
return map;
}
/**
*
* @param str
* @return
*/
public static boolean isValidJsonString(String str) {
try {
new JSONObject(str);
return true;
} catch (JSONException e) {
return false;
}
}
/**
*
* @param str
* @return
*/
public static JSONArray getJsonArrayOrNull(String str) {
try {
return new JSONArray(str);
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
/**
*
* @param csv
* @return
*/
public static JSONArray getJsonArrayFromCsvOrNull(String csv){
try {
return CDL.rowToJSONArray(new JSONTokener(csv));
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
/**
* This is specifically to work with GSON. for example even "some string" is
* not valid json but gson can considers as valid json
*
* @param str
* @return
*/
public static boolean isValidGsonString(String str) {
try {
new JsonParser().parse(str);
return true;
} catch (JsonParseException e) {
return false;
}
}
/**
* @param obj
* @return
*/
static Map toMap(JSONObject obj) {
Map map = new HashMap();
if (obj != null) {
Iterator> iter = obj.keys();
while (iter.hasNext()) {
String key = (String) iter.next();
try {
map.put(key, obj.get(key));
} catch (JSONException e) {
}
}
}
return map;
}
/**
* @param str
* @return JsonElement or null if not a valid json
*/
public static JsonElement getGsonElement(String str) {
try {
return new JsonParser().parse(str);
} catch (JsonParseException e) {
return null;
}
}
/**
*
* @param file
* @param obj
*/
public static void writeJsonObjectToFile(final String file, final T obj) {
File f = new File(file);
try {
Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create();
String jsonStr = gson.toJson(obj, obj.getClass());
FileUtil.writeStringToFile(f, jsonStr, "UTF-8");
} catch (Throwable e) {
System.err.println("Unable to write : " + obj.getClass().getCanonicalName() + " in file: " + file + " :"
+ e.getMessage());
logger.error("Unable to write : " + obj.getClass().getCanonicalName() + " in file: " + file + " :"
+ e.getMessage());
}
}
/**
*
* @param file
* @return
*/
public static Object[][] getJsonArrayOfMaps(String file) {
try {
Gson gson = new Gson();
final Type DATASET_TYPE = new TypeToken>>() {
private static final long serialVersionUID = 4426388930719377223L;
}.getType();
List