List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:org.tsukuba_bunko.lilac.web.controller.JsonRequestParser.java
License:Open Source License
/** * JSON?Java?????//from w ww. ja va 2s . com * @param elementJSON? * @return Java */ public Object getJavaObject(JsonElement element) { if (element.isJsonObject()) { return getJavaObject(element.getAsJsonObject()); } else if (element.isJsonArray()) { return getJavaObject(element.getAsJsonArray()); } else if (element.isJsonPrimitive()) { return getJavaObject(element.getAsJsonPrimitive()); } else { return null; } }
From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractDetailedElementDeserializer.java
License:Open Source License
/** * {@inheritDoc}/*from w w w . jav a 2 s. c om*/ * * @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.AbstractTuleapDeserializer.java
License:Open Source License
/** * {@inheritDoc}/* w ww . j av a 2 s .c om*/ * * @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(); T pojo = super.deserialize(rootJsonElement, type, context); JsonElement jsonTrackerRef = jsonObject.get(ITuleapConstants.JSON_TRACKER); TuleapReference trackerRef = context.deserialize(jsonTrackerRef, TuleapReference.class); pojo.setTracker(trackerRef); JsonArray fields = jsonObject.get(ITuleapConstants.VALUES).getAsJsonArray(); for (JsonElement field : fields) { JsonObject jsonField = field.getAsJsonObject(); int fieldId = jsonField.get(ITuleapConstants.FIELD_ID).getAsInt(); if (jsonField.has(ITuleapConstants.FIELD_VALUE)) { JsonElement jsonValue = jsonField.get(ITuleapConstants.FIELD_VALUE); if (jsonValue.isJsonPrimitive()) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (primitive.isString()) { pojo.addFieldValue(new LiteralFieldValue(fieldId, primitive.getAsString())); } else if (primitive.isNumber()) { pojo.addFieldValue(new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsNumber()))); } else if (primitive.isBoolean()) { pojo.addFieldValue( new LiteralFieldValue(fieldId, String.valueOf(primitive.getAsBoolean()))); } } } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_ID) && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID).isJsonNull()) { // sb? JsonElement jsonBindValueId = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_ID); int bindValueId = jsonBindValueId.getAsInt(); pojo.addFieldValue(new BoundFieldValue(fieldId, Lists.newArrayList(Integer.valueOf(bindValueId)))); } else if (jsonField.has(ITuleapConstants.FIELD_BIND_VALUE_IDS) && !jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS).isJsonNull()) { // sb?, msb, cb, or tbl (open list) JsonElement jsonBindValueIds = jsonField.get(ITuleapConstants.FIELD_BIND_VALUE_IDS); JsonArray jsonIds = jsonBindValueIds.getAsJsonArray(); if (jsonIds.size() > 0) { JsonPrimitive firstElement = jsonIds.get(0).getAsJsonPrimitive(); if (firstElement.isString()) { // Open list (tbl) List<String> bindValueIds = new ArrayList<String>(); for (JsonElement idElement : jsonIds) { bindValueIds.add(idElement.getAsString()); } pojo.addFieldValue(new OpenListFieldValue(fieldId, bindValueIds)); } else { List<Integer> bindValueIds = new ArrayList<Integer>(); for (JsonElement idElement : jsonIds) { bindValueIds.add(Integer.valueOf(idElement.getAsInt())); } pojo.addFieldValue(new BoundFieldValue(fieldId, bindValueIds)); } } else { pojo.addFieldValue(new BoundFieldValue(fieldId, Collections.<Integer>emptyList())); } } else if (jsonField.has(ITuleapConstants.FIELD_LINKS) && !jsonField.get(ITuleapConstants.FIELD_LINKS).isJsonNull()) { // Artifact links pojo.addFieldValue( context.<ArtifactLinkFieldValue>deserialize(jsonField, ArtifactLinkFieldValue.class)); } else if (jsonField.has(ITuleapConstants.FILE_DESCRIPTIONS) && jsonField.get(ITuleapConstants.FILE_DESCRIPTIONS).isJsonArray()) { AttachmentFieldValue value = context.deserialize(jsonField, AttachmentFieldValue.class); pojo.addFieldValue(value); } } return pojo; }
From source file:org.tuleap.mylyn.task.core.internal.parser.DateIso8601Adapter.java
License:Open Source License
/** * {@inheritDoc}/*from w ww. j a va2 s.c o m*/ * * @see com.google.gson.JsonDeserializer#deserialize(com.google.gson.JsonElement, java.lang.reflect.Type, * com.google.gson.JsonDeserializationContext) */ @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { try { return parseIso8601Date(json.getAsString()); } catch (ParseException e) { throw new JsonParseException(e); } } throw new JsonParseException(TuleapCoreMessages.getString(TuleapCoreKeys.invalidDate, json.toString())); }
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//from ww w.j a v a2 s . 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.waveprotocol.wave.communication.gson.GsonUtil.java
License:Apache License
/** * Unpack a JsonElement into the object type * * @param <T> The type to deserialize * @param object The object used to accept the pare result * @param valueObj The root of a tree of JsonElements or an indirection index * @param gson A Gson context/*from w ww.j av a 2 s.co m*/ * @param raw * @throws GsonException */ public static <T extends GsonSerializable> void extractJsonObject(T object, JsonElement valueObj, Gson gson, RawStringData raw) throws GsonException { if (valueObj.isJsonObject()) { object.fromGson(valueObj.getAsJsonObject(), gson, raw); } else if (valueObj.isJsonPrimitive()) { JsonPrimitive primitive = valueObj.getAsJsonPrimitive(); String s = null; if (raw == null || !primitive.isString()) { throw new GsonException("Decoding " + valueObj + " as object " + object.getClass() + " with no RawStringData given"); } s = raw.getString(valueObj.getAsString()); GsonUtil.parseJson(object, gson, s, raw); } else { throw new GsonException( "Cannot decode valueObject " + valueObj.getClass() + " as object " + object.getClass()); } }
From source file:org.wikimedia.analytics.kraken.funnel.UserActionNode.java
License:Open Source License
/** * Instantiates a new node./*from w w w . j a v a 2 s .com*/ * * @param json {@link com.google.gson.JsonObject} NOTE: keys must be upper-case */ public UserActionNode(final JsonObject json) { /** * // TODO: this gets repeated for every instantiation so it should be * factored out, this will be taken care of once we store the EventLogging * data using AVRO. * * This function takes as input different fields from the EventLogging * extension and turns it into a single string that is in the form: * client:languagecode:project:namespace:page:event {@link Node} * * @param project the project variable as generated by the EventLogging extension. * @param namespace the namespace variable as generated by the EventLogging extension. * @param page the page variable as generated by the EventLogging extension. * @param event the event variable as generated by the EventLogging extension. */ componentValues = new HashMap<ComponentType, String>(); for (ComponentType type : ComponentType.values()) { JsonElement value = json.get(type.toString().toLowerCase()); if (value != null && value.isJsonPrimitive()) { String valueString = value.getAsString(); switch (type) { case PROJECT: componentValues.putAll(splitProject(valueString)); break; default: componentValues.put(type, valueString); break; } } } }
From source file:org.wso2.ballerina.nativeimpl.lang.json.GetBoolean.java
License:Open Source License
@Override public BValue[] execute(Context ctx) { String jsonPath = null;/* w w w. java 2 s .com*/ BValue result = null; try { // Accessing Parameters. BJSON json = (BJSON) getArgument(ctx, 0); jsonPath = getArgument(ctx, 1).stringValue(); // Getting the value from JSON ReadContext jsonCtx = JsonPath.parse(json.value()); JsonElement element = jsonCtx.read(jsonPath); if (element == null) { throw new BallerinaException("No matching element found for jsonpath: " + jsonPath); } else if (element.isJsonPrimitive()) { // if the resulting value is a primitive, return the respective primitive value object JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isBoolean()) { result = new BBoolean(value.getAsBoolean()); } else { throw new BallerinaException("The element matching path: " + jsonPath + " is not a Boolean."); } } else { throw new BallerinaException( "The element matching path: " + jsonPath + " is a JSON, not a Boolean."); } } catch (PathNotFoundException e) { ErrorHandler.handleNonExistingJsonpPath(OPERATION, jsonPath, e); } catch (InvalidPathException e) { ErrorHandler.handleInvalidJsonPath(OPERATION, e); } catch (JsonPathException e) { ErrorHandler.handleJsonPathException(OPERATION, e); } catch (Throwable e) { ErrorHandler.handleJsonPathException(OPERATION, e); } // Setting output value. return getBValues(result); }
From source file:org.wso2.ballerina.nativeimpl.lang.json.GetDouble.java
License:Open Source License
@Override public BValue[] execute(Context ctx) { String jsonPath = null;//from ww w . java 2 s . c o m BValue result = null; try { // Accessing Parameters. BJSON json = (BJSON) getArgument(ctx, 0); jsonPath = getArgument(ctx, 1).stringValue(); // Getting the value from JSON ReadContext jsonCtx = JsonPath.parse(json.value()); Object elementObj = jsonCtx.read(jsonPath); if (elementObj == null) { throw new BallerinaException("No matching element found for jsonpath: " + jsonPath); } else if (elementObj instanceof JsonElement) { JsonElement element = (JsonElement) elementObj; if (element.isJsonPrimitive()) { // if the resulting value is a primitive, return the respective primitive value object JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isNumber()) { Number number = value.getAsNumber(); if (number instanceof Float || number instanceof Double) { result = new BDouble(number.doubleValue()); } else { throw new BallerinaException( "The element matching path: " + jsonPath + " is not a Double."); } } else { throw new BallerinaException( "The element matching path: " + jsonPath + " is not a Double."); } } else { throw new BallerinaException( "The element matching path: " + jsonPath + " is a JSON, not a Double."); } } else if (elementObj instanceof Double) { // this handles the JsonPath's min(), max(), avg(), stddev() function result = new BDouble((Double) elementObj); } } catch (PathNotFoundException e) { ErrorHandler.handleNonExistingJsonpPath(OPERATION, jsonPath, e); } catch (InvalidPathException e) { ErrorHandler.handleInvalidJsonPath(OPERATION, e); } catch (JsonPathException e) { ErrorHandler.handleJsonPathException(OPERATION, e); } catch (Throwable e) { ErrorHandler.handleJsonPathException(OPERATION, e); } // Setting output value. return getBValues(result); }