Extract JsonWriter for json output

This commit is contained in:
AsamK 2021-01-16 20:11:32 +01:00
parent d4b4bd8603
commit 03bf043959
6 changed files with 94 additions and 89 deletions

View file

@ -1,12 +1,10 @@
package org.asamk.signal.commands;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import org.asamk.signal.JsonWriter;
import org.asamk.signal.OutputType;
import org.asamk.signal.manager.Manager;
import org.slf4j.Logger;
@ -20,7 +18,6 @@ import java.util.stream.Collectors;
public class GetUserStatusCommand implements LocalCommand {
// TODO delete later when "json" variable is removed
private final static Logger logger = LoggerFactory.getLogger(GetUserStatusCommand.class);
@Override
@ -35,9 +32,6 @@ public class GetUserStatusCommand implements LocalCommand {
@Override
public int handleCommand(final Namespace ns, final Manager m) {
// Setup the json object mapper
ObjectMapper jsonProcessor = new ObjectMapper();
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
boolean inJson = ns.get("output") == OutputType.JSON || ns.getBoolean("json");
// TODO delete later when "json" variable is removed
@ -56,16 +50,18 @@ public class GetUserStatusCommand implements LocalCommand {
// Output
if (inJson) {
List<JsonIsRegistered> objects = registered.entrySet()
final JsonWriter jsonWriter = new JsonWriter(System.out);
List<JsonUserStatus> jsonUserStatuses = registered.entrySet()
.stream()
.map(entry -> new JsonIsRegistered(entry.getKey(), entry.getValue()))
.map(entry -> new JsonUserStatus(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
try {
jsonProcessor.writeValue(System.out, objects);
System.out.println();
jsonWriter.write(jsonUserStatuses);
} catch (IOException e) {
System.err.println(e.getMessage());
logger.error("Failed to write json object: {}", e.getMessage());
return 3;
}
} else {
for (Map.Entry<String, Boolean> entry : registered.entrySet()) {
@ -76,13 +72,13 @@ public class GetUserStatusCommand implements LocalCommand {
return 0;
}
private static final class JsonIsRegistered {
private static final class JsonUserStatus {
public String name;
public boolean isRegistered;
public JsonIsRegistered(String name, boolean isRegistered) {
public JsonUserStatus(String name, boolean isRegistered) {
this.name = name;
this.isRegistered = isRegistered;
}