尝试覆盖JsonSerializer.serialize方法,但我得到CommandSerializer is not abstract and does not override abstract method serialize(Command,Type,JsonSerializationContext) in JsonSerializer
我需要稍后实例化CommandSerializer
所以我不能把它变成一个抽象类。我错过了什么,我想看的其他地方?这是我的代码:
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
public interface Command extends DeviceCommand {
public static class CommandSerializer implements JsonSerializer<Command> {
public JsonElement serialize(Command command, Type type, JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(command.toString());
}
}
...
}
JsonSerializer看起来像这样:
public interface JsonSerializer<T> {
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context);
}