Example usage for com.google.gson JsonElement isJsonPrimitive

List of usage examples for com.google.gson JsonElement isJsonPrimitive

Introduction

In this page you can find the example usage for com.google.gson JsonElement isJsonPrimitive.

Prototype

public boolean isJsonPrimitive() 

Source Link

Document

provides check for verifying if this element is a primitive or not.

Usage

From source file:com.birbit.jsonapi.JsonApiLinksDeserializer.java

License:Apache License

@Override
public JsonApiLinks deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (!json.isJsonObject()) {
        throw new JsonParseException("JsonApiLinks json element must be an object");
    }//  w w w  .j a  v  a  2s .c o m
    JsonObject asJsonObject = json.getAsJsonObject();
    Set<Map.Entry<String, JsonElement>> entries = asJsonObject.entrySet();
    if (entries.isEmpty()) {
        return JsonApiLinks.EMPTY;
    }
    Map<String, JsonApiLinkItem> result = new HashMap<String, JsonApiLinkItem>();
    for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) {
        JsonElement value = entry.getValue();
        if (value.isJsonPrimitive()) {
            result.put(entry.getKey(), new JsonApiLinkItem(entry.getValue().getAsString()));
        } else {
            result.put(entry.getKey(),
                    context.<JsonApiLinkItem>deserialize(entry.getValue(), JsonApiLinkItem.class));
        }
    }
    return new JsonApiLinks(result);
}

From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java

License:Apache License

private void parseLinks(JsonDeserializationContext context, T t, JsonObject jsonObject)
        throws IllegalAccessException, InvocationTargetException {
    JsonElement links = jsonObject.get("links");
    if (links != null && links.isJsonObject()) {
        JsonObject linksObject = links.getAsJsonObject();
        Setter linksObjectSetter = linkSetters.get("");
        if (linksObjectSetter != null) {
            linksObjectSetter.setOnObject(t, context.deserialize(links, JsonApiLinks.class));
        }// w  ww  .j a v  a  2  s .  c  o m
        for (Map.Entry<String, Setter> entry : linkSetters.entrySet()) {
            JsonElement link = linksObject.get(entry.getKey());
            if (link != null && link.isJsonPrimitive()) {
                entry.getValue().setOnObject(t, link.getAsString());
            }
        }
    }
}

From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java

License:Apache License

private void parseRelationships(JsonDeserializationContext context, T t, JsonObject jsonObject)
        throws IllegalAccessException, InvocationTargetException {
    JsonElement relationships = jsonObject.get("relationships");
    if (relationships != null && relationships.isJsonObject()) {
        JsonObject relationshipsObject = relationships.getAsJsonObject();
        for (Map.Entry<String, Setter> entry : relationshipSetters.entrySet()) {
            JsonElement relationship = relationshipsObject.get(entry.getKey());
            if (relationship != null && relationship.isJsonObject()) {
                if (entry.getValue().type() == JsonApiRelationshipList.class) {
                    entry.getValue().setOnObject(t,
                            context.deserialize(relationship, JsonApiRelationshipList.class));
                } else if (entry.getValue().type() == JsonApiRelationship.class) { // JsonApiRelationship
                    entry.getValue().setOnObject(t,
                            context.deserialize(relationship, JsonApiRelationship.class));
                } else { // String list or id
                    JsonElement data = relationship.getAsJsonObject().get("data");
                    if (data != null) {
                        if (data.isJsonObject()) {
                            JsonElement relationshipIdElement = data.getAsJsonObject().get("id");
                            if (relationshipIdElement != null) {
                                if (relationshipIdElement.isJsonPrimitive()) {
                                    entry.getValue().setOnObject(t, relationshipIdElement.getAsString());
                                }//from   w  w w .  j a v  a 2  s.  co m
                            }
                        } else if (data.isJsonArray()) {
                            List<String> idList = parseIds(data.getAsJsonArray());
                            entry.getValue().setOnObject(t, idList);
                        }
                    }
                }
            }
        }
    }
}

From source file:com.birbit.jsonapi.JsonApiResourceDeserializer.java

License:Apache License

private List<String> parseIds(JsonArray jsonArray) {
    List<String> result = new ArrayList<String>(jsonArray.size());
    for (int i = 0; i < jsonArray.size(); i++) {
        JsonElement item = jsonArray.get(i);
        if (item.isJsonObject()) {
            JsonElement idField = item.getAsJsonObject().get("id");
            if (idField != null && idField.isJsonPrimitive()) {
                result.add(idField.getAsString());
            }//ww w  .  j  a v a 2s. com
        }
    }
    return result;
}

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

License:Open Source License

/**
 * Detects the type of the given json element and deserializes its contents into the appropriate
 * component types.//from   w  w  w  .  java2s.  c om
 *
 * @param json The json element to be deserialized
 *
 * @return The deserialized chat component
 */
private ChatComponent deserializeComponent(JsonElement json) {
    if (json.isJsonPrimitive()) {
        // Only text components can be raw primitives (strings):
        return this.deserializeTextComponent(json);
    } else if (json.isJsonArray()) {
        // Only text components can be json arrays:
        return this.deserializeComponentArray(json.getAsJsonArray());
    } else {
        return this.deserializeTextComponent(json);
    }
}

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  .jav  a2 s.c  o  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 {//  w w w. j  a va2 s  . co 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  w  w  .  ja v  a  2s  .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.ca.dvs.utilities.raml.RamlUtil.java

License:Open Source License

/**
 * @param jsonString/*from w  w  w.  j  av a  2 s .  c  o m*/
 * @return the appropriate JsonElement object from the parsed jsonString
 */
private static Object getJsonElementValue(String jsonString) {

    JsonParser jsonParser = new JsonParser();
    JsonElement jsonElement = jsonParser.parse(jsonString);

    if (jsonElement.isJsonObject()) {

        return jsonElement.getAsJsonObject();

    } else if (jsonElement.isJsonArray()) {

        return jsonElement.getAsJsonArray();

    } else if (jsonElement.isJsonNull()) {

        return jsonElement.getAsJsonNull();

    } else if (jsonElement.isJsonPrimitive()) {

        return jsonElement.getAsJsonPrimitive();

    } else {

        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;
    }//  w ww. j  a va2 s.c  om
    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");
}