
com.github.becausetesting.json.JSONUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons Show documentation
Show all versions of commons Show documentation
A common libraries used for testing framework.
/**
* Project Name:commons
* File Name:JSONUtils.java
* Package Name:com.github.becausetesting.json
* Date:Apr 17, 20162:30:48 PM
* Copyright (c) 2016, [email protected] All Rights Reserved.
*
*/
package com.github.becausetesting.json;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* ClassName:JSONUtils
* Function: TODO ADD FUNCTION.
* Reason: TODO ADD REASON.
* Date: Apr 17, 2016 2:30:48 PM
* @author Administrator
* @version 1.0.0
* @since JDK 1.8
*/
public class JSONUtils {
private static Gson gson;
static{
GsonBuilder gsonBuilder = new GsonBuilder();
gson = gsonBuilder.setPrettyPrinting().serializeNulls().create();
}
/**
* toJson: get the josn
* @author Administrator
* @param jsonElement json object.
* @return the return string.
* @since JDK 1.8
*/
public String fromObject(Object jsonElement){
return gson.toJson(jsonElement);
}
public T toJson(String json,Class classOfT){
return gson.fromJson(json, classOfT);
}
public JsonElement toJsonElementParse(String json){
JsonParser parser=new JsonParser();
JsonElement JsonElement= parser.parse(json);
//JsonElement jsonElement = gson.fromJson(json, JsonElement.class);
//JsonArray jsonArray = jsonElement.getAsJsonArray();
return JsonElement;
}
public JsonElement toJsonElement(String json){
JsonElement jsonElement = gson.fromJson(json, JsonElement.class);
//JsonObject jsonObject = jsonElement.getAsJsonObject();
return jsonElement;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy