Search in sources :

Example 1 with InetAddressExAdapter

use of org.netxms.websvc.json.adapters.InetAddressExAdapter 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);
}
Also used : InetAddressAdapter(org.netxms.websvc.json.adapters.InetAddressAdapter) GsonBuilder(com.google.gson.GsonBuilder) Internal(org.netxms.base.annotations.Internal) JSONArray(org.json.JSONArray) JsonObject(com.google.gson.JsonObject) InetAddressExAdapter(org.netxms.websvc.json.adapters.InetAddressExAdapter) JsonArray(com.google.gson.JsonArray) DateAdapter(org.netxms.websvc.json.adapters.DateAdapter) JSONObject(org.json.JSONObject) ExclusionStrategy(com.google.gson.ExclusionStrategy) FieldAttributes(com.google.gson.FieldAttributes) MacAddressAdapter(org.netxms.websvc.json.adapters.MacAddressAdapter)

Aggregations

ExclusionStrategy (com.google.gson.ExclusionStrategy)1 FieldAttributes (com.google.gson.FieldAttributes)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 Internal (org.netxms.base.annotations.Internal)1 DateAdapter (org.netxms.websvc.json.adapters.DateAdapter)1 InetAddressAdapter (org.netxms.websvc.json.adapters.InetAddressAdapter)1 InetAddressExAdapter (org.netxms.websvc.json.adapters.InetAddressExAdapter)1 MacAddressAdapter (org.netxms.websvc.json.adapters.MacAddressAdapter)1