/** * Creates a new {@code SudoRetrieveModifiedThingTags} from a JSON object. * * @param jsonObject the JSON object of which a new SudoRetrieveModifiedThingTags is to be created. * @param dittoHeaders the optional command headers of the request. * @return the SudoRetrieveModifiedThingTags which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws JsonMissingFieldException if the passed in {@code jsonObject} was not in the expected format. */ public static SudoStreamModifiedEntities fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { try { final Instant start = Instant.parse(jsonObject.getValueOrThrow(JSON_START)); final Instant end = Instant.parse(jsonObject.getValueOrThrow(JSON_END)); final Integer burst = jsonObject.getValue(JSON_BURST).orElse(null); final Long timeoutMillis = jsonObject.getValue(JSON_TIMEOUT_MILLIS).orElse(null); return SudoStreamModifiedEntities.of(start, end, burst, timeoutMillis, dittoHeaders); } catch (final DateTimeParseException e) { throw JsonParseException.newBuilder() .message("A given instant is not a valid timestamp.") .description(jsonObject.toString()) .cause(e) .build(); } }
/** * 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); }
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 T fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final String type = resolveType(jsonObject); final JsonParsable<T> jsonObjectParsable = parseStrategies.get(type); if (null != jsonObjectParsable) { try { return jsonObjectParsable.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException jre) { final JsonExceptionBuilder builder = JsonRuntimeException.newBuilder(jre.getErrorCode()) .message("Error when parsing Json type '" + type + "': " + jre.getMessage()) .cause(jre.getCause()); jre.getDescription().ifPresent(builder::description); jre.getHref().ifPresent(builder::href); // rethrow after enhancing the exception with the type which failed to parse throw (JsonRuntimeException) builder.build(); } } else { throw JsonTypeNotParsableException.newBuilder(type, getClass().getSimpleName()) .dittoHeaders(dittoHeaders) .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 T fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { final String type = resolveType(jsonObject); final JsonParsable<T> jsonObjectParsable = parseStrategies.get(type); if (null != jsonObjectParsable) { try { return jsonObjectParsable.parse(jsonObject, dittoHeaders); } catch (final JsonRuntimeException jre) { final JsonExceptionBuilder builder = JsonRuntimeException.newBuilder(jre.getErrorCode()) .message("Error when parsing Json type '" + type + "': " + jre.getMessage()) .cause(jre.getCause()); jre.getDescription().ifPresent(builder::description); jre.getHref().ifPresent(builder::href); // rethrow after enhancing the exception with the type which failed to parse throw (JsonRuntimeException) builder.build(); } } else { throw JsonTypeNotParsableException.newBuilder(type, getClass().getSimpleName()) .dittoHeaders(dittoHeaders) .build(); } }
/** * 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); }
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 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 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 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 <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 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()); }
/** * 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); }
JsonParseException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders)); JsonMissingFieldException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders)); JsonFieldSelectorInvalidException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders)); JsonPointerInvalidException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));
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(); } }
JsonParseException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders)); JsonMissingFieldException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders)); JsonFieldSelectorInvalidException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders)); JsonPointerInvalidException.newBuilder().message(getMessage(jsonObject)).build(), dittoHeaders));