Java Code Examples for com.google.gson.JsonPrimitive#getAsNumber()
The following examples show how to use
com.google.gson.JsonPrimitive#getAsNumber() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GsonScalars.java From graphql-spqr with Apache License 2.0 | 6 votes |
@Override public Object serialize(Object dataFetcherResult) { if (dataFetcherResult instanceof JsonPrimitive) { JsonPrimitive primitive = (JsonPrimitive) dataFetcherResult; if (primitive.isString()) { return primitive.getAsString(); } if (primitive.isNumber()) { return primitive.getAsNumber(); } if (primitive.isBoolean()) { return primitive.getAsBoolean(); } if (primitive.isJsonNull()) { return null; } } throw serializationException(dataFetcherResult, JsonPrimitive.class); }
Example 2
Source File: JsonAttributeValueSerialization.java From XACML with MIT License | 6 votes |
private static StdAttributeValue<?> parsePrimitiveAttribute(JsonPrimitive jsonPrimitive) { try { if (jsonPrimitive.isString()) { return new StdAttributeValue<>(XACML3.ID_DATATYPE_STRING, jsonPrimitive.getAsString()); } else if (jsonPrimitive.isBoolean()) { return new StdAttributeValue<>(XACML3.ID_DATATYPE_BOOLEAN, jsonPrimitive.getAsBoolean()); } else if (jsonPrimitive.isNumber()) { Number number = jsonPrimitive.getAsNumber(); logger.debug("Number is {} {} ceil {}", number.doubleValue(), number.longValue(), Math.ceil(number.doubleValue())); if (Math.ceil(number.doubleValue()) == number.longValue()) { return new StdAttributeValue<>(XACML3.ID_DATATYPE_INTEGER, DataTypeInteger.newInstance().convert(jsonPrimitive.getAsInt())); } else { return new StdAttributeValue<>(XACML3.ID_DATATYPE_DOUBLE, jsonPrimitive.getAsDouble()); } } } catch (DataTypeException e) { logger.error("Parse primitive failed", e); } return null; }
Example 3
Source File: ValueEncoder.java From dolphin-platform with Apache License 2.0 | 6 votes |
public static Object decodeValue(final JsonElement jsonElement) { if (jsonElement == null || jsonElement.isJsonNull()) { return null; } if (! jsonElement.isJsonPrimitive()) { throw new JsonParseException("Illegal JSON detected"); } final JsonPrimitive value = (JsonPrimitive) jsonElement; if (value.isString()) { return value.getAsString(); } else if (value.isBoolean()) { return value.getAsBoolean(); } else if (value.isNumber()) { return value.getAsNumber(); } throw new JsonParseException("Currently only String, Boolean, or Number are allowed as primitives"); }
Example 4
Source File: JsonUtils.java From kurento-java with Apache License 2.0 | 6 votes |
public Object toPrimitiveObject(JsonElement element) { JsonPrimitive primitive = (JsonPrimitive) element; if (primitive.isBoolean()) { return Boolean.valueOf(primitive.getAsBoolean()); } else if (primitive.isNumber()) { Number number = primitive.getAsNumber(); double value = number.doubleValue(); if ((int) value == value) { return Integer.valueOf((int) value); } else if ((long) value == value) { return Long.valueOf((long) value); } else if ((float) value == value) { return Float.valueOf((float) value); } else { return Double.valueOf((double) value); } } else if (primitive.isString()) { return primitive.getAsString(); } else { throw new JsonRpcException("Unrecognized JsonPrimitive: " + primitive); } }
Example 5
Source File: BasicJsonUtils.java From kurento-java with Apache License 2.0 | 6 votes |
private static Object convertValue(JsonElement value) { if (value.isJsonNull()) { return null; } else if (value.isJsonPrimitive()) { JsonPrimitive prim = value.getAsJsonPrimitive(); if (prim.isBoolean()) { return prim.getAsBoolean(); } else if (prim.isNumber()) { Number n = prim.getAsNumber(); if (n.doubleValue() == n.intValue()) { return n.intValue(); } else { return n.doubleValue(); } } else if (prim.isString()) { return prim.getAsString(); } else { throw new RuntimeException("Unrecognized value: " + value); } } else { return value.toString(); } }
Example 6
Source File: ParameterAdapter.java From activitystreams with Apache License 2.0 | 6 votes |
private Object deserialize( JsonDeserializationContext context, JsonElement el) { if (el.isJsonArray()) { return context.deserialize(el, Iterable.class); } else if (el.isJsonObject()) { return context.deserialize(el, ASObject.class); } else if (el.isJsonPrimitive()) { JsonPrimitive p = el.getAsJsonPrimitive(); if (p.isBoolean()) return p.getAsBoolean(); else if (p.isNumber()) return p.getAsNumber(); else return p.getAsString(); } else return null; }
Example 7
Source File: EventDeserializer.java From easypost-java with MIT License | 5 votes |
private Object deserializeJsonPrimitive(JsonPrimitive element) { if (element.isBoolean()) { return element.getAsBoolean(); } else if (element.isNumber()) { return element.getAsNumber(); } else { return element.getAsString(); } }
Example 8
Source File: JsonUtil.java From olca-app with Mozilla Public License 2.0 | 5 votes |
private static JsonPrimitive deepCopy(JsonPrimitive element) { if (element.isBoolean()) return new JsonPrimitive(element.getAsBoolean()); if (element.isNumber()) return new JsonPrimitive(element.getAsNumber()); return new JsonPrimitive(element.getAsString()); }
Example 9
Source File: SpecUtils.java From trimou with Apache License 2.0 | 5 votes |
private static Object getJsonPrimitiveElementValue(JsonElement element) { JsonPrimitive primitive = element.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isString()) { return primitive.getAsString(); } else if (primitive.isNumber()) { return primitive.getAsNumber(); } else { throw new IllegalStateException("Unsupported primitive type"); } }
Example 10
Source File: JsonElementResolver.java From trimou with Apache License 2.0 | 5 votes |
private Object unwrapJsonPrimitive(JsonPrimitive jsonPrimitive) { if (jsonPrimitive.isBoolean()) { return jsonPrimitive.getAsBoolean(); } else if (jsonPrimitive.isString()) { return jsonPrimitive.getAsString(); } else if (jsonPrimitive.isNumber()) { return jsonPrimitive.getAsNumber(); } return jsonPrimitive; }
Example 11
Source File: LuaJsonElement.java From Lukkit with MIT License | 5 votes |
private LuaValue getValueFromElement(JsonElement element) { if (element.isJsonArray() || element.isJsonObject()) { return this.getTableFromElement(element); } else if (element.isJsonNull()) { return LuaValue.NIL; } else if (element.isJsonPrimitive()) { JsonPrimitive primitiveValue = element.getAsJsonPrimitive(); if (primitiveValue.isBoolean()) { return LuaValue.valueOf(primitiveValue.getAsBoolean()); } else if (primitiveValue.isString()) { return LuaValue.valueOf(primitiveValue.getAsString()); } else if (primitiveValue.isNumber()) { Number numberValue = primitiveValue.getAsNumber(); if (numberValue instanceof Double) return LuaValue.valueOf(numberValue.doubleValue()); else if (numberValue instanceof Integer) return LuaValue.valueOf(numberValue.intValue()); else if (numberValue instanceof Short) return LuaValue.valueOf(numberValue.shortValue()); else if (numberValue instanceof Long) return LuaValue.valueOf(numberValue.longValue()); else if (numberValue instanceof Float) return LuaValue.valueOf(numberValue.floatValue()); else if (numberValue instanceof Byte) return LuaValue.valueOf(numberValue.byteValue()); } } else { LuaError error = new LuaError("A LuaJsonElement object was passed an unsupported value other than that supported by LuaJ. Value: " + element.toString()); LuaEnvironment.addError(error); error.printStackTrace(); } return LuaValue.NIL; }
Example 12
Source File: ASObjectAdapter.java From activitystreams with Apache License 2.0 | 5 votes |
@Override protected Object doForward(JsonPrimitive b) { if (b.isBoolean()) return b.getAsBoolean(); else if (b.isNumber()) return b.getAsNumber(); else return b.getAsString(); }
Example 13
Source File: SPLGenerator.java From streamsx.topology with Apache License 2.0 | 5 votes |
/** * Append the value with the correct SPL suffix. Integer & Double do not * require a suffix */ static void numberLiteral(StringBuilder sb, JsonPrimitive value, String type) { String suffix = ""; switch (type) { case "INT8": suffix = "b"; break; case "INT16": suffix = "h"; break; case "INT32": break; case "INT64": suffix = "l"; break; case "UINT8": suffix = "ub"; break; case "UINT16": suffix = "uh"; break; case "UINT32": suffix = "uw"; break; case "UINT64": suffix = "ul"; break; case "FLOAT32": suffix = "w"; break; // word, meaning 32 bits case "FLOAT64": break; } String literal; if (value.isNumber() && isUnsignedInt(type)) { Number nv = value.getAsNumber(); if ("UINT64".equals(type)) literal = Long.toUnsignedString(nv.longValue()); else if ("UINT32".equals(type)) literal = Integer.toUnsignedString(nv.intValue()); else if ("UINT16".equals(type)) literal = Integer.toUnsignedString(Short.toUnsignedInt(nv.shortValue())); else literal = Integer.toUnsignedString(Byte.toUnsignedInt(nv.byteValue())); } else { literal = value.getAsNumber().toString(); } sb.append(literal); sb.append(suffix); }
Example 14
Source File: JsonConverter.java From external-resources with Apache License 2.0 | 5 votes |
private Resource get(JsonPrimitive primitive) { if (primitive.isBoolean()) { return new Resource(primitive.getAsBoolean()); } else if (primitive.isNumber()) { return new Resource(primitive.getAsNumber()); } else { return new Resource(primitive.getAsString()); } }
Example 15
Source File: CommonSupplementReader.java From moql with Apache License 2.0 | 5 votes |
protected Object getValue(JsonPrimitive value) { if (value.isNumber()) { Number number = value.getAsNumber(); double d = number.doubleValue(); long l = number.longValue(); if (d - l > 0) { return d; } else { return l; } } else if (value.isBoolean()) return value.getAsBoolean(); else return value.getAsString(); }
Example 16
Source File: ModuleRegistry.java From Wizardry with GNU Lesser General Public License v3.0 | 5 votes |
private Object getJsonValue(String key, JsonPrimitive elem) { // TODO: Move to utils if (elem.isString()) return elem.getAsString(); else if (elem.isNumber()) return elem.getAsNumber(); else if (elem.isBoolean()) return elem.getAsBoolean(); // ... TODO: Add more data types. String value = elem.getAsString(); Wizardry.LOGGER.warn("| | |_ WARNING! Using fallback as string for parameter '" + key + "' having value '" + value + "'."); return value; }
Example 17
Source File: BackupManager.java From revolution-irc with GNU General Public License v3.0 | 5 votes |
@SuppressLint("ApplySharedPref") private static void importPreferencesFromJson(Context context, Reader reader) { SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(context).edit(); SettingsHelper.getInstance(context).clear(); JsonObject obj = SettingsHelper.getGson().fromJson(reader, JsonObject.class); for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { JsonElement el = entry.getValue(); if (el.isJsonArray()) { Set<String> items = new HashSet<>(); for (JsonElement child : el.getAsJsonArray()) items.add(child.getAsString()); prefs.putStringSet(entry.getKey(), items); } else { JsonPrimitive primitive = el.getAsJsonPrimitive(); if (primitive.isBoolean()) { prefs.putBoolean(entry.getKey(), primitive.getAsBoolean()); } else if (primitive.isNumber()) { Number number = primitive.getAsNumber(); if (number instanceof Float || number instanceof Double) prefs.putFloat(entry.getKey(), number.floatValue()); else if (number instanceof Long) prefs.putLong(entry.getKey(), number.longValue()); else prefs.putInt(entry.getKey(), number.intValue()); } else if (primitive.isString()) { prefs.putString(entry.getKey(), primitive.getAsString()); } } } prefs.commit(); // This will be called asynchronously }
Example 18
Source File: AbstractGsonConverter.java From helper with MIT License | 5 votes |
@Override public Object unwarpPrimitive(JsonPrimitive primitive) { if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { return primitive.getAsNumber(); } else if (primitive.isString()) { return primitive.getAsString(); } else { throw new IllegalArgumentException("Unknown primitive type: " + primitive); } }
Example 19
Source File: JsonParser.java From org.hl7.fhir.core with Apache License 2.0 | 4 votes |
private void parseChildPrimitiveInstance(Element element, Property property, String name, String npath, JsonElement main, JsonElement fork) throws FHIRException { if (main != null && !(main instanceof JsonPrimitive)) logError(line(main), col(main), npath, IssueType.INVALID, context.formatMessage( I18nConstants.THIS_PROPERTY_MUST_BE_AN_SIMPLE_VALUE_NOT_, describe(main)), IssueSeverity.ERROR); else if (fork != null && !(fork instanceof JsonObject)) logError(line(fork), col(fork), npath, IssueType.INVALID, context.formatMessage(I18nConstants.THIS_PROPERTY_MUST_BE_AN_OBJECT_NOT_, describe(fork)), IssueSeverity.ERROR); else { Element n = new Element(name, property).markLocation(line(main != null ? main : fork), col(main != null ? main : fork)); element.getChildren().add(n); if (main != null) { JsonPrimitive p = (JsonPrimitive) main; if (p.isNumber() && p.getAsNumber() instanceof JsonTrackingParser.PresentedBigDecimal) { String rawValue = ((JsonTrackingParser.PresentedBigDecimal) p.getAsNumber()).getPresentation(); n.setValue(rawValue); } else { n.setValue(p.getAsString()); } if (!n.getProperty().isChoice() && n.getType().equals("xhtml")) { try { n.setXhtml(new XhtmlParser().setValidatorMode(policy == ValidationPolicy.EVERYTHING).parse(n.getValue(), null).getDocumentElement()); } catch (Exception e) { logError(line(main), col(main), npath, IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_XHTML_, e.getMessage()), IssueSeverity.ERROR); } } if (policy == ValidationPolicy.EVERYTHING) { // now we cross-check the primitive format against the stated type if (Utilities.existsInList(n.getType(), "boolean")) { if (!p.isBoolean()) logError(line(main), col(main), npath, IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_THE_PRIMITIVE_VALUE_MUST_BE_A_BOOLEAN), IssueSeverity.ERROR); } else if (Utilities.existsInList(n.getType(), "integer", "unsignedInt", "positiveInt", "decimal")) { if (!p.isNumber()) logError(line(main), col(main), npath, IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_THE_PRIMITIVE_VALUE_MUST_BE_A_NUMBER), IssueSeverity.ERROR); } else if (!p.isString()) logError(line(main), col(main), npath, IssueType.INVALID, context.formatMessage(I18nConstants.ERROR_PARSING_JSON_THE_PRIMITIVE_VALUE_MUST_BE_A_STRING), IssueSeverity.ERROR); } } if (fork != null) { JsonObject child = (JsonObject) fork; checkObject(child, npath); parseChildren(npath, child, n, false); } } }