/** * Deserialize credentials from JSON. * * @param jsonObject credentials in JSON format. * @return deserialized credentials. */ static Credentials fromJson(final JsonObject jsonObject) { final String type = jsonObject.getValueOrThrow(JsonFields.TYPE); final Function<JsonObject, Credentials> deserializer = JsonFields.DESERIALIZER_MAP.get(type); if (deserializer == null) { throw JsonParseException.newBuilder() .message(String.format("Unknown credential type <%s>", type)) .description("Original JSON: " + jsonObject.toString()) .build(); } return deserializer.apply(jsonObject); }
/** * Deserialize credentials from JSON. * * @param jsonObject credentials in JSON format. * @return deserialized credentials. */ static Credentials fromJson(final JsonObject jsonObject) { final String type = jsonObject.getValueOrThrow(JsonFields.TYPE); final Function<JsonObject, Credentials> deserializer = JsonFields.DESERIALIZER_MAP.get(type); if (deserializer == null) { throw JsonParseException.newBuilder() .message(String.format("Unknown credential type <%s>", type)) .description("Original JSON: " + jsonObject.toString()) .build(); } return deserializer.apply(jsonObject); }
/** * Creates a new {@code Resource} object based on the given {@code resourceKey} and {@code jsonValue}. * * @param resourceKey the JSON key which is assumed to be the path of a Resource prefixed with a type. * @param jsonValue the JSON value containing the effected permissions for the Resource. This value is supposed to * be a {@link JsonObject}. * @return a new {@code Resource} object. * @throws NullPointerException if any argument is {@code null}. * @throws DittoJsonException if {@code jsonValue} is not a JSON object or the JSON has not the expected format. */ public static Resource of(final ResourceKey resourceKey, final JsonValue jsonValue) { checkNotNull(jsonValue, "JSON value"); final EffectedPermissions effectedPermissions = Optional.of(jsonValue) .filter(JsonValue::isObject) .map(JsonValue::asObject) .map(object -> wrapJsonRuntimeException(() -> ImmutableEffectedPermissions.fromJson(object))) .orElseThrow(() -> new DittoJsonException(JsonParseException.newBuilder() .message("The JSON object for the 'permissions' (grant/revoke) of the 'resource' '" + resourceKey + "' is missing or not an object.") .build())); return of(resourceKey, effectedPermissions); }
/** * Creates a JSON object from the given string. * * @param jsonString the string that represents the JSON object. * @return the JSON object that has been created from the string. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON object. */ public static JsonObject newObject(final String jsonString) { requireNonNull(jsonString, "The JSON string to create a JSON object from must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException("The JSON string to create a JSON object from must not be empty!"); } if (isJsonNullLiteralString(jsonString)) { return nullObject(); } else { final JsonValue jsonValue = JsonValueParser.fromString().apply(jsonString); if (!jsonValue.isObject()) { final String msgPattern = "<{0}> is not a valid JSON object!"; throw JsonParseException.newBuilder().message(MessageFormat.format(msgPattern, jsonString)).build(); } return jsonValue.asObject(); } }
/** * Creates a new JSON array from the given string. * * @param jsonString the string that represents the JSON array. * @return the JSON array that has been created from the string. * @throws NullPointerException if {@code jsonString} is {@code null}. * @throws IllegalArgumentException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} does not contain a valid JSON array. */ public static JsonArray newArray(final String jsonString) { requireNonNull(jsonString, "The JSON string to create JSON array from must not be null!"); if (jsonString.isEmpty()) { throw new IllegalArgumentException("The JSON string to create a JSON array from must not be empty!"); } if (isJsonNullLiteralString(jsonString)) { return nullArray(); } else { final JsonValue jsonValue = JsonValueParser.fromString().apply(jsonString); if (!jsonValue.isArray()) { final String msgPattern = "<{0}> is not a valid JSON array!"; throw JsonParseException.newBuilder().message(MessageFormat.format(msgPattern, jsonString)).build(); } return jsonValue.asArray(); } }
/** * Creates a new {@code ConnectionData} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the ConnectionData to be created. * @return a new ConnectionData 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. */ static ConnectionData fromJson(final JsonObject jsonObject) { final JsonObject readAmqpConnection = jsonObject.getValueOrThrow(AMQP_CONNECTION); final AmqpConnection amqpConnection = AmqpBridgeModelFactory.connectionFromJson(readAmqpConnection); final String readConnectionStatus = jsonObject.getValueOrThrow(CONNECTION_STATUS); final ConnectionStatus connectionStatus = ConnectionStatus.forName(readConnectionStatus).orElseThrow(() -> JsonParseException.newBuilder() .message("Could not create ConnectionStatus from: " + jsonObject) .build()); return new ConnectionData(amqpConnection, connectionStatus); }
private static <T> T tryToParseJsonValue(final String jsonString, final DittoJsonHandler<?, ?, T> dittoJsonHandler) { try { return parseJsonValue(jsonString, dittoJsonHandler); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse JSON string ''{0}''!", jsonString)) .cause(e) .build(); } }
private static ConnectionStatus getConnectionStatusOrThrow(final JsonObject jsonObject) { final String readConnectionStatus = jsonObject.getValueOrThrow(JsonFields.CONNECTION_STATUS); return ConnectionStatus.forName(readConnectionStatus) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection status <{0}> is invalid!", readConnectionStatus)) .build()); }
private static DittoRuntimeException buildError(final WithDittoHeaders withDittoHeaders) { final JsonParseException jsonException = JsonParseException.newBuilder() .message("JSON contains forbidden character '\\u0000'") .build(); return new DittoJsonException(jsonException).setDittoHeaders(withDittoHeaders.getDittoHeaders()); } }
private static ConnectionType getConnectionTypeOrThrow(final JsonObject jsonObject) { final String readConnectionType = jsonObject.getValueOrThrow(JsonFields.CONNECTION_TYPE); return ConnectionType.forName(readConnectionType) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection type <{0}> is invalid!", readConnectionType)) .build()); }
private static JsonValue tryToReadJsonValueFrom(final Reader reader) { try { return readJsonValueFrom(reader); } catch (final ParseException | IOException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to parse JSON value from reader!") .cause(e) .build(); } }
private static String[] splitTypeWithPath(final String typeWithPath) { final String[] split = typeWithPath.split(ResourceKey.KEY_DELIMITER, 2); if (split.length < 2) { throw new DittoJsonException(JsonParseException.newBuilder() .message("The provided string was not in the expected format 'type:path'") .build()); } return split; }
private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { final String msgPattern ="The JSON object's field <{0>' is not in ISO-8601 format as expected!"; throw JsonParseException.newBuilder() .message(MessageFormat.format(msgPattern, Thing.JsonFields.MODIFIED.getPointer())) .cause(e) .build(); } }
private static com.eclipsesource.json.JsonValue tryToRead(final Reader reader) { try { return Json.parse(reader); } catch (final ParseException | IOException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to parse JSON from reader!") .cause(e) .build(); } }
private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { final String msgPattern ="The JSON object's field <{0>' is not in ISO-8601 format as expected!"; throw JsonParseException.newBuilder() .message(MessageFormat.format(msgPattern, Thing.JsonFields.MODIFIED.getPointer())) .cause(e) .build(); } }
private static com.eclipsesource.json.JsonObject tryToReadJsonObjectFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asObject(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON object from string!") .cause(e) .build(); } }
private static ConnectionStatus getConnectionStatusOrThrow(final JsonObject jsonObject) { final String readConnectionStatus = jsonObject.getValueOrThrow(JsonFields.CONNECTION_STATUS); return ConnectionStatus.forName(readConnectionStatus) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection status <{0}> is invalid!", readConnectionStatus)) .build()); }
private static com.eclipsesource.json.JsonArray tryToReadMinimalJsonArrayFrom(final String jsonString) { try { final com.eclipsesource.json.JsonValue parsedJsonString = Json.parse(jsonString); return parsedJsonString.asArray(); } catch (final ParseException | UnsupportedOperationException | StackOverflowError e) { throw JsonParseException.newBuilder() .message("Failed to create JSON array from string!") .cause(e) .build(); } }
private static ConnectionType getConnectionTypeOrThrow(final JsonObject jsonObject) { final String readConnectionType = jsonObject.getValueOrThrow(JsonFields.CONNECTION_TYPE); return ConnectionType.forName(readConnectionType) .orElseThrow(() -> JsonParseException.newBuilder() .message(MessageFormat.format("Connection type <{0}> is invalid!", readConnectionType)) .build()); }
private static com.eclipsesource.json.JsonValue tryToRead(final String json) { try { return Json.parse(json); } catch (final ParseException | StackOverflowError e) { throw JsonParseException.newBuilder() .message(MessageFormat.format("Failed to parse ''{0}''!", json)) .cause(e) .build(); } }