DataTools.ConvertObjectToJson Maven / Gradle / Ivy
package DataTools;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
Copyright 2016 Alianza Inc.
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.
*/
/**
Generates various types of randomized unique data
*/
public class ConvertObjectToJson {
/**
* Converts a data model into a JSONObject
* @param model Data model to be converted
* @return JSONObject
*/
public JSONObject convertToJson(T model) {
JSONObject json = null;
try {
json = removeNull(handleEnums(model));
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
/**
* If the key or value of a JSONObject is null, remove that key (recursive function for JSONObjects within JSONObjects)
* @param json JSONObject to remove null from
* @return JSONObject
*/
private JSONObject removeNull(JSONObject json) {
Iterator keys = null;
try {
keys = new JSONObject(json.toString()).keys();
} catch (JSONException e) {
e.printStackTrace();
}
//full json has null for non populated data, we just remove those
for (String key : iterable(keys)) {
try {
if (json.isNull(key) || json.getString(key).startsWith("null") || json.getString(key).equals("{}"))
json.remove(key);
//handle recursive json objects
else if (json.getString(key).startsWith("{") && json.getString(key).endsWith("}"))
json.put(key, removeNull(new JSONObject(json.getString(key))));
} catch (JSONException e) {
e.printStackTrace();
}
}
return json;
}
/**
* Used for looping through json keys
* @param it
* @param
* @return it
*/
private static Iterable iterable(final Iterator it) {
return () -> it;
}
/**
* this method takes a model with all its data set
* returns a json object of the model,
* all fields from the model will be in the json object
* null values will need to be removed *removeNull(handleEnums(model)
* method is complex because of handling the enums,
* normal conversion to json with enums present will cause failures
* @param model Data model to convert to Json
* @return JSONObject
*/
private JSONObject handleEnums(Object model) {
JSONObject json = new JSONObject();
Method[] methods = model.getClass().getMethods();
String modelType = ".model";
String enumType = ".type";
//populate new one, leave out enum fields
for (Method get : methods) {
//setup by finding gets and sets
String methName = get.getName();
try {
//check to see if it is a method call we care about it
if ((methName.startsWith("get") || methName.startsWith("is")) && get.getParameterCount() == 0 && get.invoke(model) != null) {
Package returnType = get.getReturnType().getPackage();
//handle if it is a list
if (get.invoke(model) instanceof java.util.List) {
List