use of com.canoo.dp.impl.remoting.codec.encoders.CommandTranscoder in project dolphin-platform by canoo.
the class OptimizedJsonCodec method encode.
@Override
@SuppressWarnings("unchecked")
public String encode(final List<? extends Command> commands) {
Assert.requireNonNull(commands, "commands");
LOG.debug("Encoding command list with {} commands", commands.size());
final StringBuilder builder = new StringBuilder("[");
for (final Command command : commands) {
if (command == null) {
throw new IllegalArgumentException("Command list contains a null command: " + command);
} else {
LOG.trace("Encoding command of type {}", command.getClass());
final CommandTranscoder encoder = transcoders.get(command.getId());
if (encoder == null) {
throw new RuntimeException("No encoder for command type " + command.getClass() + " found");
}
final JsonObject jsonObject = encoder.encode(command);
GSON.toJson(jsonObject, builder);
builder.append(",");
}
}
if (!commands.isEmpty()) {
final int length = builder.length();
builder.delete(length - 1, length);
}
builder.append("]");
if (LOG.isTraceEnabled()) {
LOG.trace("Encoded message: {}", builder.toString());
}
return builder.toString();
}
Aggregations