
com.senseidb.search.client.json.JsonSerializer Maven / Gradle / Ivy
package com.senseidb.search.client.json;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class JsonSerializer {
public static Object serialize(Object object) {
try {
return serialize(object,true);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
public static Object serialize(Object object, boolean handleCustomJsonHandler) throws JSONException {
if (object == null) {
return null;
}
if (object instanceof String || object instanceof Number || object instanceof Boolean || object.getClass().isPrimitive() || object instanceof JSONObject) {
return object;
}
CustomJsonHandler customJsonHandler = getCustomJsonHandlerByType(object.getClass());
if (customJsonHandler != null && handleCustomJsonHandler) {
JsonHandler jsonHandler = instantiate(customJsonHandler.value());
return jsonHandler.serialize(object);
}
if (object.getClass().isEnum()) {
return object.toString();
}
if (object instanceof Collection) {
Collection collection = (Collection) object;
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy