/** * Creates a new {@code PolicyDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyDeleted} which was created from the given JSON object. * @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 * 'PolicyDeleted' format. */ public static PolicyDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyDeleted>(TYPE, jsonObject).deserialize( (revision, timestamp) -> of(jsonObject.getValueOrThrow(JsonFields.POLICY_ID), revision, timestamp, dittoHeaders)); }
/** * Constructs a new {@code EventJsonDeserializer} object. * * @param type the type of the target event of deserialization. * @param jsonObject the JSON object to deserialized. * @throws NullPointerException if any argument is {@code null}. * @throws IllegalArgumentException if {@code type} is empty or does not contain a type prefix. */ public EventJsonDeserializer(final String type, final JsonObject jsonObject) { validateType(argumentNotEmpty(type, "event type")); checkNotNull(jsonObject, "JSON object to be deserialized"); this.jsonObject = jsonObject; expectedType = type; eventTypePrefix = type.split(":")[0]; }
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)); } }
/** * 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 Event}. This method receives the partly * deserialized values which can be completed by the implementor by further values if required. * * @param factoryMethodFunction creates the actual {@code Event} object. * @return the created event. * @throws NullPointerException if {@code factoryMethodFunction} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the JSON is invalid or if the event TYPE differs from the * expected one. */ public T deserialize(final FactoryMethodFunction<T> factoryMethodFunction) { checkNotNull(factoryMethodFunction, "method for creating an event object"); validateEventType(); // added in V2 for V1, fallback to revision "0": final Long revision = jsonObject.getValue(Event.JsonFields.REVISION).orElse(0L); final Instant timestamp = jsonObject.getValue(Event.JsonFields.TIMESTAMP.getPointer()) .filter(JsonValue::isString) .map(JsonValue::asString) .map(EventJsonDeserializer::tryToParseModified) .orElse(null); return factoryMethodFunction.create(revision, timestamp); }
/** * Creates a new {@code PolicyDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyDeleted} which was created from the given JSON object. * @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 * 'PolicyDeleted' format. */ public static PolicyDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyDeleted>(TYPE, jsonObject).deserialize( (revision, timestamp) -> of(jsonObject.getValueOrThrow(JsonFields.POLICY_ID), revision, timestamp, dittoHeaders)); }
/** * Creates a new {@code AttributesDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new AttributesDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code AttributesDeleted} which was created from the given JSON object. * @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 * 'AttributesDeleted' format. */ public static AttributesDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<AttributesDeleted>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String extractedThingId = jsonObject.getValueOrThrow(JsonFields.THING_ID); return of(extractedThingId, revision, dittoHeaders); }); }
/** * Creates a new {@code ThingDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new ThingDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ThingDeleted} which was created from the given JSON object. * @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 * 'ThingDeleted' format. */ public static ThingDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ThingDeleted>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String extractedThingId = jsonObject.getValueOrThrow(JsonFields.THING_ID); return of(extractedThingId, revision, timestamp, dittoHeaders); }); }
/** * Creates a {@code ConnectionOpened} event from a JSON object. * * @param jsonObject the JSON object of which the event is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the event. * @throws NullPointerException if any argument is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static ConnectionOpened fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ConnectionOpened>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String readConnectionId = jsonObject.getValueOrThrow(JsonFields.CONNECTION_ID); return of(readConnectionId, timestamp, dittoHeaders); }); }
/** * Creates a {@code ConnectionDeleted} event from a JSON object. * * @param jsonObject the JSON object of which the event is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the event. * @throws NullPointerException if any argument is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static ConnectionDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ConnectionDeleted>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String readConnectionId = jsonObject.getValueOrThrow(JsonFields.CONNECTION_ID); return of(readConnectionId, timestamp, dittoHeaders); }); }
/** * Creates a {@code ConnectionDeleted} event from a JSON object. * * @param jsonObject the JSON object of which the event is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the event. * @throws NullPointerException if any argument is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static ConnectionDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ConnectionDeleted>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String readConnectionId = jsonObject.getValueOrThrow(JsonFields.CONNECTION_ID); return of(readConnectionId, timestamp, dittoHeaders); }); }
/** * Creates a new {@code FeaturesDeleted} from a JSON object. * * @param jsonObject the JSON object from which a new FeaturesDeleted instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code FeaturesDeleted} which was created from the given JSON object. * @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 * 'FeaturesDeleted' format. */ public static FeaturesDeleted fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<FeaturesDeleted>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String extractedThingId = jsonObject.getValueOrThrow(JsonFields.THING_ID); return of(extractedThingId, revision, timestamp, dittoHeaders); }); }
/** * Creates a {@code ConnectionClosed} event from a JSON object. * * @param jsonObject the JSON object of which the event is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the event. * @throws NullPointerException if any argument is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static ConnectionClosed fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ConnectionClosed>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String readConnectionId = jsonObject.getValueOrThrow(JsonFields.CONNECTION_ID); return of(readConnectionId, timestamp, dittoHeaders); }); }
/** * Creates a {@code ConnectionClosed} event from a JSON object. * * @param jsonObject the JSON object of which the event is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the event. * @throws NullPointerException if any argument is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static ConnectionClosed fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ConnectionClosed>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String readConnectionId = jsonObject.getValueOrThrow(JsonFields.CONNECTION_ID); return of(readConnectionId, timestamp, dittoHeaders); }); }
/** * Creates a {@code ConnectionOpened} event from a JSON object. * * @param jsonObject the JSON object of which the event is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the event. * @throws NullPointerException if any argument is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. */ public static ConnectionOpened fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ConnectionOpened>(TYPE, jsonObject) .deserialize((revision, timestamp) -> { final String readConnectionId = jsonObject.getValueOrThrow(JsonFields.CONNECTION_ID); return of(readConnectionId, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyModified} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyModified} which was created from the given JSON object. * @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 'PolicyModified' format. */ public static PolicyModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return of(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyCreated} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyCreated} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonString} was not in the expected 'PolicyCreated' format. */ public static PolicyCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyCreated>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return new PolicyCreated(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyIdModified} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyIdModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyIdModified} which was created from the given JSON object. * @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 * 'PolicyIdModified' format. */ public static PolicyIdModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyIdModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final String extractedThingId = jsonObject.getValueOrThrow(JsonFields.THING_ID); final String extractedPolicyId = jsonObject.getValueOrThrow(JSON_POLICY_ID); return of(extractedThingId, extractedPolicyId, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code ThingModified} from a JSON object. * * @param jsonObject the JSON object from which a new ThingModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code ThingModified} which was created from the given JSON object. * @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 * 'ThingModified' format. */ public static ThingModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<ThingModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject thingJsonObject = jsonObject.getValueOrThrow(JsonFields.THING); // THING was in V1 and V2 final Thing extractedModifiedThing = ThingsModelFactory.newThing(thingJsonObject); return of(extractedModifiedThing, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyModified} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyModified instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyModified} which was created from the given JSON object. * @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 'PolicyModified' format. */ public static PolicyModified fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyModified>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return of(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }
/** * Creates a new {@code PolicyCreated} from a JSON object. * * @param jsonObject the JSON object from which a new PolicyCreated instance is to be created. * @param dittoHeaders the headers of the command which was the cause of this event. * @return the {@code PolicyCreated} which was created from the given JSON object. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonString} was not in the expected 'PolicyCreated' format. */ public static PolicyCreated fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new EventJsonDeserializer<PolicyCreated>(TYPE, jsonObject).deserialize((revision, timestamp) -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy extractedModifiedPolicy = PoliciesModelFactory.newPolicy(policyJsonObject); return new PolicyCreated(extractedModifiedPolicy, revision, timestamp, dittoHeaders); }); }