List of usage examples for com.google.gson JsonPrimitive isNumber
public boolean isNumber()
From source file:org.trimou.gson.resolver.JsonElementResolver.java
License:Apache License
private Object unwrapJsonPrimitive(JsonPrimitive jsonPrimitive) { if (jsonPrimitive.isBoolean()) { return jsonPrimitive.getAsBoolean(); } else if (jsonPrimitive.isString()) { return jsonPrimitive.getAsString(); } else if (jsonPrimitive.isNumber()) { return jsonPrimitive.getAsNumber(); }/* w ww . j a va 2 s . c o m*/ return jsonPrimitive; }
From source file:org.tsukuba_bunko.lilac.web.controller.JsonRequestParser.java
License:Open Source License
/** * JavaScript?(?,,)Java?????// w w w . j av a2 s .c o m * @param jsonPrimitiveJavaScript? * @returnJava */ public Object getJavaObject(JsonPrimitive jsonPrimitive) { if (jsonPrimitive.isBoolean()) { return jsonPrimitive.getAsBoolean(); } else if (jsonPrimitive.isNumber()) { return jsonPrimitive.getAsNumber(); } else { return jsonPrimitive.getAsString(); } }
From source file:org.tuleap.mylyn.task.core.internal.parser.AbstractTuleapDeserializer.java
License:Open Source License
/** * {@inheritDoc}//ww w .j av a 2s .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 { 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.wso2.ballerina.nativeimpl.lang.json.GetDouble.java
License:Open Source License
@Override public BValue[] execute(Context ctx) { String jsonPath = null;/*from w w w . j ava 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()); 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); }
From source file:org.wso2.ballerina.nativeimpl.lang.json.GetFloat.java
License:Open Source License
@Override public BValue[] execute(Context ctx) { String jsonPath = null;//w w w . jav a 2 s. c om 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.isNumber()) { Number number = value.getAsNumber(); if (number instanceof Float || number instanceof Double) { result = new BFloat(number.floatValue()); } else { throw new BallerinaException("The element matching path: " + jsonPath + " is not a Float."); } } else { throw new BallerinaException("The element matching path: " + jsonPath + " is not a Float."); } } else { throw new BallerinaException("The element matching path: " + jsonPath + " is a JSON, not a Float."); } } 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.GetInt.java
License:Open Source License
@Override public BValue[] execute(Context ctx) { String jsonPath = null;//from www . j ava2 s . c om 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 Integer | number instanceof Long | number instanceof Short) { result = new BInteger(number.intValue()); } else { throw new BallerinaException( "The element matching path: " + jsonPath + " is not an Integer."); } } else { throw new BallerinaException( "The element matching path: " + jsonPath + " is not an Integer."); } } else { throw new BallerinaException( "The element matching path: " + jsonPath + " is a JSON, not an Integer."); } } else if (elementObj instanceof Integer) { // this handles the JsonPath's length() function result = new BInteger((Integer) 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); }
From source file:org.wso2.carbon.identity.entitlement.endpoint.util.JSONRequestParser.java
License:Open Source License
/** * Converts a given <code>{@link JsonElement}</code> to a <code>String</code> DataType * Predicted based on XACML 3.0 JSON profile * * @param element/*from w w w.jav a 2 s . co m*/ * @return */ private static String jsonElementToDataType(JsonPrimitive element) { if (element.isString()) { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_STRING; } else if (element.isBoolean()) { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_BOOLEAN; } else if (element.isNumber()) { double n1 = element.getAsDouble(); int n2 = element.getAsInt(); if (Math.ceil(n1) == n2) { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_INTEGER; } else { return EntitlementEndpointConstants.ATTRIBUTE_DATA_TYPE_DOUBLE; } } return null; }
From source file:org.wso2.developerstudio.datamapper.diagram.schemagen.util.SchemaBuilder.java
License:Open Source License
private static TypeEnum RealTypeOf(JsonElement element) { if (element == null || element.isJsonNull()) { return TypeEnum.NULL; } else if (element.isJsonArray()) { return TypeEnum.ARRAY; } else if (element.isJsonObject()) { return TypeEnum.OBJECT; } else if (element.isJsonPrimitive()) { JsonPrimitive p = element.getAsJsonPrimitive(); if (p.isNumber()) { return TypeEnum.NUMBER; } else if (p.isBoolean()) { return TypeEnum.BOOLEAN; } else if (p.isString()) { String value = p.getAsString(); if (StringUtils.isNotEmpty(value)) { return TypeEnum.STRING; } else { return TypeEnum.NULL; }/*w ww .j ava 2 s . c om*/ } } return TypeEnum.UNDEFINED; }
From source file:org.zoxweb.server.util.GSONUtil.java
License:Apache License
public static QueryRequest fromQueryRequest(String json) { JsonElement je = new JsonParser().parse(json); QueryRequest ret = null;//from ww w . jav a2 s.c o m if (je instanceof JsonObject) { ret = new QueryRequest(); JsonObject jo = (JsonObject) je; ret.setCanonicalID(jo.get(MetaToken.CANONICAL_ID.getName()).getAsString()); JsonElement batchSize = jo.get("batch_size"); if (batchSize != null) { ret.setBatchSize(batchSize.getAsInt()); } JsonArray jaFNs = (JsonArray) jo.get("field_names"); if (jaFNs != null) { List<String> fieldNames = new ArrayList<String>(); for (int i = 0; i < jaFNs.size(); i++) { fieldNames.add(jaFNs.get(i).getAsString()); } ret.setFieldNames(fieldNames); } JsonArray jaQuery = (JsonArray) jo.get("query"); if (jaQuery != null) { List<QueryMarker> qms = new ArrayList<QueryMarker>(); for (int i = 0; i < jaQuery.size(); i++) { // get the query marker JsonObject joQM = (JsonObject) jaQuery.get(i); QueryMarker qm = null; JsonPrimitive lo = (JsonPrimitive) joQM.get(MetaToken.LOGICAL_OPERATOR.getName()); if (lo != null) { qm = Const.LogicalOperator.valueOf(lo.getAsString()); } else { Const.RelationalOperator ro = null; JsonPrimitive jpRO = (JsonPrimitive) joQM.get(MetaToken.RELATIONAL_OPERATOR.getName()); if (jpRO != null) { ro = Const.RelationalOperator.valueOf(jpRO.getAsString()); } String name = null; JsonElement value = null; Set<Map.Entry<String, JsonElement>> allParams = joQM.entrySet(); for (Map.Entry<String, JsonElement> e : allParams) { if (!e.getKey().equals(MetaToken.RELATIONAL_OPERATOR.getName())) { name = e.getKey(); value = e.getValue(); break; } } // try to guess the type if (value.isJsonPrimitive()) { JsonPrimitive jp = (JsonPrimitive) value; if (jp.isString()) { qm = new QueryMatchString(ro, jp.getAsString(), name); } else if (jp.isNumber()) { qm = new QueryMatchLong(ro, jp.getAsLong(), name); } } } if (qm != null) { qms.add(qm); } } ret.setQuery(qms); } } return ret; }
From source file:org.zoxweb.server.util.GSONUtil.java
License:Apache License
public static NVBase<?> guessPrimitive(String name, NVConfig nvc, JsonPrimitive jp) { GNVType gnvType = nvc != null ? GNVType.toGNVType(nvc) : null; if (gnvType == null) { GNVTypeName tn = GNVType.toGNVTypeName(name, ':'); if (tn != null) { gnvType = tn.getType();// www. ja v a 2s . c o m name = tn.getName(); } } if (gnvType != null) { switch (gnvType) { case NVBLOB: try { byte value[] = SharedBase64.decode(Base64Type.URL, jp.getAsString()); return new NVBlob(name, value); } catch (Exception e) { } break; case NVBOOLEAN: return new NVBoolean(name, jp.getAsBoolean()); case NVDOUBLE: return new NVDouble(name, jp.getAsDouble()); case NVFLOAT: return new NVFloat(name, jp.getAsFloat()); case NVINT: return new NVInt(name, jp.getAsInt()); case NVLONG: return new NVLong(name, jp.getAsLong()); } } if (jp.isBoolean()) { return new NVBoolean(name, jp.getAsBoolean()); } else if (jp.isNumber()) { // if there is no dots it should be a //if (jp.getAsString().indexOf(".") == -1) { try { Number number = SharedUtil.parseNumber(jp.getAsString()); return SharedUtil.numberToNVBase(name, number); } catch (NumberFormatException e) { e.printStackTrace(); } } //else { try { return new NVDouble(name, jp.getAsDouble()); } catch (NumberFormatException e) { e.printStackTrace(); } } } else if (jp.isString()) { try { byte value[] = SharedBase64.decodeWrappedAsString(jp.getAsString()); return new NVBlob(name, value); } catch (Exception e) { } try { Long.parseLong(jp.getAsString()); } catch (Exception e) { if (TimestampFilter.SINGLETON.isValid(jp.getAsString())) { return new NVLong(name, TimestampFilter.SINGLETON.validate(jp.getAsString())); } } return new NVPair(name, jp.getAsString()); } return null; }