- if (dbusconnection != null) {
- try {
- dbusconnection.addSigHandler(Signal.MessageReceived.class, new DBusSigHandler<Signal.MessageReceived>() {
- @Override
- public void handle(Signal.MessageReceived s) {
- System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
- s.getSender(), DateUtils.formatTimestamp(s.getTimestamp()), s.getMessage()));
- if (s.getGroupId().length > 0) {
- System.out.println("Group info:");
- System.out.println(" Id: " + Base64.encodeBytes(s.getGroupId()));
- }
- if (s.getAttachments().size() > 0) {
- System.out.println("Attachments: ");
- for (String attachment : s.getAttachments()) {
- System.out.println("- Stored plaintext in: " + attachment);
- }
+ boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
+
+ // TODO delete later when "json" variable is removed
+ if (ns.getBoolean("json")) {
+ logger.warn("\"--json\" option has been deprecated, please use the global \"--output=json\" instead.");
+ }
+
+ final JsonWriter jsonWriter = inJson ? new JsonWriter(System.out) : null;
+ try {
+ dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
+ if (jsonWriter != null) {
+ JsonMessageEnvelope envelope = new JsonMessageEnvelope(messageReceived);
+ final Map<String, JsonMessageEnvelope> object = Map.of("envelope", envelope);
+ try {
+ jsonWriter.write(object);
+ } catch (IOException e) {
+ logger.error("Failed to write json object: {}", e.getMessage());
+ }
+ } else {
+ System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
+ messageReceived.getSender(),
+ DateUtils.formatTimestamp(messageReceived.getTimestamp()),
+ messageReceived.getMessage()));
+ if (messageReceived.getGroupId().length > 0) {
+ System.out.println("Group info:");
+ System.out.println(" Id: " + Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
+ }
+ if (messageReceived.getAttachments().size() > 0) {
+ System.out.println("Attachments: ");
+ for (String attachment : messageReceived.getAttachments()) {
+ System.out.println("- Stored plaintext in: " + attachment);