use of org.netxms.websvc.json.adapters.MacAddressAdapter in project netxms by netxms.
the class JsonTools method jsonFromObject.
/**
* Create JSON representation for given object
*
* @param object object to serialize
* @return JSON code
*/
public static String jsonFromObject(Object object, Set<String> fields) {
if ((object instanceof JsonObject) || (object instanceof JsonArray) || (object instanceof JSONObject) || (object instanceof JSONArray))
return JsonFilter.createFilter(object, fields).filter().toString();
if (object instanceof ResponseContainer)
return ((ResponseContainer) object).toJson(fields);
GsonBuilder builder = new GsonBuilder();
// FIXME: remove for production
builder.setPrettyPrinting();
builder.registerTypeAdapter(Date.class, new DateAdapter());
builder.registerTypeAdapter(InetAddress.class, new InetAddressAdapter());
builder.registerTypeAdapter(InetAddressEx.class, new InetAddressExAdapter());
builder.registerTypeAdapter(MacAddress.class, new MacAddressAdapter());
builder.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getAnnotation(Internal.class) != null;
}
@Override
public boolean shouldSkipClass(Class<?> c) {
return c.isAnnotationPresent(Internal.class);
}
});
if ((fields != null) && !fields.isEmpty()) {
return JsonFilter.createFilter(builder.create().toJsonTree(object), fields).filter().toString();
}
return builder.create().toJson(object);
}
Aggregations