List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:de.azapps.mirakel.model.task.TaskDeserializer.java
License:Open Source License
private static void handleAdditionalEntries(final Task t, final String key, final JsonElement val) { if (val.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) val; if (p.isBoolean()) { t.addAdditionalEntry(key, String.valueOf(val.getAsBoolean())); } else if (p.isNumber()) { t.addAdditionalEntry(key, String.valueOf(val.getAsInt())); } else if (p.isJsonNull()) { t.addAdditionalEntry(key, "null"); } else if (p.isString()) { t.addAdditionalEntry(key, '"' + val.getAsString() + '"'); } else {//from w w w. j a v a 2 s. co m Log.w(TAG, "unknown json-type"); } } else if (val.isJsonArray()) { final JsonArray a = (JsonArray) val; StringBuilder s = new StringBuilder("["); boolean first = true; for (final JsonElement e : a) { if (e.isJsonPrimitive()) { final JsonPrimitive p = (JsonPrimitive) e; final String add; if (p.isBoolean()) { add = String.valueOf(p.getAsBoolean()); } else if (p.isNumber()) { add = String.valueOf(p.getAsInt()); } else if (p.isString()) { add = '"' + p.getAsString() + '"'; } else if (p.isJsonNull()) { add = "null"; } else { Log.w(TAG, "unknown json-type"); break; } s.append(first ? "" : ",").append(add); first = false; } else { Log.w(TAG, "unknown json-type"); } } t.addAdditionalEntry(key, s + "]"); } else { Log.w(TAG, "unknown json-type"); } }
From source file:de.azapps.mirakel.model.task.TaskDeserializer.java
License:Open Source License
private static void handleTags(final Task t, final JsonElement val) { final JsonArray tags = val.getAsJsonArray(); final List<Tag> currentTags = new ArrayList<>(t.getTags()); for (final JsonElement tag : tags) { if (tag.isJsonPrimitive()) { String tagName = tag.getAsString(); tagName = tagName.replace("_", " "); final Tag newTag = Tag.newTag(tagName); if (!currentTags.remove(newTag)) { // tag is not linked with this task t.addTag(newTag, false, true); }/*from w w w . jav a 2s. c om*/ } } for (final Tag tag : currentTags) { // remove unused tags t.removeTag(tag, false, true); } }
From source file:de.azapps.mirakel.sync.taskwarrior.model.TaskWarriorTaskDeserializer.java
License:Open Source License
@Override public TaskWarriorTask deserialize(final JsonElement json, final Type type, final JsonDeserializationContext ctx) throws JsonParseException { final JsonObject el = json.getAsJsonObject(); final JsonElement uuid = el.get("uuid"); final JsonElement status = el.get("status"); final JsonElement entry = el.get("entry"); final JsonElement description = el.get("description"); if (uuid == null || status == null || entry == null || description == null || !uuid.isJsonPrimitive() || !status.isJsonPrimitive() || !entry.isJsonPrimitive() || !description.isJsonPrimitive() || !uuid.getAsJsonPrimitive().isString() || !status.getAsJsonPrimitive().isString() || !entry.getAsJsonPrimitive().isString() || !description.getAsJsonPrimitive().isString()) { throw new JsonParseException("Invalid syntax, missing required field"); }/* w ww .ja v a 2s. c o m*/ final TaskWarriorTask task = new TaskWarriorTask(uuid.getAsString(), status.getAsString(), parseDate(entry.getAsString()), description.getAsString()); for (final Entry<String, JsonElement> element : el.entrySet()) { switch (element.getKey()) { case "uuid": case "description": case "entry": case "status": break; case "priority": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setPriority(element.getValue().getAsString()); } else { throw new JsonParseException("priority is not a json primitive"); } break; case "priorityNumber": // taskd does not handle numbers in the right way if (element.getValue().isJsonPrimitive()) { task.setPriorityNumber((int) element.getValue().getAsDouble()); } else { throw new JsonParseException("priority is not a json primitive"); } break; case "progress": // taskd does not handle numbers in the right way if (element.getValue().isJsonPrimitive()) { task.setProgress((int) element.getValue().getAsDouble()); } else { throw new JsonParseException("progress is not a json primitive"); } break; case "project": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setProject(element.getValue().getAsString()); } else { throw new JsonParseException("project is not a json primitive"); } break; case "modification": case "modified": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setModified(parseDate(element.getValue().getAsString())); } else { throw new JsonParseException("modified is not a json primitive"); } break; case "due": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setDue(parseDate(element.getValue().getAsString())); } else { throw new JsonParseException("due is not a json primitive"); } break; case "reminder": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setReminder(parseDate(element.getValue().getAsString())); } else { throw new JsonParseException("reminder is not a json primitive"); } break; case "annotations": if (element.getValue().isJsonArray()) { final JsonArray annotations = element.getValue().getAsJsonArray(); for (int i = 0; i < annotations.size(); i++) { if (annotations.get(i).isJsonObject()) { final JsonElement descr = annotations.get(i).getAsJsonObject().get("description"); final JsonElement annotationEntry = annotations.get(i).getAsJsonObject().get("entry"); if (descr == null || annotationEntry == null || !descr.isJsonPrimitive() || !annotationEntry.isJsonPrimitive() || !descr.getAsJsonPrimitive().isString() || !annotationEntry.getAsJsonPrimitive().isString()) { throw new JsonParseException("Annotation is not valid"); } else { task.addAnnotation(descr.getAsString(), parseDate(annotationEntry.getAsString())); } } else { throw new JsonParseException("Annotation is not a json object"); } } } else { throw new JsonParseException("annotations is not a json array"); } break; case "depends": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { final String depends = element.getValue().getAsString(); task.addDepends(depends.split(",")); } else { throw new JsonParseException("depends is not a json primitive"); } break; case "tags": if (element.getValue().isJsonArray()) { final JsonArray tags = element.getValue().getAsJsonArray(); for (int i = 0; i < tags.size(); i++) { if (tags.get(i).isJsonPrimitive() && tags.get(i).getAsJsonPrimitive().isString()) { task.addTags(tags.get(i).getAsString()); } else { throw new JsonParseException("tag is not a string"); } } } else { throw new JsonParseException("tags is not a json array"); } break; case "recur": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setRecur(element.getValue().getAsString()); } else { throw new JsonParseException("recur is not a json primitive"); } break; case "imask": if (element.getValue().isJsonPrimitive()) { task.setImask((int) element.getValue().getAsDouble()); } else { throw new JsonParseException("imask is not a json primitive"); } break; case "parent": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setParent(element.getValue().getAsString()); } else { throw new JsonParseException("parent is not a json primitive"); } break; case "mask": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setMask(element.getValue().getAsString()); } else { throw new JsonParseException("mask is not a json primitive"); } break; case "until": if (element.getValue().isJsonPrimitive() && element.getValue().getAsJsonPrimitive().isString()) { task.setUntil(parseDate(element.getValue().getAsString())); } else { throw new JsonParseException("until is not a json primitive"); } break; default: task.addUDA(element.getKey(), element.getValue().getAsString()); break; } } return task; }
From source file:de.innovationgate.wgpublisher.webtml.utils.JsonUtils.java
License:Open Source License
public Object jsonToJava(JsonElement jsonValue) { Object value = null;//from w ww .j a va 2 s. c o m if (jsonValue.isJsonNull()) { value = null; } else if (jsonValue.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) jsonValue; if (prim.isNumber()) { value = prim.getAsDouble(); } else if (prim.isBoolean()) { value = prim.getAsBoolean(); } else { value = prim.getAsString(); } value = jsonToJavaConversions(value); } else if (jsonValue.isJsonArray()) { JsonArray array = jsonValue.getAsJsonArray(); List<Object> list = new ArrayList<Object>(); for (JsonElement element : array) { list.add(jsonToJava(element)); } } else if (jsonValue.isJsonObject()) { JsonObject obj = jsonValue.getAsJsonObject(); Map<String, Object> map = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { map.put(String.valueOf(entry.getKey()), jsonToJava(entry.getValue())); } } return value; }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static float getFloatVal(JsonElement json) { if (json == null || json.isJsonNull()) { throw new JsonSyntaxException("Json cannot be null"); }/*from www . jav a2 s .co m*/ if (!json.isJsonPrimitive()) { throw new JsonSyntaxException("Expected value to be a primitive"); } return json.getAsFloat(); }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static float getFloatVal(JsonElement json, float defVal) { if (json == null || !json.isJsonPrimitive()) { return defVal; }/* w w w. j ava 2s . c o m*/ return json.getAsFloat(); }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static String getStringVal(JsonElement json) { if (json == null || json.isJsonNull()) { return null; }/*from w w w . j a va 2 s. com*/ if (!json.isJsonPrimitive()) { throw new JsonSyntaxException("Expected value to be a primitive"); } return json.getAsString(); }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static String getStringVal(JsonElement json, String defVal) { if (json == null || !json.isJsonPrimitive()) { return defVal; }//www . ja v a2s. co m return json.getAsString(); }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static int getIntVal(JsonElement json) { if (json == null || json.isJsonNull()) { throw new JsonSyntaxException("Json cannot be null"); }/*from ww w. jav a2 s. c o m*/ if (!json.isJsonPrimitive()) { throw new JsonSyntaxException("Expected value to be a primitive"); } return json.getAsInt(); }
From source file:de.sanandrew.mods.sanlib.lib.util.JsonUtils.java
License:Creative Commons License
public static int getIntVal(JsonElement json, int defVal) { if (json == null || !json.isJsonPrimitive()) { return defVal; }/*w ww . j av a 2 s . c o m*/ return json.getAsInt(); }