List of usage examples for com.google.gson JsonPrimitive getAsInt
@Override public int getAsInt()
From source file:org.eclipse.jgit.diff.EditDeserializer.java
License:Apache License
private static int get(final JsonArray a, final int idx) throws JsonParseException { final JsonElement v = a.get(idx); if (!v.isJsonPrimitive()) { throw new JsonParseException("Expected array of 4 for Edit type"); }//from w w w . jav a 2 s . c o m final JsonPrimitive p = (JsonPrimitive) v; if (!p.isNumber()) { throw new JsonParseException("Expected array of 4 for Edit type"); } return p.getAsInt(); }
From source file:org.eclipse.milo.opcua.binaryschema.gson.JsonStructureCodec.java
License:Open Source License
@Override protected Object memberTypeToOpcUaScalar(JsonElement member, String typeName) { if (member == null || member.isJsonNull()) { return null; } else if (member.isJsonArray()) { JsonArray array = member.getAsJsonArray(); switch (typeName) { case "ByteString": { byte[] bs = new byte[array.size()]; for (int i = 0; i < array.size(); i++) { bs[i] = array.get(i).getAsByte(); }// ww w. j a v a 2s.c o m return ByteString.of(bs); } default: return array; } } else if (member.isJsonObject()) { JsonObject jsonObject = member.getAsJsonObject(); switch (typeName) { case "QualifiedName": { return new QualifiedName(jsonObject.get("namespaceIndex").getAsInt(), jsonObject.get("name").getAsString()); } case "LocalizedText": { return new LocalizedText(jsonObject.get("locale").getAsString(), jsonObject.get("text").getAsString()); } default: return jsonObject; } } else if (member.isJsonPrimitive()) { JsonPrimitive primitive = member.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isString()) { switch (typeName) { case "Guid": return UUID.fromString(primitive.getAsString()); case "NodeId": return NodeId.parseSafe(primitive.getAsString()).orElse(NodeId.NULL_VALUE); case "ExpandedNodeId": return ExpandedNodeId.parse(primitive.getAsString()); case "XmlElement": return new XmlElement(primitive.getAsString()); default: return primitive.getAsString(); } } else if (primitive.isNumber()) { switch (typeName) { case "SByte": return primitive.getAsByte(); case "Int16": return primitive.getAsShort(); case "Int32": return primitive.getAsInt(); case "Int64": return primitive.getAsLong(); case "Byte": return ubyte(primitive.getAsShort()); case "UInt16": return ushort(primitive.getAsInt()); case "UInt32": return uint(primitive.getAsLong()); case "UInt64": return ulong(primitive.getAsBigInteger()); case "Float": return primitive.getAsFloat(); case "Double": return primitive.getAsDouble(); case "DateTime": return new DateTime(primitive.getAsLong()); case "StatusCode": return new StatusCode(primitive.getAsLong()); default: return primitive.getAsNumber(); } } } return null; }
From source file:org.immutables.mongo.fixture.holder.HolderJsonSerializer.java
License:Apache License
@Override public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject root = (JsonObject) json; ImmutableHolder.Builder builder = ImmutableHolder.builder(); if (root.has("id")) { builder.id(root.get("id").getAsString()); }//from w w w .j av a2 s.co m JsonElement value = root.get(VALUE_PROPERTY); if (value == null) { throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type)); } if (value.isJsonObject()) { final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString(); try { Class<?> valueType = Class.forName(valueTypeName); builder.value(context.deserialize(value, valueType)); } catch (ClassNotFoundException e) { throw new JsonParseException( String.format("Couldn't construct value class %s for %s", valueTypeName, type), e); } } else if (value.isJsonPrimitive()) { final JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isString()) { builder.value(primitive.getAsString()); } else if (primitive.isNumber()) { builder.value(primitive.getAsInt()); } else if (primitive.isBoolean()) { builder.value(primitive.getAsBoolean()); } } else { throw new JsonParseException(String.format("Couldn't deserialize %s : %s. Not a primitive or object", VALUE_PROPERTY, value)); } return builder.build(); }
From source file:org.openecomp.sdc.be.model.tosca.converters.ToscaValueBaseConverter.java
License:Open Source License
public Object json2JavaPrimitive(JsonPrimitive prim) { if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isString()) { return prim.getAsString(); } else if (prim.isNumber()) { String strRepesentation = prim.getAsString(); if (strRepesentation.contains(".")) { return prim.getAsDouble(); } else {/* ww w.j av a 2 s. com*/ return prim.getAsInt(); } } else { throw new IllegalStateException(); } }
From source file:org.openhab.io.neeo.internal.NeeoUtil.java
License:Open Source License
/** * Converts a JSON property to an integer * * @param jo the non-null {@link JsonObject} to use * @param propertyName the non-empty property name * @return the possibly null integer/*from w ww . j a v a 2 s . c o m*/ */ @Nullable public static Integer getInt(JsonObject jo, String propertyName) { Objects.requireNonNull(jo, "jo cannot be null"); requireNotEmpty(propertyName, "propertyName cannot be empty"); final JsonPrimitive jp = jo.getAsJsonPrimitive(propertyName); return jp == null || jp.isJsonNull() ? null : jp.getAsInt(); }
From source file:org.qcert.camp.translator.Rule2CAMP.java
License:Open Source License
/** * Obtain the correct kind of value from a JsonPrimitive node. The kinds we support are int, String, and boolean * @param primitive the JsonPrimitive node * @return the value/*from w w w . j av a 2 s . c om*/ */ private static Object valueFromJson(JsonPrimitive primitive) { if (primitive.isString()) return primitive.getAsString(); if (primitive.isNumber()) return primitive.getAsInt(); if (primitive.isBoolean()) return primitive.getAsBoolean(); throw new IllegalStateException(); }
From source file:org.runbuddy.libtomahawk.resolver.ScriptResolver.java
License:Open Source License
private void onTestConfigFinished(JsonPrimitive results) { int type = -1; String message = null;/*from ww w .ja v a 2 s.com*/ if (results.isString()) { type = AuthenticatorManager.CONFIG_TEST_RESULT_TYPE_OTHER; message = results.getAsString(); } else if (results.isNumber() && results.getAsInt() > 0 && results.getAsInt() < 8) { type = results.getAsInt(); } Log.d(TAG, getName() + ": Config test result received. type: " + type + ", message:" + message); if (type == AuthenticatorManager.CONFIG_TEST_RESULT_TYPE_SUCCESS) { setEnabled(true); } else { setEnabled(false); } AuthenticatorManager.ConfigTestResultEvent event = new AuthenticatorManager.ConfigTestResultEvent(); event.mComponent = ScriptResolver.this; event.mType = type; event.mMessage = message; EventBus.getDefault().post(event); AuthenticatorManager.showToast(getPrettyName(), event); }
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/*w ww . 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.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();//from w ww. j ava 2 s .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; }
From source file:rpc.server.data.JSONSerializer.java
License:Open Source License
private Object fromJsonElement(JsonElement jsonElement, Type expected) throws NoSuitableSerializableFactory { if (jsonElement == null) { return null; }//from w w w .j a va 2 s.c o m // Null if (jsonElement.isJsonNull()) { return null; } // Boolean // Integer // Long // Float // Double // String // Enum if (jsonElement.isJsonPrimitive()) { JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive(); if (asJsonPrimitive.isBoolean()) { return asJsonPrimitive.getAsBoolean(); } if (asJsonPrimitive.isNumber()) { if (expected.isInteger()) { return asJsonPrimitive.getAsInt(); } if (expected.isLong()) { return asJsonPrimitive.getAsLong(); } if (expected.isFloat()) { return asJsonPrimitive.getAsFloat(); } if (expected.isDouble()) { return asJsonPrimitive.getAsDouble(); } return asJsonPrimitive.getAsNumber(); } if (asJsonPrimitive.isString()) { if (expected.isEnum()) { String value = asJsonPrimitive.getAsString(); return Enum.valueOf((Class) expected.getTypeClass(), value); } else { return asJsonPrimitive.getAsString(); } } } // Map // Serializable if (jsonElement.isJsonObject()) { JsonObject asJsonObject = jsonElement.getAsJsonObject(); if (expected.isMap()) { Map<Object, Object> map = new HashMap<Object, Object>(); Type keyType = expected.getParameterized(0); Type valueType = expected.getParameterized(1); if (!(keyType.isString() || keyType.isEnum())) { return null; } for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (keyType.isString()) { map.put(entry.getKey(), fromJsonElement(value, valueType)); } if (keyType.isEnum()) { map.put(Enum.valueOf((Class) keyType.getTypeClass(), key), fromJsonElement(value, valueType)); } } return map; } else { if (provider == null) { throw new NoSuitableSerializableFactory(); } Serializable object = provider.make(expected); for (Map.Entry<String, Type> entry : object.fields().entrySet()) { String field = entry.getKey(); Type fieldType = entry.getValue(); JsonElement value = asJsonObject.get(field); object.set(field, fromJsonElement(value, fieldType)); } return object; } } // List if (jsonElement.isJsonArray()) { JsonArray asJsonArray = jsonElement.getAsJsonArray(); int size = asJsonArray.size(); List<Object> list = new ArrayList<Object>(); Type itemType = expected.getParameterized(0); for (int i = 0; i < size; i++) { JsonElement value = asJsonArray.get(i); list.add(fromJsonElement(value, itemType)); } return list; } return null; }