private static Object toJavaObject(final JsonValue jsonValue) { final Object result; if (null == jsonValue) { result = null; } else if (jsonValue.isNull()) { result = null; } else if (jsonValue.isString()) { result = jsonValue.asString(); } else if (jsonValue.isBoolean()) { result = jsonValue.asBoolean(); } else if (jsonValue.isNumber()) { final Double doubleValue = jsonValue.asDouble(); if (doubleValue.intValue() == doubleValue) { result = doubleValue.intValue(); } else if (doubleValue.longValue() == doubleValue) { result = doubleValue.longValue(); } else { result = doubleValue; } } else if (jsonValue.isObject()) { result = toJavaMap(jsonValue.asObject()); } else if (jsonValue.isArray()) { result = toJavaList(jsonValue.asArray()); } else { throw new IllegalStateException( MessageFormat.format("Failed to convert {0} to a Java object", jsonValue)); } return result; }
if (v.isNull()) { return null; } else if (v.isNumber()) { try { return v.asInt(); } catch (NumberFormatException ex1) { try { return v.asLong(); } catch (NumberFormatException ex2) { return v.asDouble(); } else if (v.isBoolean()) { return v.asBoolean() ? 1 : 0; } else if (v.isString()) { return v.asString(); } else if (v.isArray()) { JsonArray a = v.asArray(); return a.stream().map(w -> getJavaValue(w)).toArray(); } else if (v.isObject()) { JsonObject o = v.asObject(); Map<String, Object> m = new HashMap<>(); o.forEach(e -> m.put(e.getKeyName(), getJavaValue(e.getValue()))); } else { return v.toString();
@Override public ImmutableJsonArray add(final int value, final int... furtherValues) { checkFurtherValues(furtherValues); return add(JsonValue.of(value), Arrays.stream(furtherValues) .mapToObj(JsonValue::of) .toArray(JsonValue[]::new)); }
private static Object mapJsonValueToJava(final JsonValue jsonValue) { final Object result; if (jsonValue.isString()) { result = jsonValue.asString(); } else if (jsonValue.isBoolean()) { result = jsonValue.asBoolean(); } else if (jsonValue.isNull()) { result = null; } else if (jsonValue.isNumber()) { if (jsonValue.isLong()) { result = jsonValue.asLong(); } else { result = jsonValue.asDouble(); } } else if (jsonValue.isArray()) { result = null; // filtering arrays is not supported } else if (jsonValue.isObject()) { result = null; // filtering objects is not supported } else { result = null; } return result; }
private static boolean isJsonValueIllegal(final JsonValue entity) { final boolean result; if (entity.isArray()) { result = entity.asArray().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonValueIllegal); } else if (entity.isObject()) { result = entity.asObject().stream().anyMatch(CommandWithOptionalEntityValidator::isJsonFieldIllegal); } else if (entity.isString()) { result = isStringIllegal(entity.asString()); } else { result = false; } return result; }
} else if (jsonValue.isNull()) { result = Json.NULL; } else if (jsonValue.isString()) { result = Json.value(jsonValue.asString()); } else if (jsonValue.isBoolean()) { result = Json.value(jsonValue.asBoolean()); } else if (jsonValue.isNumber()) { final Double doubleValue = jsonValue.asDouble(); if (doubleValue.intValue() == doubleValue) { result = Json.value(doubleValue.intValue()); result = Json.value(doubleValue); } else if (jsonValue.isObject()) { result = Json.parse(jsonValue.toString()); } else if (jsonValue.isArray()) { result = Json.parse(jsonValue.toString()); } else { throw new IllegalStateException(
private JsonObject lazyLoadThingJsonObject() { if (thing == null) { thing = JsonFactory.readFrom(thingPlainJson).asObject(); } return thing; }
private static Collection<ResourcePermissions> createEntriesForAttribute(final JsonPointer attributePointer, final JsonValue attributeValue, final Enforcer policyEnforcer) { final Collection<ResourcePermissions> result = new HashSet<>(3); if (attributeValue.isObject() && !attributeValue.isNull()) { final JsonObject jsonObject = attributeValue.asObject(); // Recursion! jsonObject.forEach( subField -> result.addAll(createEntriesForAttribute(attributePointer.addLeaf(subField.getKey()), subField.getValue(), policyEnforcer))); } else { result.add(AttributeResourcePermissions.getInstance(attributePointer, attributeValue, policyEnforcer)); } return result; }
@Nullable private JsonValue getViewForJsonValueOrNull(final JsonValue jsonValue, final Set<String> subjectIds, final Permissions permissions) { final JsonValue result; if (jsonValue.isObject()) { result = getViewForJsonObjectOrNull(jsonValue.asObject(), subjectIds, permissions); } else if (jsonValue.isArray()) { result = getViewForJsonArrayOrNull(jsonValue.asArray(), subjectIds, permissions); } else if (grantRevokeIndex.hasPermissions(subjectIds, permissions)) { result = jsonValue; } else { result = null; } return result; }
@Override public boolean contains(final CharSequence key) { requireNonNull(key, "The key or pointer to check the existence of a value for must not be null!"); final boolean result; final JsonPointer pointer = JsonFactory.newPointer(key); if (1 >= pointer.getLevelCount()) { result = pointer.getRoot().map(this::containsKey).orElse(false); } else { result = pointer.getRoot() .flatMap(this::getValueForKey) .map(jsonValue -> !jsonValue.isObject() || jsonValue.asObject().contains(pointer.nextLevel())) // Recursion .orElse(false); } return result; }
private static JsonValue mergeJsonValues(final JsonValue value1, final JsonValue value2) { final JsonValue result; if (areJsonObjects(value1, value2)) { result = mergeJsonObjects(value1.asObject(), value2.asObject()); } else if (areJsonArrays(value1, value2)) { result = mergeJsonArrays(value1.asArray(), value2.asArray()); } else { result = value1; } return result; }
/** * Returns a JSON string representation of this object. The content of the result is determined by the given * pointer. * * @param pointer determines the field to be included in the JSON representation. * @return a JSON string representation of this object. * @throws NullPointerException if {@code pointer} is {@code null}. * @see #toJson(JsonPointer) */ default String toJsonString(final JsonPointer pointer) { return toJson(pointer).toString(); }
private static JsonObject createPolicyJsonObjectForPut(final String jsonString, final String policyId) { final JsonObject policyJsonObject = wrapJsonRuntimeException(() -> JsonFactory.newObject(jsonString)); policyJsonObject.getValue(Policy.JsonFields.ID.getPointer()) .ifPresent(policyIdJsonValue -> { if (!policyIdJsonValue.isString() || !policyId.equals(policyIdJsonValue.asString())) { throw PolicyIdNotExplicitlySettableException.newBuilder().build(); } }); return policyJsonObject.setValue(Policy.JsonFields.ID.getPointer(), JsonValue.of(policyId)); }
private JsonArray lazyLoadThingsJsonArray() { if (things == null) { things = JsonFactory.readFrom(thingsPlainJson).asArray(); } return things; }
/** * Creates a new {@code HeaderMapping} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the HeaderMapping to be created. * @return a new HeaderMapping which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if {@code jsonObject} is not an appropriate JSON object. */ public static HeaderMapping fromJson(final JsonObject jsonObject) { return new ImmutableHeaderMapping(jsonObject.stream() .filter(f -> f.getValue().isString()) .collect(Collectors.toMap(JsonField::getKeyName, jsonField -> jsonField.getValue().asString()))); }
private static HttpResponse addEntityAccordingToContentType(final HttpResponse response, final JsonValue entity, final DittoHeaders dittoHeaders) { if (hasPlainTextContentType(dittoHeaders)) { return response.withEntity(CONTENT_TYPE_TEXT, ByteString.fromString(entity.asString())); } else { return response.withEntity(CONTENT_TYPE_JSON, ByteString.fromString(entity.toString())); } }