Example usage for com.google.gson GsonBuilder disableHtmlEscaping

List of usage examples for com.google.gson GsonBuilder disableHtmlEscaping

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder disableHtmlEscaping.

Prototype

public GsonBuilder disableHtmlEscaping() 

Source Link

Document

By default, Gson escapes HTML characters such as < > etc.

Usage

From source file:com.bzcentre.dapiPush.DapiReceiver.java

License:Open Source License

private Object[] wrapupPushResult(String user_id, int push_status, String return_code, Set<String> deliverables,
        MsgCounter msgCounter, List<String[]> undeliverables) {
    GsonBuilder gBuilder = new GsonBuilder();
    Gson g = gBuilder.disableHtmlEscaping().serializeNulls().create();
    String query = "";
    int rows;/*  www  . j a  v a2  s .co  m*/

    if (deliverables.size() > 0) { //something has been pushed
        try {
            stmt = dbconn.createStatement();
            if (msgCounter.apns > 0) {
                query = "UPDATE `Statistics` SET `counter`=`counter`+" + msgCounter.apns + " WHERE `user_id`="
                        + user_id + " AND `provider`='apns'";
                rows = stmt.executeUpdate(query);
                if (rows == 0) {
                    query = "INSERT INTO `Statistics` (`provider`,`user_id`,`counter`) VALUES ('apns',"
                            + user_id + "," + deliverables.size() + ")";
                    stmt.executeUpdate(query);
                }
            }
            if (msgCounter.fcm > 0) {
                query = "UPDATE `Statistics` SET `counter`=`counter`+" + msgCounter.fcm + " WHERE `user_id`="
                        + user_id + " AND `provider`='fcm'";
                rows = stmt.executeUpdate(query);
                if (rows == 0) {
                    query = "INSERT INTO `Statistics` (`provider`,`user_id`,`counter`) VALUES ('fcm'," + user_id
                            + "," + deliverables.size() + ")";
                    stmt.executeUpdate(query);
                }
            }
        } catch (SQLException | NullPointerException e) {
            if (e.getClass().getName().equals("com.mysql.jdbc.CommunicationsException")) {
                if (msgCounter.apns > 0) {
                    query = "UPDATE `Statistics` SET `counter`=`counter`+" + msgCounter.apns
                            + " WHERE `user_id`=" + user_id + " AND `provider`='apns'";
                    rows = rebuildDBConnection("stmt", query);
                    if (rows == 0) {
                        query = "INSERT INTO `Statistics` (`provider`,`user_id`,`counter`) VALUES ('apns',"
                                + user_id + "," + msgCounter.apns + ")";
                        try {
                            stmt.executeUpdate(query);
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
                if (msgCounter.fcm > 0) {
                    query = "UPDATE `Statistics` SET `counter`=`counter`+" + msgCounter.fcm
                            + " WHERE `user_id`=" + user_id + " AND `provider`='fcm'";
                    rows = rebuildDBConnection("stmt", query);
                    if (rows == 0) {
                        query = "INSERT INTO `Statistics` (`provider`,`user_id`,`counter`) VALUES ('apns',"
                                + user_id + "," + msgCounter.fcm + ")";
                        try {
                            stmt.executeUpdate(query);
                        } catch (SQLException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            }
            NginxClojureRT.log.info(TAG + e.getMessage() + " \nquery=" + query);
        } finally {
            NginxClojureRT.log.info(
                    TAG + "Statistics updated with total msg count:" + (msgCounter.apns + msgCounter.fcm));
        }

    }

    // remove push succeeded items from undeliverables
    for (String deliverable : deliverables) {
        for (int i = 0; i < undeliverables.size(); i++) {
            if (deliverable.equals(undeliverables.get(i)[2])) {
                undeliverables.remove(i);
            }
        }
    }

    NginxClojureRT.log.info(TAG + " return " + push_status + " and " + undeliverables.size()
            + " undeliverables. errorCode:" + return_code + " errMeg:" + errMsg);
    // 1. There are tokens that may have been expired. dapiAir will call unregisterTokens
    if (!undeliverables.isEmpty()) {
        return new Object[] { push_status, // this should be NGX_HTTP_NOT_FOUND
                ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
                "{\"code\":\"" + return_code + "\", \"message\":" + g.toJson(undeliverables.toArray()) + "}" //response body can be string, File or Array/Collection of string or File
        };
    }

    // 2. something wrong
    if ((return_code != null && return_code.indexOf("pushOK") == -1) || push_status == NGX_HTTP_NOT_FOUND) {
        return new Object[] { push_status, ArrayMap.create(CONTENT_TYPE, "text/plain"), //headers map
                "{\"code\":\"" + return_code + "\", \"message\":\"" + errMsg + "\"}" //response body can be string, File or Array/Collection of string or File
        };
    }

    // 3. everyghing fine
    return new Object[] { push_status, // 204 OK
            null, null };
}

From source file:com.expensify.testframework.SubmitResultContainer.java

License:Microsoft Public License

public static String RawStringify(ResultBase result) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    Gson gson = gsonBuilder.create();/*from   ww  w . j  ava  2 s  .  co m*/
    gsonBuilder.serializeNulls();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
    gsonBuilder.disableHtmlEscaping();
    gsonBuilder.registerTypeHierarchyAdapter(ResultBase.class, new ResultSerializer());
    String resultJson = gson.toJson(result);
    resultJson = hackResultJsonOrder(resultJson);
    return resultJson;
}

From source file:com.g3net.tool.ObjectUtils.java

License:Apache License

public static String toString(Object obj) {

    GsonBuilder builder = new GsonBuilder();
    builder.serializeNulls();// w ww.ja v a  2  s .  c  o  m
    builder.disableHtmlEscaping();
    builder.setPrettyPrinting();
    Gson gson = builder.create();
    return gson.toJson(obj);
}

From source file:com.g3net.tool.ObjectUtils.java

License:Apache License

/**
 * //from  w  w  w. ja  v  a 2s .c  om
 * @param obj
 * @param type 
 *   Type typeOfT = new com.google.gson.reflect.TypeToken<Collection<Foo>>(){}.getType();
 * @return
 */
public static String toString(Object obj, Type type) {

    GsonBuilder builder = new GsonBuilder();
    builder.serializeNulls();
    builder.disableHtmlEscaping();
    builder.setPrettyPrinting();
    Gson gson = builder.create();
    return gson.toJson(obj, type);
}

From source file:com.github.zhizheng.json.JsonSchemaGeneratorImpl.java

License:Apache License

/**
 * Json to Json Schema//  ww  w . j a v a 2  s.  c  om
 * 
 * @param jsonElement
 * @return
 */
private String fromJsonElement(JsonElement jsonElement) {
    GsonBuilder gsonBuilder = new GsonBuilder();
    if (jsonSchemaConfig.isPrettyPrint()) {
        gsonBuilder.setPrettyPrinting();
    }
    gsonBuilder.disableHtmlEscaping();
    Gson gson = gsonBuilder.create();
    JsonObject jsonSchemaElement = makeSchemaElement(jsonElement, null, true, null);
    String jsonSchemaString = gson.toJson(jsonSchemaElement);
    return jsonSchemaString;
}

From source file:com.ibm.watson.developer_cloud.util.GsonSingleton.java

License:Open Source License

/**
 * Creates a {@link com.google.gson.Gson} object that can be use to serialize and deserialize Java objects.
 *
 * @param prettyPrint if true the JSON will be pretty printed
 * @return the {@link Gson}/*  ww  w. j  a v a 2s.c  om*/
 */
private static Gson createGson(Boolean prettyPrint) {
    GsonBuilder builder = new GsonBuilder();

    registerTypeAdapters(builder);

    if (prettyPrint) {
        builder.setPrettyPrinting();
    }
    builder.disableHtmlEscaping();
    return builder.create();
}

From source file:com.katsu.gson.GsonImpl.java

License:Open Source License

private void createGson() {
    GsonBuilder gsonBuilder = new GsonBuilder().addSerializationExclusionStrategy(new MvcExclusionStrategy());
    if (properties != null) {
        if (properties.containsKey(JsonProperty.DATE_FORMAT.getValue())) {
            gsonBuilder = gsonBuilder/*w  w w.ja  va  2 s.c  om*/
                    .setDateFormat(properties.getProperty(JsonProperty.DATE_FORMAT.getValue()));
        } else {
            gsonBuilder = gsonBuilder.setDateFormat(Constants.DATE_PATTERN);
        }
        if (!Constants.TRUE.equals(properties.getProperty(JsonProperty.HTML_ESCAPE.getValue()))) {
            gsonBuilder = gsonBuilder.disableHtmlEscaping();
        }
    } else {
        gsonBuilder = gsonBuilder.setDateFormat(Constants.DATE_PATTERN);
    }
    this.gson = gsonBuilder.create();
}

From source file:com.microsoft.intellij.helpers.activityConfiguration.azureCustomWizardParameter.AzureParameterPane.java

License:Open Source License

private void updateDocument() {
    if ((mobileServicesCheckBox.isSelected() && !notificationHubCheckBox.isSelected()
            && selectedMobileService != null)
            || (!mobileServicesCheckBox.isSelected() && notificationHubCheckBox.isSelected() && senderID != null
                    && connectionString != null && hubName != null)
            || (mobileServicesCheckBox.isSelected() && notificationHubCheckBox.isSelected()
                    && selectedMobileService != null && senderID != null && connectionString != null
                    && hubName != null)) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.disableHtmlEscaping();
        Gson gson = gsonBuilder.create();

        AzureParameters azureParameters = new AzureParameters(mobileServicesCheckBox.isSelected(),
                notificationHubCheckBox.isSelected(),
                mobileServicesCheckBox.isSelected() ? selectedMobileService.getAppUrl() : null,
                mobileServicesCheckBox.isSelected() ? selectedMobileService.getAppKey() : null,
                notificationHubCheckBox.isSelected() ? senderID : null,
                notificationHubCheckBox.isSelected() ? connectionString : null,
                notificationHubCheckBox.isSelected() ? hubName : null);

        String stringVal = gson.toJson(azureParameters);

        setValue(stringVal);/*from   w  w  w .j a  v a 2s  .  c  o m*/
    } else {
        setValue("");
    }
}

From source file:com.sk89q.worldedit.world.registry.LegacyMapper.java

License:Open Source License

/**
 * Attempt to load the data from file./*ww w  .  ja v a 2  s . c om*/
 *
 * @throws IOException thrown on I/O error
 */
private void loadFromResource() throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Vector3.class, new VectorAdapter());
    Gson gson = gsonBuilder.disableHtmlEscaping().create();
    URL url = LegacyMapper.class.getResource("legacy.json");
    if (url == null) {
        throw new IOException("Could not find legacy.json");
    }
    String data = Resources.toString(url, Charset.defaultCharset());
    LegacyDataFile dataFile = gson.fromJson(data, new TypeToken<LegacyDataFile>() {
    }.getType());

    ParserContext parserContext = new ParserContext();
    parserContext.setPreferringWildcard(false);
    parserContext.setRestricted(false);
    parserContext.setTryLegacy(false); // This is legacy. Don't match itself.

    for (Map.Entry<String, String> blockEntry : dataFile.blocks.entrySet()) {
        try {
            String id = blockEntry.getKey();
            BlockState state = WorldEdit.getInstance().getBlockFactory()
                    .parseFromInput(blockEntry.getValue(), parserContext).toImmutableState();
            blockToStringMap.put(state, id);
            stringToBlockMap.put(id, state);
        } catch (Exception e) {
            log.warning("Unknown block: " + blockEntry.getValue());
        }
    }

    for (Map.Entry<String, String> itemEntry : dataFile.items.entrySet()) {
        try {
            String id = itemEntry.getKey();
            ItemType type = ItemTypes.get(itemEntry.getValue());
            itemToStringMap.put(type, id);
            stringToItemMap.put(id, type);
        } catch (Exception e) {
            log.warning("Unknown item: " + itemEntry.getValue());
        }
    }
}

From source file:com.softwarementors.extjs.djn.gson.DefaultGsonBuilderConfigurator.java

License:Open Source License

public void configure(GsonBuilder builder, GlobalConfiguration configuration) {
    assert builder != null;
    assert configuration != null;

    if (configuration.getDebug()) {
        builder.setPrettyPrinting();/*from  w  ww . j  a  v a 2  s .  co  m*/
    }
    builder.serializeNulls();
    builder.disableHtmlEscaping();

    builder.registerTypeAdapter(Date.class, new DateDeserializer());
    builder.registerTypeAdapter(Date.class, new DateSerializer());
}