private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { throw new JsonParseException("The JSON object's field '_modified' is not in ISO-8601 format as expected"); } }
@Override protected JsonParseException doBuild(final String errorCode, @Nullable final String message, @Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) { return new JsonParseException(message, description, cause, href); }
@Override protected JsonParseException doBuild(final String errorCode, @Nullable final String message, @Nullable final String description, @Nullable final Throwable cause, @Nullable final URI href) { return new JsonParseException(message, description, cause, href); }
private void validateCommandResponseType() { final String actualCommandResponseType = jsonObject.getValueOrThrow(CommandResponse.JsonFields.TYPE); if (!expectedCommandResponseType.equals(actualCommandResponseType)) { final String msgPattern = "Command Response JSON was not a <{0}> command response but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedCommandResponseType, actualCommandResponseType); throw new DittoJsonException(new JsonParseException(msg)); } }
private void validateCommandResponseType() { final String actualCommandResponseType = jsonObject.getValueOrThrow(CommandResponse.JsonFields.TYPE); if (!expectedCommandResponseType.equals(actualCommandResponseType)) { final String msgPattern = "Command Response JSON was not a <{0}> command response but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedCommandResponseType, actualCommandResponseType); throw new DittoJsonException(new JsonParseException(msg)); } }
private void checkValueType(final JsonValue jsonValue) { requireNonNull(jsonValue, "The JsonValue to be mapped must not be (Java) null!"); if (!hasExpectedValueType(jsonValue)) { final String msgPattern = "Value <{0}> for <{1}> is not of type <{2}>!"; final String message = MessageFormat.format(msgPattern, jsonValue, pointer, valueType.getSimpleName()); throw new JsonParseException(message); } }
private static Instant tryToParseModified(final CharSequence dateTime) { try { return Instant.parse(dateTime); } catch (final DateTimeParseException e) { throw new JsonParseException("The JSON object's field '" + JsonFields.MODIFIED.getPointer() + "' " + "is not in ISO-8601 format as expected"); } }
private void checkValueType(final JsonValue jsonValue) { requireNonNull(jsonValue, "The JsonValue to be mapped must not be (Java) null!"); if (!hasExpectedValueType(jsonValue)) { final String msgPattern = "Value <{0}> for <{1}> is not of type <{2}>!"; final String message = MessageFormat.format(msgPattern, jsonValue, pointer, valueType.getSimpleName()); throw new JsonParseException(message); } }
/** * Reads the given string and creates a JSON value based on the read data. The actual type of this JSON value is * unknown but can be obtained by invoking the {@code is...} methods. * * @param jsonString the JSON document to read. * @return a JSON value representing the read document. This value can be a JSON literal, a JSON object and so on. * @throws NullPointerException if {@code jsonString} is empty. * @throws JsonParseException if {@code jsonString} is empty or if it is no valid JSON. */ public static JsonValue readFrom(final String jsonString) { requireNonNull(jsonString, "The JSON string to read from must not be null!"); if (jsonString.isEmpty()) { throw new JsonParseException("The JSON string to read from must not be empty!"); } return JsonValueParser.fromString().apply(jsonString); }
private static List<String> thingsIdsFrom(final Adaptable adaptable) { final JsonArray array = adaptable.getPayload() .getValue() .filter(JsonValue::isObject) .map(JsonValue::asObject) .orElseThrow(() -> new JsonParseException("Adaptable payload was non existing or no JsonObject")) .getValue(RetrieveThings.JSON_THING_IDS) .filter(JsonValue::isArray) .map(JsonValue::asArray) .orElseThrow(() -> new JsonParseException("Could not map 'thingIds' value to expected JsonArray")); return array.stream() .map(JsonValue::asString) .collect(Collectors.toList()); }
private static List<String> thingsIdsFrom(final Adaptable adaptable) { final JsonArray array = adaptable.getPayload() .getValue() .filter(JsonValue::isObject) .map(JsonValue::asObject) .orElseThrow(() -> new JsonParseException("Adaptable payload was non existing or no JsonObject")) .getValue(RetrieveThings.JSON_THING_IDS) .filter(JsonValue::isArray) .map(JsonValue::asArray) .orElseThrow(() -> new JsonParseException("Could not map 'thingIds' value to expected JsonArray")); return array.stream() .map(JsonValue::asString) .collect(Collectors.toList()); }
private void validateCommandType() { final JsonFieldDefinition<String> typeFieldDefinition = Command.JsonFields.TYPE; final Optional<String> actualCommandTypeOptional = jsonObject.getValue(typeFieldDefinition); final String actualCommandType = actualCommandTypeOptional.orElseGet(() -> { // "type" was introduced in V2, if not present, take "command" instead. final String commandTypePrefix = expectedCommandType.split(":")[0]; return jsonObject.getValue(Command.JsonFields.ID) .map(id -> commandTypePrefix + ':' + id) .orElseThrow(() -> new JsonMissingFieldException(typeFieldDefinition)); }); if (!expectedCommandType.equals(actualCommandType)) { final String msgPattern = "Command JSON was not a <{0}> command but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedCommandType, actualCommandType); throw new DittoJsonException(new JsonParseException(msg)); } }
private void validateCommandType() { final JsonFieldDefinition<String> typeFieldDefinition = Command.JsonFields.TYPE; final Optional<String> actualCommandTypeOptional = jsonObject.getValue(typeFieldDefinition); final String actualCommandType = actualCommandTypeOptional.orElseGet(() -> { // "type" was introduced in V2, if not present, take "command" instead. final String commandTypePrefix = expectedCommandType.split(":")[0]; return jsonObject.getValue(Command.JsonFields.ID) .map(id -> commandTypePrefix + ':' + id) .orElseThrow(() -> new JsonMissingFieldException(typeFieldDefinition)); }); if (!expectedCommandType.equals(actualCommandType)) { final String msgPattern = "Command JSON was not a <{0}> command but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedCommandType, actualCommandType); throw new DittoJsonException(new JsonParseException(msg)); } }
private void validateEventType() { final String type = jsonObject.getValue(Event.JsonFields.TYPE) .orElseGet(() -> // if type was not present (was included in V2) // take event instead and transform to V2 format, fail if "event" is not present, too extractEventTypeV1() .orElseThrow(() -> new JsonMissingFieldException(Event.JsonFields.TYPE.getPointer())) ); if (!expectedType.equals(type)) { final String msgPattern = "Event JSON was not a <{0}> event but a <{1}>!"; final String msg = MessageFormat.format(msgPattern, expectedType, type); throw new DittoJsonException(new JsonParseException(msg)); } }
/** * Reads the given string and creates a JSON value based on the read data. The actual type of this JSON value is * unknown but can be obtained by invoking the {@code is...} methods. * * @param json the JSON document to read. * @return a JSON value representing the read document. This value can be a JSON literal, a JSON object and so on. * @throws NullPointerException if {@code json} is empty. * @throws JsonParseException if {@code json} is empty or if it is no valid JSON. */ public static JsonValue readFrom(final String json) { requireNonNull(json, "The JSON to read from must not be null!"); if (json.isEmpty()) { throw new JsonParseException("The JSON to read from must not be empty!"); } final com.eclipsesource.json.JsonValue minimalJsonValue = tryToRead(json); return convert(minimalJsonValue); }
/** * Creates a new {@code LoggerConfig} object from the specified JSON object. * * @param jsonObject a JSON object which provides the data for the LoggerConfig to be created. * @return a new LoggerConfig which is initialised with the extracted data from {@code jsonObject}. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws JsonMissingFieldException if {@code jsonObject} does not contain all required fields. * @throws JsonParseException if {@code jsonObject} contains invalid JSON. */ public static LoggerConfig fromJson(final JsonObject jsonObject) { final LogLevel level = LogLevel.forIdentifier(jsonObject.getValueOrThrow(JsonFields.LEVEL)) .orElseThrow(() -> new JsonParseException("Invalid LogLevel!")); final Optional<String> logger = jsonObject.getValue(JsonFields.LOGGER); return logger.map(s -> of(level, s)).orElseGet(() -> of(level)); }
/** * Partly deserializes the JSON which was given to this object's constructor. The factory method function which is * given to this method is responsible for creating the actual {@code CommandResponseType}. This method receives the * partly deserialized values which can be completed by implementors if further values are required. * * @param factoryMethodFunction creates the actual {@code CommandResponseType} object. * @return the command response. * @throws NullPointerException if {@code factoryMethodFunction} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the JSON is invalid or if the command response type differs from the expected one. */ public T deserialize(final FactoryMethodFunction<T> factoryMethodFunction) { checkNotNull(factoryMethodFunction, "method for creating a command response object"); validateCommandResponseType(); final int statusCode = jsonObject.getValueOrThrow(CommandResponse.JsonFields.STATUS); final HttpStatusCode httpStatusCode = HttpStatusCode.forInt(statusCode).orElseThrow(() -> { final String msgPattern = "HTTP status code <{0}> of JSON Object is not supported!"; return new JsonParseException(MessageFormat.format(msgPattern, statusCode)); }); return factoryMethodFunction.create(httpStatusCode); }
/** * Partly deserializes the JSON which was given to this object's constructor. The factory method function which is * given to this method is responsible for creating the actual {@code CommandResponseType}. This method receives the * partly deserialized values which can be completed by implementors if further values are required. * * @param factoryMethodFunction creates the actual {@code CommandResponseType} object. * @return the command response. * @throws NullPointerException if {@code factoryMethodFunction} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the JSON is invalid or if the command response type differs from the expected one. */ public T deserialize(final FactoryMethodFunction<T> factoryMethodFunction) { checkNotNull(factoryMethodFunction, "method for creating a command response object"); validateCommandResponseType(); final int statusCode = jsonObject.getValueOrThrow(CommandResponse.JsonFields.STATUS); final HttpStatusCode httpStatusCode = HttpStatusCode.forInt(statusCode).orElseThrow(() -> { final String msgPattern = "HTTP status code <{0}> of JSON Object is not supported!"; return new JsonParseException(MessageFormat.format(msgPattern, statusCode)); }); return factoryMethodFunction.create(httpStatusCode); }
/** * Creates a new {@link StatusDetailMessage} from a JSON object. * * @param jsonObject the JSON object. * @return the message. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws IllegalArgumentException if {@code jsonObject} is empty. * @throws JsonMissingFieldException if the passed in {@code jsonObject} was not in the expected format. */ public static StatusDetailMessage fromJson(final JsonObject jsonObject) { if (jsonObject.getSize() != 1) { throw new JsonParseException("Message must contain exactly one field, but does not:\n" + jsonObject); } final JsonField messageField = jsonObject.iterator().next(); final Level level = Level.valueOf(messageField.getKeyName()); final JsonValue message = messageField.getValue(); return of(level, message); }
/** * Creates a new {@link StatusDetailMessage} from a JSON object. * * @param jsonObject the JSON object. * @return the message. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws IllegalArgumentException if {@code jsonObject} is empty. * @throws JsonMissingFieldException if the passed in {@code jsonObject} was not in the expected format. */ public static StatusDetailMessage fromJson(final JsonObject jsonObject) { if (jsonObject.getSize() != 1) { throw new JsonParseException("Message must contain exactly one field, but does not:\n" + jsonObject); } final JsonField messageField = jsonObject.iterator().next(); final Level level = Level.valueOf(messageField.getKeyName()); final JsonValue message = messageField.getValue(); return of(level, message); }