Example usage for com.google.gson JsonPrimitive isString

List of usage examples for com.google.gson JsonPrimitive isString

Introduction

In this page you can find the example usage for com.google.gson JsonPrimitive isString.

Prototype

public boolean isString() 

Source Link

Document

Check whether this primitive contains a String value.

Usage

From source file:com.blackducksoftware.integration.hub.detect.detector.npm.NpmCliParser.java

License:Apache License

private void populateChildren(final MutableDependencyGraph graph, final Dependency parentDependency,
        final JsonObject parentNodeChildren, final Boolean root) {
    if (parentNodeChildren == null) {
        return;/*from w  ww  . j  a  v  a  2s  . c o  m*/
    }
    final Set<Entry<String, JsonElement>> elements = parentNodeChildren.entrySet();
    elements.forEach(it -> {
        if (it.getValue() != null && it.getValue().isJsonObject()) {

        }
        final JsonObject element = it.getValue().getAsJsonObject();
        final String name = it.getKey();
        String version = null;
        final JsonPrimitive versionPrimitive = element.getAsJsonPrimitive(JSON_VERSION);
        if (versionPrimitive != null && versionPrimitive.isString()) {
            version = versionPrimitive.getAsString();
        }
        final JsonObject children = element.getAsJsonObject(JSON_DEPENDENCIES);

        if (name != null && version != null) {
            final ExternalId externalId = externalIdFactory.createNameVersionExternalId(Forge.NPM, name,
                    version);
            final Dependency child = new Dependency(name, version, externalId);

            populateChildren(graph, child, children, false);
            if (root) {
                graph.addChildToRoot(child);
            } else {
                graph.addParentWithChild(parentDependency, child);
            }
        }
    });
}

From source file:com.blackypaw.mc.i18n.chat.ChatComponentDeserializer.java

License:Open Source License

/**
 * Creates a text component given its JSON representation. Both the shorthand notation as
 * a raw string as well as the notation as a full-blown JSON object are supported.
 *
 * @param json The JSON element to be deserialized into a text component
 *
 * @return The deserialized TextComponent
 *//*from   w w w . j a va  2  s  .  co m*/
private TextComponent deserializeTextComponent(JsonElement json) {
    final TextComponent component = new TextComponent("");

    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            component.setText(primitive.getAsString());
        }
    } else if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();

        if (object.has("text")) {
            JsonElement textElement = object.get("text");
            if (textElement.isJsonPrimitive()) {
                JsonPrimitive textPrimitive = textElement.getAsJsonPrimitive();
                if (textPrimitive.isString()) {
                    component.setText(textPrimitive.getAsString());
                }
            }
        }

        if (object.has("extra")) {
            JsonElement extraElement = object.get("extra");
            if (extraElement.isJsonArray()) {
                JsonArray extraArray = extraElement.getAsJsonArray();
                for (int i = 0; i < extraArray.size(); ++i) {
                    JsonElement fieldElement = extraArray.get(i);
                    component.addChild(this.deserializeComponent(fieldElement));
                }
            }
        }
    }

    return component;
}

From source file:com.builtbroken.builder.html.parts.HTMLPartHeader.java

@Override
public String process(JsonElement value) {
    try {//from w  ww  . jav a  2s.c o  m
        if (value.isJsonObject() && !value.isJsonPrimitive()) {
            JsonObject h = value.getAsJsonObject();
            String text = h.getAsJsonPrimitive("text").getAsString();
            int size = 2;
            if (h.has("size")) {
                JsonPrimitive p = h.getAsJsonPrimitive("size");
                if (p.isString()) {
                    String s = p.getAsString().toLowerCase();
                    if (s.equals("small")) {
                        size = 3;
                    } else if (s.equals("medium")) {
                        size = 2;
                    } else if (s.equals("large")) {
                        size = 1;
                    }
                } else {
                    size = p.getAsInt();
                }
            }
            if (h.has("link")) {
                String link = h.getAsJsonPrimitive("link").getAsString();
                if (link.startsWith("url")) {
                    return "<h" + size + "><a href=\"" + link + "\">" + text + "</a></h" + size + ">";
                } else if (link.endsWith(".json")) {
                    return "<h" + size + "><a href=\"#PageRef:" + link + "#\">" + text + "</a></h" + size + ">";
                } else {
                    return "<h" + size + "><a href=\"#" + link + "#\">" + text + "</a></h" + size + ">";
                }
            }
            return "<h" + size + ">" + text + "</h" + size + ">";
        }
        return "<h2>" + value.getAsString() + "</h2>";
    } catch (Exception e) {
        throw new RuntimeException("Unexpected error while parsing header tag " + value, e);
    }
}

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

License:Open Source License

private Object extractAps(JsonElement in) {
    if (in == null || in.isJsonNull())
        return null;

    if (in.isJsonArray()) {
        List<Object> list = new ArrayList<>();
        JsonArray arr = in.getAsJsonArray();
        for (JsonElement anArr : arr) {
            list.add(extractAps(anArr));
        }/*w  ww.j av  a  2  s.  c  o m*/
        return list;
    } else if (in.isJsonObject()) {
        Map<String, Object> map = new LinkedTreeMap<>();
        JsonObject obj = in.getAsJsonObject();
        Set<Map.Entry<String, JsonElement>> entitySet = obj.entrySet();
        for (Map.Entry<String, JsonElement> entry : entitySet) {
            map.put(entry.getKey(), extractAps(entry.getValue()));
            NginxClojureRT.log.debug(TAG + entry.getKey() + "=>" + map.get(entry.getKey()) + "=>"
                    + map.get(entry.getKey()).getClass().getTypeName());
            switch (entry.getKey()) {
            case "dapi":
                this.setDapi(map.get(entry.getKey()));
                break;
            case "acme1":
                this.setAcme1(map.get(entry.getKey()));
                break;
            case "acme2":
                this.setAcme2(map.get(entry.getKey()));
                break;
            case "acme3":
                this.setAcme3(map.get(entry.getKey()));
                break;
            case "acme4":
                this.setAcme4(map.get(entry.getKey()));
                break;
            case "acme5":
                this.setAcme5(map.get(entry.getKey()));
                break;
            case "acme6":
                this.setAcme6(map.get(entry.getKey()));
                break;
            case "acme7":
                this.setAcme7(map.get(entry.getKey()));
                break;
            case "acme8":
                this.setAcme8(map.get(entry.getKey()));
                break;
            case "aps":
                NginxClojureRT.log.debug(TAG + "aps initialized");
                break;
            case "badge":
                this.getAps().setBadge(map.get(entry.getKey()));
                break;
            case "sound":
                this.getAps().setSound(map.get(entry.getKey()));
                break;
            case "alert":
                NginxClojureRT.log.debug(TAG + "alert initialized");
                break;
            case "title":
                this.getAps().getAlert().setTitle(map.get(entry.getKey()));
                break;
            case "body":
                this.getAps().getAlert().setBody(map.get(entry.getKey()));
                break;
            case "action-loc-key":
                this.getAps().getAlert().setActionLocKey(map.get(entry.getKey()));
                break;
            default:
                NginxClojureRT.log.info(TAG + "Unhandled field : " + entry.getKey());
                break;
            }
        }
        return map;
    } else if (in.isJsonPrimitive()) {
        JsonPrimitive prim = in.getAsJsonPrimitive();
        if (prim.isBoolean()) {
            return prim.getAsBoolean();
        } else if (prim.isString()) {
            return prim.getAsString();
        } else if (prim.isNumber()) {
            Number num = prim.getAsNumber();
            // here you can handle double int/long values
            // and return any type you want
            // this solution will transform 3.0 float to long values
            if (Math.ceil(num.doubleValue()) == num.longValue()) {
                return num.longValue();
            } else {
                return num.doubleValue();
            }
        }
    }
    NginxClojureRT.log.info("Handling json null");
    return null;
}

From source file:com.canoo.dolphin.impl.codec.ValueEncoder.java

License:Apache License

static Object decodeValue(JsonElement jsonElement) {
    if (jsonElement == null || jsonElement.isJsonNull()) {
        return null;
    }/*from   w w w.  j ava 2s  . co m*/
    if (!jsonElement.isJsonPrimitive()) {
        throw new JsonParseException("Illegal JSON detected");
    }
    final JsonPrimitive value = (JsonPrimitive) jsonElement;

    if (value.isString()) {
        return value.getAsString();
    } else if (value.isBoolean()) {
        return value.getAsBoolean();
    } else if (value.isNumber()) {
        return value.getAsNumber();
    }
    throw new JsonParseException("Currently only String, Boolean, or Number are allowed as primitives");
}

From source file:com.cloudbase.CBNaturalDeserializer.java

License:Open Source License

private Object handlePrimitive(JsonPrimitive json) {
    if (json.isBoolean())
        return json.getAsBoolean();
    else if (json.isString())
        return json.getAsString();
    else {//from  www.  j a v  a  2  s  .  co m
        BigDecimal bigDec = json.getAsBigDecimal();
        // Find out if it is an int type
        try {
            bigDec.toBigIntegerExact();
            try {
                return bigDec.intValueExact();
            } catch (ArithmeticException e) {
            }
            return bigDec.longValue();
        } catch (ArithmeticException e) {
        }
        // Just return it as a double
        return bigDec.doubleValue();
    }
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

/**
 * Calculate the objecthash for a Gson JsonElement object, with a custom redaction prefix string.
 * @param o the Gson JsonElement to calculated the objecthash for.
 * @param r the string to use as a prefix to indicate that a string should be treated as a redacted subobject.
 * @return the objecthash for this object
 * @throws ContinusecException upon error
 *//*  ww w  .j  ava 2s.  c  om*/
public static final byte[] objectHashWithRedaction(JsonElement o, String r) throws ContinusecException {
    if (o == null || o.isJsonNull()) {
        return hashNull();
    } else if (o.isJsonArray()) {
        return hashArray(o.getAsJsonArray(), r);
    } else if (o.isJsonObject()) {
        return hashObject(o.getAsJsonObject(), r);
    } else if (o.isJsonPrimitive()) {
        JsonPrimitive p = o.getAsJsonPrimitive();
        if (p.isBoolean()) {
            return hashBoolean(p.getAsBoolean());
        } else if (p.isNumber()) {
            return hashDouble(p.getAsDouble());
        } else if (p.isString()) {
            return hashString(p.getAsString(), r);
        } else {
            throw new InvalidObjectException();
        }
    } else {
        throw new InvalidObjectException();
    }
}

From source file:com.continusec.client.ObjectHash.java

License:Apache License

private static final JsonElement shedObject(JsonObject o, String r) throws ContinusecException {
    JsonObject rv = new JsonObject();
    for (Map.Entry<String, JsonElement> e : o.entrySet()) {
        JsonElement v = e.getValue();//from  w w  w .j  a  v  a2  s.  c om
        if (v.isJsonArray()) {
            JsonArray a = v.getAsJsonArray();
            if (a.size() == 2) {
                rv.add(e.getKey(), shedRedactable(a.get(1), r));
            } else {
                throw new InvalidObjectException();
            }
        } else if (v.isJsonPrimitive()) {
            JsonPrimitive p = v.getAsJsonPrimitive();
            if (p.isString()) {
                if (p.getAsString().startsWith(r)) {
                    // all good, but we shed it.
                } else {
                    throw new InvalidObjectException();
                }
            } else {
                throw new InvalidObjectException();
            }
        } else {
            throw new InvalidObjectException();
        }
    }
    return rv;
}

From source file:com.continuuity.loom.macro.Expander.java

License:Apache License

/**
 * Given a JSON tree, find the element specified by the path (or the root if path is null). In that subtree,
 * recursively traverse all elements, and expand all String typed right hand side values.  If a macro cannot be
 * expanded due to the cluster object missing certain data, that macro will be left unexpanded.
 *
 * @param json A JSON tree/*w  w  w  . ja  v a  2  s . c  o m*/
 * @param path the path to expand under
 * @param cluster the cluster to use for expanding macros.
 * @param nodes the cluster nodes to use for expanding macros.
 * @param node the cluster node to use for expanding macros.
 * @return a new JSON tree if any expansion took place, and the original JSON tree otherwise.
 * @throws SyntaxException if a macro expression is ill-formed.
 * @throws IncompleteClusterException if the cluster does not have the meta data to expand all macros.
 */
public static JsonElement expand(JsonElement json, @Nullable java.util.List<String> path, Cluster cluster,
        Set<Node> nodes, Node node) throws SyntaxException, IncompleteClusterException {

    // if path is given,
    if (path != null && !path.isEmpty()) {
        String first = path.get(0);
        if (json.isJsonObject()) {
            JsonObject object = json.getAsJsonObject();
            JsonElement json1 = object.get(first);
            if (json1 != null) {
                JsonElement expanded = expand(json1, path.subList(1, path.size()), cluster, nodes, node);
                if (expanded != json1) {
                    // only construct new json object if actual expansion happened
                    JsonObject object1 = new JsonObject();
                    for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
                        object1.add(entry.getKey(), entry.getKey().equals(first) ? expanded : entry.getValue());
                    }
                    return object1;
                }
            }
        }
        // path was given, but either no corresponding subtree was found or no expansion happened...
        return json;
    }

    if (json.isJsonPrimitive()) {
        JsonPrimitive primitive = json.getAsJsonPrimitive();
        if (primitive.isString()) {
            String value = primitive.getAsString();
            String expanded = expand(value, cluster, nodes, node);
            if (!expanded.equals(value)) {
                // only return a new json element if actual expansion happened
                return new JsonPrimitive(expanded);
            }
        }
    }

    if (json.isJsonArray()) {
        JsonArray array = json.getAsJsonArray();
        JsonArray array1 = new JsonArray();
        boolean expansionHappened = false;
        for (JsonElement element : array) {
            JsonElement expanded = expand(element, path, cluster, nodes, node);
            if (expanded != element) {
                expansionHappened = true;
            }
            array1.add(expanded);
        }
        // only return a new json array if actual expansion happened
        if (expansionHappened) {
            return array1;
        }
    }

    if (json.isJsonObject()) {
        JsonObject object = json.getAsJsonObject();
        JsonObject object1 = new JsonObject();
        boolean expansionHappened = false;
        for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
            JsonElement expanded = expand(entry.getValue(), path, cluster, nodes, node);
            if (expanded != entry.getValue()) {
                expansionHappened = true;
            }
            object1.add(entry.getKey(), expand(entry.getValue(), path, cluster, nodes, node));
        }
        if (expansionHappened) {
            return object1;
        }
    }

    return json;
}

From source file:com.exsoloscript.challonge.gson.OffsetDateTimeAdapter.java

License:Apache License

@Override
public OffsetDateTime deserialize(JsonElement json, Type type,
        JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
    JsonPrimitive jsonPrimitive = json.getAsJsonPrimitive();

    // if provided as String - '2011-12-03T10:15:30+01:00'
    if (jsonPrimitive.isString()) {
        return OffsetDateTime.parse(jsonPrimitive.getAsString(), DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    }// ww  w  .  j a  v  a  2  s . co  m

    throw new JsonParseException("Unable to parse OffsetDateTime. DateTime was not provided as string.");
}