Package com.devbliss.doctest.utils

Source Code of com.devbliss.doctest.utils.JSONHelper

package com.devbliss.doctest.utils;

import java.util.List;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

/**
* Utils class used to transform object into json-formatted {@link String}
*
* @author bmary
* @author mbankmann
*
*/
public class JSONHelper {

    /**
     * Converts the given POJO into a Json representation.
     *
     * @param obj
     * @return
     */
    public String toJson(Object obj) {
        return toJson(obj, false);
    }

    /**
     *
     * Converts the given POJO into a Json representation.
     * If prettyPrint is true, the output will be nicely formatted.
     *
     * @param obj
     * @param prettyPrint
     * @return
     */
    public String toJson(Object obj, boolean prettyPrint) {
        GsonBuilder builder = new GsonBuilder();

        if (prettyPrint) {
            builder.setPrettyPrinting();
        }

        return builder.create().toJson(obj);
    }

    /**
     *
     * Converts the given POJO to Json and will skip the given fields while doing so.
     *
     * @param obj
     * @param excludedFields
     * @return
     */
    public String toJsonAndSkipCertainFields(Object obj, final List<String> excludedFields) {
        return toJsonAndSkipCertainFields(obj, excludedFields, false);
    }

    /**
     *
     * Converts the given POJO and will skip the given fields while doing so.
     * If prettyPrint is true, the output will be nicely formatted.
     *
     * @param obj
     * @param excludedFields
     * @param prettyPrint
     * @return
     */
    public String toJsonAndSkipCertainFields(Object obj, final List<String> excludedFields,
            boolean prettyPrint) {
        ExclusionStrategy strategy = new ExclusionStrategy() {
            public boolean shouldSkipField(FieldAttributes f) {
                if (excludedFields.contains(f.getName())) {
                    return true;
                }

                return false;
            }

            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        };

        GsonBuilder builder =
                new GsonBuilder().addSerializationExclusionStrategy(strategy)
                        .addDeserializationExclusionStrategy(strategy);

        if (prettyPrint)
            builder.setPrettyPrinting();

        return builder.create().toJson(obj);
    }

    public boolean isJsonValid(String json) {
        return json != null && !json.equals("null") && !json.isEmpty() && json.startsWith("{");
    }

    /**
     *
     * Pretty prints any json input.
     *
     * @param json
     * @return
     */
    public String prettyPrintJson(String json) {
        if (!isJsonValid(json)) {
            return json;
        }

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        JsonParser jp = new JsonParser();
        JsonElement je = jp.parse(json);
        String returnvalue = gson.toJson(je);
        return returnvalue;
    }
}
TOP

Related Classes of com.devbliss.doctest.utils.JSONHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.