List of usage examples for com.google.gson JsonPrimitive isNumber
public boolean isNumber()
From source file:org.apache.hadoop.hive.json.JsonShredder.java
License:Apache License
private void shredObject(String name, JsonElement json) throws IOException { if (json.isJsonPrimitive()) { JsonPrimitive primitive = (JsonPrimitive) json; if (primitive.isBoolean()) { getFile(name).append(primitive.getAsBoolean() + "\n"); } else if (primitive.isString()) { getFile(name).append(primitive.getAsString().replace("\\", "\\\\").replace("\n", "\\n") + "\n"); } else if (primitive.isNumber()) { getFile(name).append(primitive.getAsNumber() + "\n"); }// w w w. j a va 2 s . c o m } else if (json.isJsonNull()) { // just skip it } else if (json.isJsonArray()) { for (JsonElement child : ((JsonArray) json)) { shredObject(name, child); } } else { JsonObject obj = (JsonObject) json; for (Map.Entry<String, JsonElement> field : obj.entrySet()) { String fieldName = field.getKey(); shredObject(name + "." + fieldName, field.getValue()); } } }
From source file:org.apache.lens.driver.es.client.jest.JestResultSetTransformer.java
License:Apache License
private Type getDataType(int colPosition, JsonElement jsonObjectValue) { if (columnDataTypes.get(colPosition) != Type.NULL_TYPE) { return columnDataTypes.get(colPosition); }// www .ja v a2 s .c o m final JsonPrimitive jsonPrimitive = jsonObjectValue.getAsJsonPrimitive(); if (jsonPrimitive.isJsonNull()) { return Type.NULL_TYPE; } final Type type; if (jsonPrimitive.isBoolean()) { type = Type.BOOLEAN_TYPE; } else if (jsonPrimitive.isNumber()) { type = Type.DOUBLE_TYPE; } else { type = Type.STRING_TYPE; } columnDataTypes.set(colPosition, type); return type; }
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); }/*from www .j a v a 2 s .c om*/ } 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.apache.qpid.disttest.json.PropertyValueAdapter.java
License:Apache License
@Override public PropertyValue deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonNull()) { return null; } else if (json.isJsonPrimitive()) { Object result = null;//www . j av a 2s . c o m JsonPrimitive primitive = json.getAsJsonPrimitive(); if (primitive.isString()) { result = primitive.getAsString(); } else if (primitive.isNumber()) { String asString = primitive.getAsString(); if (asString.indexOf('.') != -1 || asString.indexOf('e') != -1) { result = primitive.getAsDouble(); } else { result = primitive.getAsLong(); } } else if (primitive.isBoolean()) { result = primitive.getAsBoolean(); } else { throw new JsonParseException("Unsupported primitive value " + primitive); } return new SimplePropertyValue(result); } else if (json.isJsonArray()) { JsonArray array = json.getAsJsonArray(); List<Object> result = new ArrayList<Object>(array.size()); for (JsonElement element : array) { result.add(context.deserialize(element, Object.class)); } return new SimplePropertyValue(result); } else if (json.isJsonObject()) { JsonObject object = json.getAsJsonObject(); JsonElement defElement = object.getAsJsonPrimitive(DEF_FIELD); Class<?> classInstance = null; if (defElement != null) { try { classInstance = _factory.getPropertyValueClass(defElement.getAsString()); } catch (ClassNotFoundException e) { // ignore } } if (classInstance == null) { Map<String, Object> result = new HashMap<String, Object>(); for (Map.Entry<String, JsonElement> entry : object.entrySet()) { Object value = context.deserialize(entry.getValue(), Object.class); result.put(entry.getKey(), value); } return new SimplePropertyValue(result); } else { return context.deserialize(json, classInstance); } } else { throw new JsonParseException("Unsupported JSON type " + json); } }
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()); }//w w w . j a v a2 s.co m return (T) ((Double) d); } else { return null; } }
From source file:org.cvasilak.jboss.mobile.app.model.ManagementModelBase.java
License:Apache License
public void setValue(JsonElement value) { if (value instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) value; if (primitive.isNumber()) { try { this.value = NumberFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { }// ww w . ja v a 2 s.c o m } else if (primitive.isBoolean()) { this.value = primitive.getAsBoolean(); } else if (primitive.isString()) { this.value = primitive.getAsString(); } } else if (value instanceof JsonNull) { this.value = "undefined"; } else if (value instanceof JsonArray) { List<String> list = new ArrayList<String>(); Iterator<JsonElement> iterator = value.getAsJsonArray().iterator(); while (iterator.hasNext()) { JsonElement elem = iterator.next(); if (elem instanceof JsonObject) list.add(elem.toString()); else list.add(elem.getAsString()); } this.value = list; } else if (value instanceof JsonObject) { this.value = value.toString(); } }
From source file:org.cvasilak.jboss.mobile.app.model.OperationParameter.java
License:Apache License
public void setDefaultValue(JsonElement value) { if (value instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) value; if (primitive.isNumber()) { try { this.defaultValue = NumberFormat.getInstance().parse(primitive.getAsString()); } catch (ParseException e) { }/* ww w. ja v a2 s .c om*/ } else if (primitive.isBoolean()) { this.defaultValue = primitive.getAsBoolean(); } else if (primitive.isString()) { this.defaultValue = primitive.getAsString(); } } }
From source file:org.dashbuilder.dataprovider.backend.elasticsearch.rest.client.impl.jest.ElasticSearchJestClient.java
License:Apache License
/** * Parses a given value (for a given column type) returned by response JSON query body from EL server. * * @param column The data column definition. * @param valueElement The value element from JSON query response to format. * @return The formatted value for the given column type. *///from w w w .j a va2 s .c o m public static Object parseValue(ElasticSearchDataSetDef definition, ElasticSearchDataSetMetadata metadata, DataColumn column, JsonElement valueElement) { if (column == null || valueElement == null || valueElement.isJsonNull()) return null; if (!valueElement.isJsonPrimitive()) throw new RuntimeException("Not expected JsonElement type to parse from query response."); JsonPrimitive valuePrimitive = valueElement.getAsJsonPrimitive(); ColumnType columnType = column.getColumnType(); if (ColumnType.NUMBER.equals(columnType)) { return valueElement.getAsDouble(); } else if (ColumnType.DATE.equals(columnType)) { // We can expect two return core types from EL server when handling dates: // 1.- String type, using the field pattern defined in the index' mappings, when it's result of a query without aggregations. // 2.- Numeric type, when it's result from a scalar function or a value pickup. if (valuePrimitive.isString()) { DateTimeFormatter formatter = null; String datePattern = metadata.getFieldPattern(column.getId()); if (datePattern == null || datePattern.trim().length() == 0) { // If no custom pattern for date field, use the default by EL -> org.joda.time.format.ISODateTimeFormat#dateOptionalTimeParser formatter = ElasticSearchDataSetProvider.EL_DEFAULT_DATETIME_FORMATTER; } else { // Obtain the date value by parsing using the EL pattern specified for this field. formatter = DateTimeFormat.forPattern(datePattern); } DateTime dateTime = formatter.parseDateTime(valuePrimitive.getAsString()); return dateTime.toDate(); } if (valuePrimitive.isNumber()) { return new Date(valuePrimitive.getAsLong()); } throw new UnsupportedOperationException( "Value core type not supported. Expecting string or number when using date core field types."); } // LABEL, TEXT or grouped DATE column types. String valueAsString = valueElement.getAsString(); ColumnGroup columnGroup = column.getColumnGroup(); // For FIXED date values, remove the unnecessary "0" at first character. (eg: replace month "01" to "1") if (columnGroup != null && GroupStrategy.FIXED.equals(columnGroup.getStrategy()) && valueAsString.startsWith("0")) return valueAsString.substring(1); return valueAsString; }
From source file:org.dashbuilder.dataprovider.backend.elasticsearch.rest.impl.jest.ElasticSearchJestClient.java
License:Apache License
/** * Parses a given value (for a given column type) returned by response JSON query body from EL server. * * @param column The data column definition. * @param valueElement The value element from JSON query response to format. * @return The object value for the given column type. *//*ww w.j a va2 s.c o m*/ public Object parseValue(DataSetMetadata metadata, DataColumn column, JsonElement valueElement) throws ParseException { if (column == null || valueElement == null || valueElement.isJsonNull()) return null; if (!valueElement.isJsonPrimitive()) throw new RuntimeException("Not expected JsonElement type to parse from query response."); ElasticSearchDataSetDef def = (ElasticSearchDataSetDef) metadata.getDefinition(); JsonPrimitive valuePrimitive = valueElement.getAsJsonPrimitive(); ColumnType columnType = column.getColumnType(); if (ColumnType.TEXT.equals(columnType)) { return typeMapper.parseText(def, column.getId(), valueElement.getAsString()); } else if (ColumnType.LABEL.equals(columnType)) { boolean isColumnGroup = column.getColumnGroup() != null && column.getColumnGroup().getStrategy().equals(GroupStrategy.FIXED); return typeMapper.parseLabel(def, column.getId(), valueElement.getAsString(), isColumnGroup); } else if (ColumnType.NUMBER.equals(columnType)) { return typeMapper.parseNumeric(def, column.getId(), valueElement.getAsString()); } else if (ColumnType.DATE.equals(columnType)) { // We can expect two return core types from EL server when handling dates: // 1.- String type, using the field pattern defined in the index' mappings, when it's result of a query without aggregations. // 2.- Numeric type, when it's result from a scalar function or a value pickup. if (valuePrimitive.isString()) { return typeMapper.parseDate(def, column.getId(), valuePrimitive.getAsString()); } if (valuePrimitive.isNumber()) { return typeMapper.parseDate(def, column.getId(), valuePrimitive.getAsLong()); } } throw new UnsupportedOperationException("Cannot parse value for column with id [" + column.getId() + "] (Data Set UUID [" + def.getUUID() + "]). Value core type not supported. Expecting string or number or date core field types."); }
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.// www . j a va2 s . c o 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; }