Search in sources :

Example 1 with FieldAttributes

use of com.google.gson.FieldAttributes in project MVCHelper by LuckyJayce.

the class ABSTestCaseFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    this.inflater = inflater;
    View view = inflater.inflate(R.layout.testcase, container, false);
    taskHelper = new TaskHelper<>();
    GsonBuilder builder = new GsonBuilder();
    // 格式化输出
    builder.setPrettyPrinting();
    // builder.serializeNulls();
    builder.addSerializationExclusionStrategy(new ExclusionStrategy() {

        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            // true; //按注解排除
            return false;
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            // 直接排除某个类 ,return true为排除
            return clazz == Gson.class || clazz == Bitmap.class;
        }
    }).create();
    gson = builder.create();
    recyclerView = (RecyclerView) view.findViewById(R.id.testcase2_recyclerView);
    paramsRecyclerView = (LinearLayout) view.findViewById(R.id.testcase2_params_recyclerView);
    resultTextView = (TextView) view.findViewById(R.id.testcase2_result_textView);
    runButton = (Button) view.findViewById(R.id.testcase2_run_button);
    resetButton = (Button) view.findViewById(R.id.testcase2_reset_button);
    itemRunButton = view.findViewById(R.id.testcase2_run2_button);
    resultStateTextView = (TextView) view.findViewById(R.id.testcase2_resultState_textView);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    recyclerView.setAdapter(tasksAdapter = new TasksAdapter());
    // recyclerView.addItemDecoration(new DividerItemDecoration(getContext()));
    // 
    // // paramsRecyclerView.setLayoutManager(new
    // // LinearLayoutManager(getContext()));
    // // paramsRecyclerView.addItemDecoration(new
    // // DividerItemDecoration(getContext()));
    // // paramsRecyclerView.setAdapter(paramsAdapter = new ParamsAdapter());
    datas = getTestCaseDatas();
    resetButton.setOnClickListener(onClickListener);
    runButton.setOnClickListener(onClickListener);
    itemRunButton.setOnClickListener(onClickListener);
    tasksAdapter.setOnItemClickListener(onItemClickListener);
    resultTextView.setOnClickListener(onClickListener);
    updateRight();
    return view;
}
Also used : Bitmap(android.graphics.Bitmap) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ExclusionStrategy(com.google.gson.ExclusionStrategy) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager) View(android.view.View) TextView(android.widget.TextView) RecyclerView(android.support.v7.widget.RecyclerView) FieldAttributes(com.google.gson.FieldAttributes)

Example 2 with FieldAttributes

use of com.google.gson.FieldAttributes in project acs-aem-commons by Adobe-Consulting-Services.

the class ControlledProcessManagerServlet method doGet.

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    String action = request.getRequestPathInfo().getSelectorString();
    Object result = null;
    try {
        switch(action) {
            case "start":
                result = doStartProcess(request);
                break;
            case "list":
                result = doProcessList();
                break;
            case "status":
                result = doProcessStatusCheck(request);
                break;
            case "halt":
                result = doHaltProcess(request);
                break;
            case "haltAll":
            case "halt.all":
            case "halt-all":
                result = doHaltAllProcesses(request);
                break;
            case "purge":
                result = doPurgeCompleted(request);
                break;
            default:
                throw new IllegalArgumentException("Action not understood.");
        }
    } catch (Exception ex) {
        result = "Exception occurred " + ex.getMessage();
        LOG.error(ex.getMessage() + " -- End of line.", ex);
    }
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.addSerializationExclusionStrategy(new ExclusionStrategy() {

        @Override
        public boolean shouldSkipField(FieldAttributes fa) {
            return (fa.hasModifier(Modifier.TRANSIENT) || fa.hasModifier(Modifier.VOLATILE));
        }

        @Override
        public boolean shouldSkipClass(Class<?> type) {
            return !type.isArray() && !type.isPrimitive() && type.getPackage().getName().startsWith("java.io");
        }
    });
    gsonBuilder.disableInnerClassSerialization();
    Gson gson = gsonBuilder.create();
    gson.toJson(result, response.getWriter());
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) ExclusionStrategy(com.google.gson.ExclusionStrategy) ServletException(javax.servlet.ServletException) RepositoryException(javax.jcr.RepositoryException) IOException(java.io.IOException) DeserializeException(com.adobe.acs.commons.mcp.util.DeserializeException) FieldAttributes(com.google.gson.FieldAttributes)

Example 3 with FieldAttributes

use of com.google.gson.FieldAttributes 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)

Example 4 with FieldAttributes

use of com.google.gson.FieldAttributes in project cuba by cuba-platform.

the class CubaJavaScriptComponent method createSharedGsonBuilder.

protected static GsonBuilder createSharedGsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.setExclusionStrategies(new ExclusionStrategy() {

        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            Expose expose = f.getAnnotation(Expose.class);
            return expose != null && !expose.serialize();
        }

        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    });
    setDefaultProperties(builder);
    return builder;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Expose(com.google.gson.annotations.Expose) ExclusionStrategy(com.google.gson.ExclusionStrategy) FieldAttributes(com.google.gson.FieldAttributes)

Aggregations

ExclusionStrategy (com.google.gson.ExclusionStrategy)4 FieldAttributes (com.google.gson.FieldAttributes)4 GsonBuilder (com.google.gson.GsonBuilder)4 Gson (com.google.gson.Gson)2 Bitmap (android.graphics.Bitmap)1 LinearLayoutManager (android.support.v7.widget.LinearLayoutManager)1 RecyclerView (android.support.v7.widget.RecyclerView)1 View (android.view.View)1 TextView (android.widget.TextView)1 DeserializeException (com.adobe.acs.commons.mcp.util.DeserializeException)1 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 Expose (com.google.gson.annotations.Expose)1 IOException (java.io.IOException)1 RepositoryException (javax.jcr.RepositoryException)1 ServletException (javax.servlet.ServletException)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