List of usage examples for com.google.gson JsonPrimitive getAsBigInteger
@Override
public BigInteger getAsBigInteger()
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 ww. j av a 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:com.synflow.cx.internal.instantiation.properties.Specializer.java
License:Open Source License
/** * Returns an IR expression from the given JSON element. * /*from w ww .j a va 2 s. c o m*/ * @param json * a JSON element (should be a primitive) * @return an expression, or <code>null</code> */ public Expression transformJson(JsonElement json) { if (json.isJsonPrimitive()) { JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isBoolean()) { return eINSTANCE.createExprBool(primitive.getAsBoolean()); } else if (primitive.isNumber()) { return eINSTANCE.createExprInt(primitive.getAsBigInteger()); } else if (primitive.isString()) { return eINSTANCE.createExprString(primitive.getAsString()); } } return null; }
From source file:org.apache.orc.tools.json.JsonSchemaFinder.java
License:Apache License
static HiveType pickType(JsonElement json) { if (json.isJsonPrimitive()) { JsonPrimitive prim = (JsonPrimitive) json; if (prim.isBoolean()) { return new BooleanType(); } else if (prim.isNumber()) { Matcher matcher = DECIMAL_PATTERN.matcher(prim.getAsString()); if (matcher.matches()) { int intDigits = matcher.group("int").length(); String fraction = matcher.group("fraction"); int scale = fraction == null ? 0 : fraction.length(); if (scale == 0) { if (intDigits < 19) { long value = prim.getAsLong(); if (value >= -128 && value < 128) { return new NumericType(HiveType.Kind.BYTE, intDigits, scale); } else if (value >= -32768 && value < 32768) { return new NumericType(HiveType.Kind.SHORT, intDigits, scale); } else if (value >= -2147483648 && value < 2147483648L) { return new NumericType(HiveType.Kind.INT, intDigits, scale); } else { return new NumericType(HiveType.Kind.LONG, intDigits, scale); }/* w ww . jav a 2s. co m*/ } else if (intDigits == 19) { // at 19 digits, it may fit inside a long, but we need to check BigInteger val = prim.getAsBigInteger(); if (val.compareTo(MIN_LONG) >= 0 && val.compareTo(MAX_LONG) <= 0) { return new NumericType(HiveType.Kind.LONG, intDigits, scale); } } } if (intDigits + scale <= MAX_DECIMAL_DIGITS) { return new NumericType(HiveType.Kind.DECIMAL, intDigits, scale); } } double value = prim.getAsDouble(); if (value >= Float.MIN_VALUE && value <= Float.MAX_VALUE) { return new NumericType(HiveType.Kind.FLOAT, 0, 0); } else { return new NumericType(HiveType.Kind.DOUBLE, 0, 0); } } else { String str = prim.getAsString(); if (TIMESTAMP_PATTERN.matcher(str).matches()) { return new StringType(HiveType.Kind.TIMESTAMP); } else if (HEX_PATTERN.matcher(str).matches()) { return new StringType(HiveType.Kind.BINARY); } else { return new StringType(HiveType.Kind.STRING); } } } else if (json.isJsonNull()) { return new NullType(); } else if (json.isJsonArray()) { ListType result = new ListType(); result.elementType = new NullType(); for (JsonElement child : ((JsonArray) json)) { HiveType sub = pickType(child); if (result.elementType.subsumes(sub)) { result.elementType.merge(sub); } else if (sub.subsumes(result.elementType)) { sub.merge(result.elementType); result.elementType = sub; } else { result.elementType = new UnionType(result.elementType, sub); } } return result; } else { JsonObject obj = (JsonObject) json; StructType result = new StructType(); for (Map.Entry<String, JsonElement> field : obj.entrySet()) { String fieldName = field.getKey(); HiveType type = pickType(field.getValue()); result.fields.put(fieldName, type); } return result; } }
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 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; }