List of usage examples for com.google.gson JsonPrimitive getAsFloat
@Override public float getAsFloat()
From source file:at.ac.tuwien.infosys.jcloudscale.datastore.mapping.type.json.JsonFloatTypeAdapter.java
License:Apache License
@Override public Float deserialize(JsonElement element, TypeMetadata<JsonElement> typeMetadata) { JsonPrimitive jsonPrimitive = (JsonPrimitive) element; if (!jsonPrimitive.isNumber()) { throw new DatastoreException("Invalid value for float type."); }//from ww w .ja v a 2 s.c o m return jsonPrimitive.getAsFloat(); }
From source file:com.jayway.jsonpath.internal.spi.mapper.GsonMapper.java
License:Apache License
@Override public Object convert(Object src, Class<?> srcType, Class<?> targetType, Configuration conf) { assertValidConversion(src, srcType, targetType); if (src == null || src.getClass().equals(JsonNull.class)) { return null; }/*from w w w . j ava 2 s.c om*/ if (JsonPrimitive.class.isAssignableFrom(srcType)) { JsonPrimitive primitive = (JsonPrimitive) src; if (targetType.equals(Long.class)) { return primitive.getAsLong(); } else if (targetType.equals(Integer.class)) { return primitive.getAsInt(); } else if (targetType.equals(BigInteger.class)) { return primitive.getAsBigInteger(); } else if (targetType.equals(Byte.class)) { return primitive.getAsByte(); } else if (targetType.equals(BigDecimal.class)) { return primitive.getAsBigDecimal(); } else if (targetType.equals(Double.class)) { return primitive.getAsDouble(); } else if (targetType.equals(Float.class)) { return primitive.getAsFloat(); } else if (targetType.equals(String.class)) { return primitive.getAsString(); } else if (targetType.equals(Boolean.class)) { return primitive.getAsBoolean(); } else if (targetType.equals(Date.class)) { if (primitive.isNumber()) { return new Date(primitive.getAsLong()); } else if (primitive.isString()) { try { return DateFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { throw new MappingException(e); } } } } else if (JsonObject.class.isAssignableFrom(srcType)) { JsonObject srcObject = (JsonObject) src; if (targetType.equals(Map.class)) { Map<String, Object> targetMap = new LinkedHashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : srcObject.entrySet()) { Object val = null; JsonElement element = entry.getValue(); if (element.isJsonPrimitive()) { val = GsonJsonProvider.unwrap(element); } else if (element.isJsonArray()) { val = convert(element, element.getClass(), List.class, conf); } else if (element.isJsonObject()) { val = convert(element, element.getClass(), Map.class, conf); } else if (element.isJsonNull()) { val = null; } targetMap.put(entry.getKey(), val); } return targetMap; } } else if (JsonArray.class.isAssignableFrom(srcType)) { JsonArray srcArray = (JsonArray) src; if (targetType.equals(List.class)) { List<Object> targetList = new ArrayList<Object>(); for (JsonElement element : srcArray) { if (element.isJsonPrimitive()) { targetList.add(GsonJsonProvider.unwrap(element)); } else if (element.isJsonArray()) { targetList.add(convert(element, element.getClass(), List.class, conf)); } else if (element.isJsonObject()) { targetList.add(convert(element, element.getClass(), Map.class, conf)); } else if (element.isJsonNull()) { targetList.add(null); } } return targetList; } } throw new MappingException("Can not map: " + srcType.getName() + " to: " + targetType.getName()); }
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(); }/*from w ww . j a va 2 s.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.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:/* ww w. j a v a 2 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:/*w w w. j av a 2s . 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.Elasticsearch56SchemaValidator.java
License:LGPL
private 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 TEXT://from w ww . j ava 2s .co m case KEYWORD: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; case DOUBLE: 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 OBJECT: case GEO_POINT: default: validateEqualWithDefault(errorCollector, attributeName, expectedValue, actualValue, null); break; } }
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();// w ww . j a va 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; }
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; }//w ww . j a v a 2 s .co 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; }