List of usage examples for com.google.gson JsonPrimitive isBoolean
public boolean isBoolean()
From source file:org.testeditor.fixture.web.json.BrowserSetupReader.java
License:Open Source License
private BrowserSetting createBrowserSettting(Entry<String, JsonElement> browserSettingEntry) { BrowserSetting browserSetting = null; String key = browserSettingEntry.getKey(); JsonElement value = browserSettingEntry.getValue(); JsonPrimitive browserSettingAsJsonPrimitive = value.getAsJsonPrimitive(); if (browserSettingAsJsonPrimitive.isString()) { browserSetting = new BrowserSetting(key, value.getAsString()); } else if (browserSettingAsJsonPrimitive.isBoolean()) { browserSetting = new BrowserSetting(key, value.getAsBoolean()); } else if (browserSettingAsJsonPrimitive.isNumber()) { browserSetting = new BrowserSetting(key, value.getAsInt()); }/*from w w w . j a va2s .co m*/ return browserSetting; }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
private static List<KeyValueProto> parseProtoValues(JsonObject valuesObject) { List<KeyValueProto> result = new ArrayList<>(); for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) { JsonElement element = valueEntry.getValue(); if (element.isJsonPrimitive()) { JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isString()) { if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) { String message = String.format( "String value length [%d] for key [%s] is greater than maximum allowed [%d]", value.getAsString().length(), valueEntry.getKey(), maxStringValueLength); throw new JsonSyntaxException(message); }//from ww w .j a v a 2s. c o m if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) { try { result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); } catch (RuntimeException th) { result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()) .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build()); } } else { result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()) .setType(KeyValueType.STRING_V).setStringV(value.getAsString()).build()); } } else if (value.isBoolean()) { result.add(KeyValueProto.newBuilder().setKey(valueEntry.getKey()) .setType(KeyValueType.BOOLEAN_V).setBoolV(value.getAsBoolean()).build()); } else if (value.isNumber()) { result.add(buildNumericKeyValueProto(value, valueEntry.getKey())); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value); } } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element); } } return result; }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
private static List<KvEntry> parseValues(JsonObject valuesObject) { List<KvEntry> result = new ArrayList<>(); for (Entry<String, JsonElement> valueEntry : valuesObject.entrySet()) { JsonElement element = valueEntry.getValue(); if (element.isJsonPrimitive()) { JsonPrimitive value = element.getAsJsonPrimitive(); if (value.isString()) { if (maxStringValueLength > 0 && value.getAsString().length() > maxStringValueLength) { String message = String.format( "String value length [%d] for key [%s] is greater than maximum allowed [%d]", value.getAsString().length(), valueEntry.getKey(), maxStringValueLength); throw new JsonSyntaxException(message); }/*from w w w . j av a2 s.com*/ if (isTypeCastEnabled && NumberUtils.isParsable(value.getAsString())) { try { parseNumericValue(result, valueEntry, value); } catch (RuntimeException th) { result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); } } else { result.add(new StringDataEntry(valueEntry.getKey(), value.getAsString())); } } else if (value.isBoolean()) { result.add(new BooleanDataEntry(valueEntry.getKey(), value.getAsBoolean())); } else if (value.isNumber()) { parseNumericValue(result, valueEntry, value); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + value); } } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + element); } } return result; }
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(); }//from w w w . jav a 2 s .c om return jsonPrimitive; }
From source file:org.tsukuba_bunko.lilac.web.controller.JsonRequestParser.java
License:Open Source License
/** * JavaScript?(?,,)Java?????// ww w .j av a2s .co 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}/*from www .j av a 2s . 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(); 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.GetBoolean.java
License:Open Source License
@Override public BValue[] execute(Context ctx) { String jsonPath = null;//from w w w . ja v a2s . co 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()); 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.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 ww w. j a v 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; }/*from w ww. ja v a 2 s . c om*/ } } return TypeEnum.UNDEFINED; }
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();// ww w.j a v a 2s. c om 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; }