List of usage examples for com.google.gson JsonPrimitive isNumber
public boolean isNumber()
From source file:com.flipkart.polyguice.config.JsonConfiguration.java
License:Apache License
private void flatten(String prefix, JsonElement element) { if (element.isJsonPrimitive()) { JsonPrimitive jsonPrim = element.getAsJsonPrimitive(); if (jsonPrim.isBoolean()) { configTab.put(prefix, jsonPrim.getAsBoolean()); } else if (jsonPrim.isNumber()) { configTab.put(prefix, jsonPrim.getAsNumber()); } else if (jsonPrim.isString()) { configTab.put(prefix, jsonPrim.getAsString()); }//from www. ja v a 2 s. com } else if (element.isJsonObject()) { JsonObject jsonObj = element.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObj.entrySet()) { String prefix1 = ((prefix != null) ? prefix + "." : "") + entry.getKey(); flatten(prefix1, entry.getValue()); } } }
From source file:com.frontier45.flume.sink.elasticsearch2.ElasticSearchLogStashEventSerializer.java
License:Apache License
private Object getValue(JsonElement obj) { JsonPrimitive jp = obj.getAsJsonPrimitive(); if (jp.isBoolean()) { return jp.getAsBoolean(); } else if (jp.isNumber()) { String value = jp.getAsString(); if (value.indexOf(".") > 0) { return jp.getAsDouble(); } else {/*from w w w.j a va 2s .c o m*/ return jp.getAsLong(); } } else if (jp.isString()) { return jp.getAsString(); } return obj; }
From source file:com.getperka.flatpack.codexes.CharacterCodex.java
License:Apache License
@Override public Character readNotNull(JsonElement element, DeserializationContext context) throws Exception { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); // Treat a number as a BMP code point if (primitive.isNumber()) { return (char) primitive.getAsInt(); } else {/*from w w w . jav a 2s. co m*/ return primitive.getAsCharacter(); } } throw new IllegalArgumentException("Cannot convert " + element.toString() + " to a char"); }
From source file:com.getperka.flatpack.codexes.DateCodex.java
License:Apache License
@Override public D readNotNull(JsonElement element, DeserializationContext context) throws Exception { if (element.isJsonPrimitive()) { long instant; JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isNumber()) { instant = primitive.getAsLong(); } else {//from ww w . j a v a 2 s . c om instant = ISODateTimeFormat.dateTimeParser().parseMillis(primitive.getAsString()); } return constructor.newInstance(instant); } throw new IllegalArgumentException("Could not parse " + element.toString() + " as a date value"); }
From source file:com.getperka.flatpack.codexes.DynamicCodex.java
License:Apache License
/** * Attempt to infer the type from the JsonElement presented. * <ul>/*w w w . j av a 2s . c o m*/ * <li>boolean -> {@link Boolean} * <li>number -> {@link BigDecimal} * <li>string -> {@link HasUuid} or {@link String} * <li>array -> {@link ListCodex} * <li>object -> {@link StringMapCodex} * </ul> */ @Override public Object readNotNull(JsonElement element, DeserializationContext context) throws Exception { if (element.isJsonPrimitive()) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { // Always return numbers as BigDecimals for consistency return primitive.getAsBigDecimal(); } else { String value = primitive.getAsString(); // Interpret UUIDs as entity references if (UUID_PATTERN.matcher(value).matches()) { UUID uuid = UUID.fromString(value); HasUuid entity = context.getEntity(uuid); if (entity != null) { return entity; } } return value; } } else if (element.isJsonArray()) { return listCodex.get().readNotNull(element, context); } else if (element.isJsonObject()) { return mapCodex.get().readNotNull(element, context); } context.fail(new UnsupportedOperationException("Cannot infer data type for " + element.toString())); return null; }
From source file:com.github.strawberry.util.Json.java
License:Open Source License
public static Object parsePrimitive(JsonElement e) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isString()) { return e.getAsString(); }/*from w w w . j a va2 s. co m*/ if (p.isBoolean()) { return e.getAsBoolean(); } if (p.isNumber()) { return e.getAsInt(); } return p.getAsString(); }
From source file:com.github.zhizheng.json.JsonValueTypes.java
License:Apache License
/** * Json /*from w ww .j a v a 2 s . co m*/ * * @param jsonElement * @return */ public static String getJsonValueType(JsonElement jsonElement) { if (jsonElement.isJsonObject()) { return OBJECT.toString(); } if (jsonElement.isJsonArray()) { return ARRAY.toString(); } if (jsonElement.isJsonPrimitive()) { JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive(); if (asJsonPrimitive.isBoolean()) { return BOOLEAN.toString(); } if (asJsonPrimitive.isNumber()) { return NUMBER.toString(); } return STRING.toString(); } return NULL.toString(); }
From source file:com.google.iosched.model.validator.IntegerConverter.java
License:Open Source License
@Override public JsonPrimitive convert(JsonPrimitive value) { if (value == null) { return new JsonPrimitive(0); }/*from w w w.ja v a 2 s. co m*/ if (value.isNumber()) { return value; } String str = value.getAsString(); try { return new JsonPrimitive(Integer.parseInt(str)); } catch (NumberFormatException ex) { throw new ConverterException(value, this); } }
From source file:com.google.iosched.model.validator.IntegerToStringConverter.java
License:Open Source License
@Override public JsonPrimitive convert(JsonPrimitive value) { if (value == null) { return null; }//from w w w . j a v a 2 s .com if (value.isNumber()) { return new JsonPrimitive(String.valueOf(value.getAsInt())); } try { Integer.parseInt(value.getAsString()); // if it parses correctly, it is validated: return value; } catch (NumberFormatException ex) { throw new ConverterException(value, this); } }
From source file:com.hbm.devices.jet.JetPeer.java
License:Open Source License
private JsonPrimitive getFetchId(JsonObject object) { JsonPrimitive method = object.getAsJsonPrimitive("method"); if ((method != null) && (method.isNumber())) { return method; }//from w w w.jav a 2s . c o m return null; }