- 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);
- }
+ final ObjectMapper jsonProcessor;
+
+ boolean inJson = ns.getString("output").equals("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.");
+ }
+
+ if (inJson) {
+ jsonProcessor = new ObjectMapper();
+ jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
+ jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
+ } else {
+ jsonProcessor = null;
+ }
+ try {
+ dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
+ if (jsonProcessor != null) {
+ JsonMessageEnvelope envelope = new JsonMessageEnvelope(messageReceived);
+ ObjectNode result = jsonProcessor.createObjectNode();
+ result.putPOJO("envelope", envelope);
+ try {
+ jsonProcessor.writeValue(System.out, result);
+ System.out.println();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ } 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.encodeBytes(messageReceived.getGroupId()));
+ }
+ if (messageReceived.getAttachments().size() > 0) {
+ System.out.println("Attachments: ");
+ for (String attachment : messageReceived.getAttachments()) {
+ System.out.println("- Stored plaintext in: " + attachment);