Example usage for com.google.gson JsonObject remove

List of usage examples for com.google.gson JsonObject remove

Introduction

In this page you can find the example usage for com.google.gson JsonObject remove.

Prototype

public JsonElement remove(String property) 

Source Link

Document

Removes the property from this JsonObject .

Usage

From source file:com.moba11y.websocketserver.ValidatorRSASignature.java

License:Apache License

@Override
public boolean validate(JsonObject jsonObject) throws ValidatorException {

    final String signatureString = jsonObject.get(PROP_SIGNATURE).getAsString();

    if (signatureString == null || signatureString.contentEquals("")) {
        throw new ValidatorException("There is no signature string in the message.\n" + jsonObject.toString());
    } else {/*from w w  w  .  j a v a2s  .  com*/
        jsonObject.remove(PROP_SIGNATURE);
    }

    final byte[] data = jsonObject.toString().getBytes();

    try {
        Signature signature = Signature.getInstance(mSignatureMethod.name());
        signature.initVerify(mPublicKey);
        signature.update(data, 0, data.length);

        if (!signature.verify(Base64.decode(signatureString.getBytes(), Base64.DEFAULT))) {
            throw new ValidatorException("Failed signature verification.  Signature incorrect.");
        }
    } catch (NoSuchAlgorithmException e) {
        //This can only happen if our enum names are off, so re-throw as a runtime exception
        throw new RuntimeException(e);
    } catch (Exception e) {
        throw new ValidatorException(e);
    }

    return true;
}

From source file:com.nextdoor.bender.deserializer.json.GenericJsonDeserializer.java

License:Apache License

@Override
public DeserializedEvent deserialize(String raw) {
    GenericJsonEvent devent = new GenericJsonEvent(null);

    JsonElement elm;/*from w  w w .j  a v  a2s  . c  om*/
    try {
        elm = parser.parse(raw);
    } catch (JsonSyntaxException e) {
        throw new DeserializationException(e);
    }

    if (!elm.isJsonObject()) {
        throw new DeserializationException("event is not a json object");
    }

    JsonObject obj = elm.getAsJsonObject();

    /*
     * Convert fields which are nested json strings into json objects
     */
    for (FieldConfig fconfig : this.nestedFieldConfigs) {
        if (obj.has(fconfig.getField())) {
            JsonElement msg = obj.get(fconfig.getField());

            /*
             * Find the JSON in the string and replace the field
             */
            NestedData data = deserialize(msg);

            obj.remove(fconfig.getField());
            obj.add(fconfig.getField(), data.nested);

            /*
             * If the string contained data before the JSON store it in a new field
             */
            if (fconfig.getPrefixField() != null && data.prefix != null) {
                obj.add(fconfig.getPrefixField(), data.prefix);
            }
        }
    }

    if (rootNodeOverridePath != null) {
        Object o = JsonPathProvider.read(obj, rootNodeOverridePath);
        if (obj == null || o instanceof JsonNull) {
            throw new DeserializationException(rootNodeOverridePath + " path not found in object");
        }
        obj = (JsonObject) o;
    }

    devent.setPayload(obj);

    return devent;
}

From source file:com.nextdoor.bender.operation.gelf.GelfOperation.java

License:Apache License

protected InternalEvent prefix(InternalEvent ievent) {
    DeserializedEvent devent;//from w  w w . ja v  a 2s .c  o  m
    if ((devent = ievent.getEventObj()) == null) {
        return null;
    }

    Object payload = devent.getPayload();

    if (payload == null) {
        return null;
    }

    if (!(payload instanceof JsonObject)) {
        throw new OperationException("Payload data is not a JsonObject");
    }

    JsonObject obj = (JsonObject) payload;

    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    /*
     * Prefix additional fields with "_". Everything that is not a GELF field is additional.
     */
    for (Entry<String, JsonElement> entry : orgEntries) {
        String key = entry.getKey();

        if (GELF_FIELDS.contains(key)) {
            continue;
        }

        JsonElement val = entry.getValue();
        obj.remove(key);

        obj.add("_" + key, val);
    }

    return ievent;
}

From source file:com.nextdoor.bender.operation.json.key.FlattenOperation.java

License:Apache License

protected void perform(JsonObject obj) {

    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();

        if (val.isJsonPrimitive() || val.isJsonNull()) {
            continue;
        }//www. java2 s  .c om

        obj.remove(entry.getKey());
        if (val.isJsonObject()) {
            perform(obj, val.getAsJsonObject(), entry.getKey());
        } else if (val.isJsonArray()) {
            perform(obj, val.getAsJsonArray(), entry.getKey());
        }
    }
}

From source file:com.nextdoor.bender.operation.json.key.KeyNameOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {

        JsonElement val = entry.getValue();
        obj.remove(entry.getKey());
        String key = entry.getKey().toLowerCase().replaceAll("[ .]", "_");

        if (val.isJsonPrimitive()) {
            JsonPrimitive prim = val.getAsJsonPrimitive();

            if (prim.isBoolean()) {
                obj.add(key + "__bool", val);
            } else if (prim.isNumber()) {
                if (prim.toString().contains(".")) {
                    obj.add(key + "__float", val);
                } else {
                    obj.add(key + "__long", val);
                }/*from ww w .  j  a va2 s .c  om*/
            } else if (prim.isString()) {
                obj.add(key + "__str", val);
            }
        } else if (val.isJsonObject()) {
            obj.add(key, val);
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            obj.add(key + "__arr", val);
        }
    }
}

From source file:com.nextdoor.bender.operation.json.key.KeyNameReplacementOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();

        /*//www . j  av a 2 s .c o  m
         * See if key matches. If it does then drop or rename. Otherwise keep recursing.
         */
        Matcher m = this.pattern.matcher(entry.getKey());
        boolean found = m.find();
        if (found) {
            /*
             * If instructed to drop then remove and continue. Otherwise remove and later rename;
             */
            obj.remove(entry.getKey());
            if (this.drop) {
                continue;
            }

            /*
             * Rename
             */
            if (val.isJsonPrimitive()) {
                obj.add(m.replaceAll(this.replacement), val);
            } else if (val.isJsonObject()) {
                obj.add(m.replaceAll(this.replacement), val);
                perform(val.getAsJsonObject());
            } else if (val.isJsonArray()) {
                JsonArray arr = val.getAsJsonArray();

                arr.forEach(item -> {
                    performOnArray(item);
                });
                obj.add(m.replaceAll(this.replacement), val);
            }
            continue;
        }

        /*
         * Keep recursing
         */
        if (val.isJsonObject()) {
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            JsonArray arr = val.getAsJsonArray();
            arr.forEach(item -> {
                performOnArray(item);
            });
        }

    }
}

From source file:com.nextdoor.bender.operation.json.key.LowerCaseKeyOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {

        JsonElement val = entry.getValue();
        obj.remove(entry.getKey());
        String key = entry.getKey().toLowerCase();

        if (val.isJsonPrimitive()) {
            obj.add(key, val);
        } else if (val.isJsonObject()) {
            obj.add(key, val);
            perform(val.getAsJsonObject());
        } else if (val.isJsonArray()) {
            obj.add(key, val);

            val.getAsJsonArray().forEach(elm -> {
                if (elm.isJsonObject()) {
                    perform((JsonObject) elm);
                }//from  w ww .  jav  a 2 s.c  o  m
            });
        }
    }
}

From source file:com.nextdoor.bender.operation.json.value.DropArraysOperation.java

License:Apache License

protected void perform(JsonObject obj) {
    Set<Entry<String, JsonElement>> entries = obj.entrySet();
    Set<Entry<String, JsonElement>> orgEntries = new HashSet<Entry<String, JsonElement>>(entries);

    for (Entry<String, JsonElement> entry : orgEntries) {
        JsonElement val = entry.getValue();

        if (val.isJsonArray()) {
            obj.remove(entry.getKey());
        } else if (val.isJsonObject()) {
            perform(val.getAsJsonObject());
        }//from w  w w.j a  v a  2s  .c  om
    }
}

From source file:com.oneops.search.msg.processor.es.DLQMessageProcessor.java

License:Apache License

private String convertMessage(String message, Map<String, String> headers) {
    String newMessage = message;//from   w w  w .  j a v  a 2s .com
    JsonElement msgRootElement = parser.parse(message);
    if (msgRootElement instanceof JsonObject) {
        JsonObject msgRoot = (JsonObject) msgRootElement;

        JsonElement element = msgRoot.get(PAYLOAD_ELEMENT_KEY);
        if (element != null) {
            if (!element.isJsonObject()) {
                //convert payLoad to a json object if it is not already
                msgRoot.remove(PAYLOAD_ELEMENT_KEY);
                String realPayload = element.getAsString();
                String escapedPayload = StringEscapeUtils.unescapeJava(realPayload);
                msgRoot.add(PAYLOAD_ELEMENT_KEY, parser.parse(escapedPayload));
            }
        }
        JsonElement hdrElement = GSON.toJsonTree(headers);
        msgRoot.add("msgHeaders", hdrElement);
        newMessage = GSON.toJson(msgRoot);
        if (logger.isDebugEnabled()) {
            logger.debug("message to be indexed " + newMessage);
        }
    }
    return newMessage;
}

From source file:com.openyelp.server.JsonCacheRpcExecutor.java

License:Apache License

private void sendError(JsonRpcServerTransport transport, JsonObject resp, Integer code, String message,
        String data) {//from w  w  w .  j  a v  a  2  s.co  m
    JsonObject error = new JsonObject();
    if (code != null) {
        error.addProperty("code", code);
    }

    if (message != null) {
        error.addProperty("message", message);
    }

    if (data != null) {
        error.addProperty("data", data);
    }

    resp.add("error", error);
    resp.remove("result");
    String responseData = resp.toString();

    try {
        transport.writeResponse(responseData);
    } catch (Exception e) {
    }
}