@Override protected String resolveType(final JsonObject jsonObject) { final Supplier<String> command = () -> jsonObject.getValue(Command.JsonFields.ID) .map(cmd -> getTypePrefix() + cmd) // and transform to V2 format // fail if "command" also is not present .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(Command.JsonFields.TYPE.getPointer().toString()) .build()); // if type was not present (was included in V2) take "command" instead return jsonObject.getValue(Command.JsonFields.TYPE).orElseGet(command); }
@Override protected String resolveType(final JsonObject jsonObject) { final Supplier<String> command = () -> jsonObject.getValue(Command.JsonFields.ID) .map(cmd -> getTypePrefix() + cmd) // and transform to V2 format // fail if "command" also is not present .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(Command.JsonFields.TYPE.getPointer().toString()) .build()); // if type was not present (was included in V2) take "command" instead return jsonObject.getValue(Command.JsonFields.TYPE).orElseGet(command); }
/** * Creates a response to a {@link RetrieveThing} command from a JSON object. * * @param jsonObject the JSON object of which the response is to be created. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static RetrieveThingResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<RetrieveThingResponse>(TYPE, jsonObject) .deserialize(statusCode -> { final String thingId = jsonObject.getValueOrThrow(ThingQueryCommandResponse.JsonFields.JSON_THING_ID); final JsonObject extractedThing = jsonObject.getValue(JSON_THING).orElse(null); final String extractedThingPlainJson = jsonObject.getValue(JSON_THING_PLAIN_JSON) .orElseGet(() -> { if (null == extractedThing) { throw JsonMissingFieldException.newBuilder() .fieldName(JSON_THING.getPointer()) .build(); } return extractedThing.toString(); }); return new RetrieveThingResponse(thingId, statusCode, extractedThing, extractedThingPlainJson, dittoHeaders); }); }
.orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(ThingCommandResponse.JsonFields.JSON_THING_ID.getPointer()) .build()); final JsonObject payload = jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.PAYLOAD).asObject();
.orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(ThingCommandResponse.JsonFields.JSON_THING_ID.getPointer()) .build()); final JsonObject payload = jsonObject.getValueOrThrow(ThingCommandResponse.JsonFields.PAYLOAD).asObject();
/** * Creates a response to a {@link RetrieveThing} command from a JSON object. * * @param jsonObject the JSON object of which the response is to be created. * @param dittoHeaders the headers of the preceding command. * @return the response. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static RetrieveThingResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandResponseJsonDeserializer<RetrieveThingResponse>(TYPE, jsonObject) .deserialize(statusCode -> { final String thingId = jsonObject.getValueOrThrow(ThingQueryCommandResponse.JsonFields.JSON_THING_ID); final JsonObject extractedThing = jsonObject.getValue(JSON_THING).orElse(null); final String extractedThingPlainJson = jsonObject.getValue(JSON_THING_PLAIN_JSON) .orElseGet(() -> { if (null == extractedThing) { throw JsonMissingFieldException.newBuilder() .fieldName(JSON_THING.getPointer()) .build(); } return extractedThing.toString(); }); return new RetrieveThingResponse(thingId, statusCode, extractedThing, extractedThingPlainJson, dittoHeaders); }); }
/** * Deserializes the {@link Message} from the JSON representation - the {@code rawPayload} is decoded with Base64. * * @param <T> the type of the message's payload. * @param jsonObject the JsonObjectReader to use for reading the message * @return the Message */ protected static <T> Message<T> deserializeMessageFromJson(final JsonObject jsonObject) { final JsonObject messageObject = jsonObject.getValueOrThrow(MessageCommandResponse.JsonFields.JSON_MESSAGE); final JsonObject messageHeadersObject = messageObject.getValue(MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS) .filter(JsonValue::isObject) .map(JsonValue::asObject) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName( MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS.getPointer().toString()) .build()); final Optional<JsonValue> messagePayloadOptional = messageObject.getValue(MessageCommand.JsonFields.JSON_MESSAGE_PAYLOAD); final MessageHeaders messageHeaders = MessageHeaders.of(messageHeadersObject); final MessageBuilder<T> messageBuilder = Message.<T>newBuilder(messageHeaders); MessagePayloadSerializer.deserialize(messagePayloadOptional, messageBuilder, messageHeaders); return messageBuilder.build(); }
/** * Deserializes the {@link Message} from the JSON representation - the {@code rawPayload} is decoded with Base64. * * @param <T> the type of the message's payload. * @param jsonObject the JsonObjectReader to use for reading the message * @return the Message */ protected static <T> Message<T> deserializeMessageFromJson(final JsonObject jsonObject) { final JsonObject messageObject = jsonObject.getValueOrThrow(MessageCommandResponse.JsonFields.JSON_MESSAGE); final JsonObject messageHeadersObject = messageObject.getValue(MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS) .filter(JsonValue::isObject) .map(JsonValue::asObject) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName( MessageCommandResponse.JsonFields.JSON_MESSAGE_HEADERS.getPointer().toString()) .build()); final Optional<JsonValue> messagePayloadOptional = messageObject.getValue(MessageCommand.JsonFields.JSON_MESSAGE_PAYLOAD); final MessageHeaders messageHeaders = MessageHeaders.of(messageHeadersObject); final MessageBuilder<T> messageBuilder = Message.<T>newBuilder(messageHeaders); MessagePayloadSerializer.deserialize(messagePayloadOptional, messageBuilder, messageHeaders); return messageBuilder.build(); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { throw JsonMissingFieldException.newBuilder().fieldName(fieldDefinition.getPointer()).build(); }
private static long revisionFrom(final Adaptable adaptable) { return adaptable.getPayload().getRevision().orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(Payload.JsonFields.REVISION.getPointer().toString()).build()); }
@Override public <T> T getValueOrThrow(final JsonFieldDefinition<T> fieldDefinition) { throw JsonMissingFieldException.newBuilder().fieldName(fieldDefinition.getPointer()).build(); }
@Override protected String resolveType(final JsonObject jsonObject) { return jsonObject.getValue(Command.JsonFields.TYPE) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(Command.JsonFields.TYPE.getPointer().toString()) .build()); }
@Override protected String resolveType(final JsonObject jsonObject) { return jsonObject.getValue(DittoRuntimeException.JsonFields.ERROR_CODE) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(DittoRuntimeException.JsonFields.ERROR_CODE.getPointer().toString()) .build()); }
@Override protected String resolveType(final JsonObject jsonObject) { return jsonObject.getValue(DittoRuntimeException.JsonFields.ERROR_CODE) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(DittoRuntimeException.JsonFields.ERROR_CODE.getPointer().toString()) .build()); }
private static long revisionFrom(final Adaptable adaptable) { return adaptable.getPayload().getRevision().orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(Payload.JsonFields.REVISION.getPointer().toString()).build()); }
private static String getMessage(final JsonObject jsonObject) { return jsonObject.getValue(DittoJsonException.JsonFields.MESSAGE) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(DittoRuntimeException.JsonFields.MESSAGE.getPointer().toString()).build()); }
private static String getMessage(final JsonObject jsonObject) { return jsonObject.getValue(DittoJsonException.JsonFields.MESSAGE) .orElseThrow(() -> JsonMissingFieldException.newBuilder() .fieldName(DittoRuntimeException.JsonFields.MESSAGE.getPointer().toString()).build()); }