List of usage examples for com.google.gson JsonPrimitive getAsDouble
@Override public double getAsDouble()
From source file:org.apache.sling.jms.Json.java
License:Apache License
private static <T> T toMapValue(JsonPrimitive p) { if (p.isString()) { return (T) p.getAsString(); } else if (p.isBoolean()) { return (T) ((Boolean) p.getAsBoolean()); } else if (p.isNumber()) { double d = p.getAsDouble(); if (Math.floor(d) == d) { return (T) ((Long) p.getAsLong()); }/*from w w w. j a v a 2 s . c o m*/ return (T) ((Double) d); } else { return null; } }
From source file:org.debux.webmotion.server.call.ClientSession.java
License:Open Source License
/** * Return only JsonElement use getAttribute and getAttributes with class as * parameter to get a value.// ww w .j a va 2 s .co m */ @Override public Object getAttribute(String name) { JsonElement value = attributes.get(name); if (value == null) { return value; } else if (value.isJsonPrimitive()) { JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isString()) { return primitive.getAsString(); } else if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { return primitive.getAsDouble(); } } else if (value.isJsonArray()) { return value.getAsJsonArray(); } else if (value.isJsonObject()) { return value.getAsJsonObject(); } else if (value.isJsonNull()) { return value.getAsJsonNull(); } return value; }
From source file:org.eclim.Eclim.java
License:Open Source License
public static Object toType(JsonElement json) { // null//from w ww . j a va 2 s .c o m if (json.isJsonNull()) { return null; // int, double, boolean, String } else if (json.isJsonPrimitive()) { JsonPrimitive prim = json.getAsJsonPrimitive(); if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isString()) { return prim.getAsString(); } else if (prim.isNumber()) { if (prim.getAsString().indexOf('.') != -1) { return prim.getAsDouble(); } return prim.getAsInt(); } // List } else if (json.isJsonArray()) { ArrayList<Object> type = new ArrayList<Object>(); for (JsonElement element : json.getAsJsonArray()) { type.add(toType(element)); } return type; // Map } else if (json.isJsonObject()) { HashMap<String, Object> type = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { type.put(entry.getKey(), toType(entry.getValue())); } return type; } return null; }
From source file:org.eclipse.che.api.core.jsonrpc.impl.GsonJsonRpcUnmarshaller.java
License:Open Source License
private Object getInnerItem(JsonElement jsonElement) { if (jsonElement.isJsonNull()) { return null; }/* w w w. jav a 2s . c om*/ if (jsonElement.isJsonObject()) { return jsonElement.getAsJsonObject(); } if (jsonElement.isJsonPrimitive()) { JsonPrimitive jsonPrimitive = jsonElement.getAsJsonPrimitive(); if (jsonPrimitive.isNumber()) { return jsonPrimitive.getAsDouble(); } else if (jsonPrimitive.isString()) { return jsonPrimitive.getAsString(); } else { return jsonPrimitive.getAsBoolean(); } } throw new IllegalStateException("Unexpected json element type"); }
From source file:org.eclipse.leshan.server.demo.servlet.json.LwM2mNodeDeserializer.java
License:Open Source License
private org.eclipse.leshan.core.model.ResourceModel.Type getTypeFor(JsonPrimitive val) { if (val.isBoolean()) return org.eclipse.leshan.core.model.ResourceModel.Type.BOOLEAN; if (val.isString()) return org.eclipse.leshan.core.model.ResourceModel.Type.STRING; if (val.isNumber()) { if (val.getAsDouble() == val.getAsLong()) { return org.eclipse.leshan.core.model.ResourceModel.Type.INTEGER; } else {/*from w ww . j av a2 s.co m*/ return org.eclipse.leshan.core.model.ResourceModel.Type.FLOAT; } } // use string as default value return org.eclipse.leshan.core.model.ResourceModel.Type.STRING; }
From source file:org.eclipse.leshan.server.demo.servlet.json.LwM2mNodeDeserializer.java
License:Open Source License
private Object deserializeValue(JsonPrimitive val, org.eclipse.leshan.core.model.ResourceModel.Type expectedType) { switch (expectedType) { case BOOLEAN: return val.getAsBoolean(); case STRING:// ww w . java 2 s . c om return val.getAsString(); case INTEGER: return val.getAsLong(); case FLOAT: return val.getAsDouble(); case TIME: case OPAQUE: default: // TODO we need to better handle this. return val.getAsString(); } }
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(); }/* w w w . ja va2s .com*/ 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.hibernate.search.elasticsearch.query.impl.TwoWayFieldBridgeProjection.java
License:LGPL
public void addDocumentFieldsRecursively(Document tmp, JsonElement value, String fieldName) { if (value == null || value.isJsonNull()) { return;//w w w. j a v a2 s. co m } PrimitiveProjection configuredProjection = primitiveProjections.get(fieldName); if (configuredProjection != null) { // Those projections are handled separately, see the calling method return; } if (value.isJsonObject()) { JsonObject jsonObject = value.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String nestedFieldName = fieldName + "." + entry.getKey(); JsonElement nestedFieldValue = entry.getValue(); addDocumentFieldsRecursively(tmp, nestedFieldValue, nestedFieldName); } } else if (value.isJsonArray()) { JsonArray jsonArray = value.getAsJsonArray(); for (JsonElement nestedValue : jsonArray) { addDocumentFieldsRecursively(tmp, nestedValue, fieldName); } } else { JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isBoolean()) { tmp.add(new StringField(fieldName, String.valueOf(primitive.getAsBoolean()), Store.NO)); } else if (primitive.isNumber()) { tmp.add(new DoubleField(fieldName, primitive.getAsDouble(), Store.NO)); } else if (primitive.isString()) { tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO)); } else { // TODO HSEARCH-2255 Better raise an exception? tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO)); } } }
From source file:org.hibernate.search.elasticsearch.schema.impl.DefaultElasticsearchSchemaValidator.java
License:LGPL
private static void validateJsonPrimitive(ValidationErrorCollector errorCollector, DataType type, String attributeName, JsonPrimitive expectedValue, JsonPrimitive actualValue) { DataType defaultedType = type == null ? DataType.OBJECT : type; // We can't just use equal, mainly because of floating-point numbers switch (defaultedType) { case DOUBLE://www . java2 s. c o m if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsDouble(), actualValue.getAsDouble(), DEFAULT_DOUBLE_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case FLOAT: if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsFloat(), actualValue.getAsFloat(), DEFAULT_FLOAT_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case INTEGER: case LONG: case DATE: case BOOLEAN: case STRING: case OBJECT: case GEO_POINT: default: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; } }
From source file:org.hibernate.search.elasticsearch.schema.impl.Elasticsearch2SchemaValidator.java
License:LGPL
@SuppressWarnings("deprecation") protected void doValidateJsonPrimitive(ValidationErrorCollector errorCollector, DataType type, String attributeName, JsonPrimitive expectedValue, JsonPrimitive actualValue) { // We can't just use equal, mainly because of floating-point numbers switch (type) { case DOUBLE://from w w w . jav a2 s.c o m if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsDouble(), actualValue.getAsDouble(), DEFAULT_DOUBLE_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case FLOAT: if (expectedValue.isNumber() && actualValue.isNumber()) { validateEqualWithDefault(errorCollector, attributeName, expectedValue.getAsFloat(), actualValue.getAsFloat(), DEFAULT_FLOAT_DELTA, null); } else { errorCollector.addError(MESSAGES.invalidAttributeValue(attributeName, expectedValue, actualValue)); } break; case INTEGER: case LONG: case DATE: case BOOLEAN: case STRING: case OBJECT: case GEO_POINT: default: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; } }