Example usage for com.google.gson JsonElement isJsonNull

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

Introduction

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

Prototype

public boolean isJsonNull() 

Source Link

Document

provides check for verifying if this element represents a null value or not.

Usage

From source file:org.terasology.utilities.gson.ResolutionHandler.java

License:Apache License

@Override
public Resolution deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
    if (json.isJsonNull()) {
        return HeadlessResolution.getInstance();
    }/*from   w  w w  . j ava 2s.  c  o  m*/
    return new LwjglResolution(context.deserialize(json, DisplayMode.class));
}

From source file:org.trimou.gson.resolver.JsonElementResolver.java

License:Apache License

private Object unwrapJsonElementIfNecessary(JsonElement jsonElement) {
    if (unwrapJsonPrimitive) {
        if (jsonElement.isJsonPrimitive()) {
            return unwrapJsonPrimitive((JsonPrimitive) jsonElement);
        } else if (jsonElement.isJsonNull()) {
            return Placeholder.NULL;
        }/*from w w w. ja  v a2s  . com*/
    }
    return jsonElement;
}

From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractDetailedElementDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}/*from  w  ww. ja  v  a2 s. co m*/
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public T deserialize(JsonElement rootJsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    T pojo = super.deserialize(rootJsonElement, type, context);

    JsonObject jsonObject = rootJsonElement.getAsJsonObject();

    JsonElement element = jsonObject.get(HTML_URL);
    if (element != null && !element.isJsonNull()) {
        String htmlUrl = element.getAsString();
        pojo.setHtmlUrl(htmlUrl);
    }
    element = jsonObject.get(SUBMITTED_ON);
    if (element != null && !element.isJsonNull()) {
        try {
            pojo.setSubmittedOn(context.<Date>deserialize(element, Date.class));
        } catch (JsonParseException e) {
            TuleapCoreActivator.log(e, false);
        }
    }
    element = jsonObject.get(SUBMITTED_BY);
    if (element != null && element.isJsonPrimitive()) {
        int submittedBy = element.getAsInt();
        pojo.setSubmittedBy(submittedBy);
    }
    element = jsonObject.get(LAST_MODIFIED_DATE);
    if (element != null && !element.isJsonNull()) {
        try {
            pojo.setLastModifiedDate(context.<Date>deserialize(element, Date.class));
        } catch (JsonParseException e) {
            TuleapCoreActivator.log(e, false);
        }
    }
    return pojo;
}

From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractProjectElementDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}/*from   w ww . j ava 2 s.  com*/
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public T deserialize(JsonElement rootJsonElement, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = rootJsonElement.getAsJsonObject();

    String id = jsonObject.get(ITuleapConstants.ID).getAsString();
    T pojo = getPrototype(id);

    JsonElement jsonElement = jsonObject.get(ITuleapConstants.LABEL);
    if (jsonElement != null && !jsonElement.isJsonNull()) {
        pojo.setLabel(jsonElement.getAsString());
    }
    TuleapReference project = context.deserialize(jsonObject.get(ITuleapConstants.JSON_PROJECT),
            TuleapReference.class);
    JsonElement element = jsonObject.get(ITuleapConstants.URI);
    if (element != null && !element.isJsonNull()) {
        pojo.setUri(element.getAsString());
    }

    pojo.setProject(project);
    return pojo;
}

From source file:org.tuleap.mylyn.task.core.internal.parser.TuleapChangesetDeserializer.java

License:Open Source License

/**
 * Deserialize the JSON representation of a comment.
 *
 * @param element//w  w w .java 2s  .co m
 *            The JSON comment
 * @param type
 *            The comment type
 * @param context
 *            The context
 * @return the TuleapElementComment
 * @throws JsonParseException
 *             The exception to throw
 */
@Override
public TuleapElementComment deserialize(JsonElement element, Type type, JsonDeserializationContext context)
        throws JsonParseException {

    JsonObject jsonObject = element.getAsJsonObject();

    Date submissionDate = null;
    int submittedBy = -1;
    String email = null;
    String body = null;

    JsonElement theElement = jsonObject.get(SUBMITTED_ON);
    if (theElement != null && !theElement.isJsonNull()) {
        try {
            submissionDate = context.<Date>deserialize(theElement, Date.class);
        } catch (JsonParseException e) {
            TuleapCoreActivator.log(e, false);
        }
    }

    theElement = jsonObject.get(SUBMITTED_BY);
    if (theElement != null && theElement.isJsonPrimitive()) {
        submittedBy = theElement.getAsInt();
    }

    theElement = jsonObject.get(EMAIL);
    if (theElement != null && theElement.isJsonPrimitive()) {
        email = theElement.getAsString();
    }

    JsonObject lastComment = jsonObject.get(LAST_COMMENT).getAsJsonObject();

    theElement = lastComment.get(BODY);
    if (theElement != null && theElement.isJsonPrimitive()) {
        body = theElement.getAsString();
    }

    TuleapUser user = new TuleapUser(null, null, submittedBy, email, null);

    return new TuleapElementComment(body, user, submissionDate);

}

From source file:org.tuleap.mylyn.task.core.internal.parser.TuleapTrackerDeserializer.java

License:Open Source License

/**
 * Extract a field from its JSON representation and adds it to the given tracker.
 *
 * @param field// w ww .  j ava 2 s.  c  o m
 *            The JSON representation of the field
 * @param tracker
 *            The tracker
 * @param jsonObject
 *            The parent JSON object of the field
 * @param fieldSemantic
 *            The JSON representation of the semantics part
 * @param context
 *            the deserialization context
 * @return The relevant kind of TuleapField, can be null for unknown or unmanaged field types.
 */
private AbstractTuleapField getField(JsonObject field, TuleapTracker tracker, JsonObject jsonObject,
        JsonObject fieldSemantic, JsonDeserializationContext context) {
    int fieldId = field.get(FIELD_ID).getAsInt();
    String fieldType = field.get(TYPE).getAsString();
    AbstractTuleapField tuleapField = createField(fieldId, fieldType);
    if (tuleapField != null) {
        // the field label
        tuleapField.setLabel(field.get(LABEL).getAsString());
        tuleapField.setName(field.get("name").getAsString()); //$NON-NLS-1$

        // the field permissions
        JsonArray permissions = field.get(PERMISSIONS).getAsJsonArray();
        String[] permissionsArray = new String[permissions.size()];
        for (int j = 0; j < permissions.size(); j++) {
            if (CREATE.equals(permissions.get(j).getAsString())) {
                permissionsArray[j] = PERMISSION_SUBMIT;
            } else {
                permissionsArray[j] = permissions.get(j).getAsString();
            }
        }
        tuleapField.setPermissions(permissionsArray);

        // The Multi Select Box case
        JsonArray fieldValuesArray = null;
        JsonObject fieldBinding = null;
        JsonElement valuesElement = field.get(VALUES);
        if (valuesElement != null && !valuesElement.isJsonNull()) {
            fieldValuesArray = valuesElement.getAsJsonArray();
        }
        JsonElement bindingElement = field.get(BINDINGS);
        if (bindingElement != null && !bindingElement.isJsonNull()) {
            fieldBinding = bindingElement.getAsJsonObject();
        }
        if (tuleapField instanceof TuleapMultiSelectBox) {
            fillTuleapMultiSelectBoxField(tracker, (TuleapMultiSelectBox) tuleapField, fieldValuesArray,
                    fieldSemantic, fieldBinding);
            // The Select Box case
        } else if (tuleapField instanceof TuleapSelectBox) {
            fillTuleapSelectBoxField(tracker, (TuleapSelectBox) tuleapField, fieldValuesArray, fieldSemantic,
                    fieldBinding, jsonObject, context);
        }
        manageTitleSemantic(tuleapField, fieldSemantic);
    }
    return tuleapField;
}

From source file:org.tuleap.mylyn.task.core.internal.parser.TuleapTrackerDeserializer.java

License:Open Source License

/**
 * Sets the workflow and the transition for the select box.
 *
 * @param jsonObject//from  w  ww .  j  av a2  s.c  om
 *            the JSON root object
 * @param selectBoxField
 *            the select box field
 * @param context
 *            the deserialization context
 */
private void fillWorkflow(JsonObject jsonObject, TuleapSelectBox selectBoxField,
        JsonDeserializationContext context) {
    JsonElement workflowJsonElement = jsonObject.get(ITuleapTrackerConstants.WORKFLOW);
    if (workflowJsonElement != null && !workflowJsonElement.isJsonNull()) {
        JsonObject workflowJsonObject = workflowJsonElement.getAsJsonObject();

        if (workflowJsonObject.get(FIELD_ID).getAsInt() == selectBoxField.getIdentifier()) {
            // the workflow transitions
            JsonArray transitionsJsonArray = workflowJsonObject.get(TRANSITIONS).getAsJsonArray();
            for (JsonElement transitionElement : transitionsJsonArray) {
                TuleapWorkflowTransition workflowTransition = context.deserialize(transitionElement,
                        TuleapWorkflowTransition.class);
                selectBoxField.getWorkflow().addTransition(workflowTransition);
            }
        }
    }
}

From source file:org.tuleap.mylyn.task.core.internal.parser.TuleapWorkflowTransitionDeserializer.java

License:Open Source License

/**
 * {@inheritDoc}//from w w w . j a va 2s.com
 *
 * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type,
 *      com.google.gson.JsonDeserializationContext)
 */
@Override
public TuleapWorkflowTransition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonElement fromElement = jsonObject.get(FROM_FIELD_VALUE_ID);
    // By default, the initial state is unselected
    int from = ITuleapConstants.CONFIGURABLE_FIELD_NONE_BINDING_ID;
    if (fromElement != null && !fromElement.isJsonNull()) {
        from = fromElement.getAsInt();
    }
    int to = jsonObject.get(TO_FIELD_VALUE_ID).getAsInt();

    TuleapWorkflowTransition transition = new TuleapWorkflowTransition();
    transition.setFrom(from);
    transition.setTo(to);
    return transition;
}

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

@Override
public Map<String, Object> deserialize2(Class<?> cls, String json) throws Exception {
    JsonStreamParser parser = new JsonStreamParser(json);
    if (parser.hasNext()) {
        JsonObject jsonobj = parser.next().getAsJsonObject();
        Set<Entry<String, JsonElement>> jset = jsonobj.entrySet();
        if (!jset.isEmpty()) {
            Map<String, PropertyDescriptor> propMap = ObjectMappingCache.getInstance()
                    .getObjectPropertyMap(cls);
            Map<String, Object> instance = new HashMap<String, Object>();
            for (Entry<String, JsonElement> entry : jset) {
                String name = entry.getKey();
                JsonElement val = entry.getValue();
                if (!val.isJsonNull()) {
                    PropertyDescriptor descriptor = propMap.get(name);
                    if (descriptor != null) {
                        Class<?> memberType = descriptor.getPropertyType();
                        if (memberType == String.class) {
                            instance.put(name, val.getAsString());
                        } else if (memberType == Integer.class || memberType == Integer.TYPE) {
                            instance.put(name, val.getAsInt());
                        } else if (memberType == Byte.class || memberType == Byte.TYPE) {
                            instance.put(name, val.getAsByte());
                        } else if (memberType == Double.class || memberType == Double.TYPE) {
                            instance.put(name, val.getAsDouble());
                        } else if (memberType == Float.class || memberType == Float.TYPE) {
                            instance.put(name, val.getAsFloat());
                        } else if (memberType == Long.class || memberType == Long.TYPE) {
                            instance.put(name, val.getAsLong());
                        } else if (memberType == Short.class || memberType == Short.TYPE) {
                            instance.put(name, val.getAsShort());
                        } else if (memberType == Boolean.class || memberType == Boolean.TYPE) {
                            instance.put(name, val.getAsBoolean());
                        } else if (memberType == BigDecimal.class) {
                            instance.put(name, val.getAsBigDecimal());
                        } else if (memberType == BigInteger.class) {
                            instance.put(name, val.getAsBigInteger());
                        } else if (memberType == byte[].class) {
                            instance.put(name, val.getAsString().getBytes());
                        } else if (memberType == java.sql.Timestamp.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Timestamp(time));
                            }//from  w ww . j a v  a 2 s.  c o m
                        } else if (memberType == java.sql.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Date(time));
                            }
                        } else if (memberType == java.util.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.util.Date(time));
                            }
                        } else {//default String
                            instance.put(name, val.getAsString());
                        }
                    } else {//String
                        instance.put(name, val.getAsString());
                    }
                }
            }
            return instance;
        }
    }
    return null;
}

From source file:org.virtue.cache.tools.GrandExchangeDumper.java

License:Open Source License

public static void main(String[] args) throws IOException {
    Cache cache = new Cache(FileStore.open(Constants.CACHE_REPOSITORY));
    ObjTypeList.init(cache, Constants.ITEM_DATA);
    JsonObject obj = new JsonObject();
    JsonArray arr = new JsonArray();
    DecimalFormat format = new DecimalFormat("###");

    int itemID = 0;

    File file = new File("./repository/prices.json");
    if (file.exists()) {
        JsonElement el = new JsonParser().parse(new FileReader(file));
        JsonArray entries = el.getAsJsonObject().get("items").getAsJsonArray();
        for (int index = 0; index < entries.size(); index++) {
            JsonObject ob = entries.get(index).getAsJsonObject();
            arr.add(ob);/*from  w w w  .j  a  v a2  s .co  m*/
            itemID = ob.get("id").getAsInt() + 1;
        }
    }

    int lastSleep = 0;

    double amount = 0;
    double index = arr.size();

    for (int id = 0; id < ObjTypeList.getInstance().getSize(); id++) {
        ObjType type = ObjTypeList.getInstance().list(id);
        if (type != null && type.stockmarket) {
            amount++;
        }
    }

    System.out.println("Total Items: " + amount + ", Starting ID: " + itemID);

    for (int id = itemID; id < ObjTypeList.getInstance().getSize(); id++) {
        ObjType type = ObjTypeList.getInstance().list(id);
        if (type != null && type.stockmarket) {
            try {
                URL link = new URL("http://api.rsapi.net/ge/item/" + id + ".json");
                InputStream stream = link.openStream();

                while (stream.available() < 1) {
                    ;
                }

                byte[] bytes = new byte[stream.available()];
                stream.read(bytes);
                stream.close();

                String json = new String(bytes);
                String parse = json.replace("[{", "{").replace("}]", "}").replace("\\/", "/");

                JsonElement element = new JsonParser().parse(parse);

                if (element.isJsonNull())
                    continue;

                try {
                    element.getAsJsonObject();
                } catch (Exception e) {
                    continue;
                }

                JsonElement exact = element.getAsJsonObject().get("prices").getAsJsonObject().get("exact");

                JsonObject item = new JsonObject();
                item.addProperty("id", id);
                item.addProperty("price", exact.getAsNumber());
                arr.add(item);

                lastSleep++;
                index++;

                if (lastSleep == 100) {
                    lastSleep = 0;
                    Thread.sleep(TimeUnit.MINUTES.toMillis(2));
                    System.out.println(format.format((index / amount) * 100) + "%");
                }
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
    }

    obj.add("items", arr);

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    FileWriter writer = new FileWriter("./repository/prices.json");
    writer.write(gson.toJson(obj));
    writer.close();
}