컨텍스트 :Runnable를 deserialize하는 방법이 있습니까?
그래서 텍스트 파일에 저장하려는 메서드 호출이 있습니다. 이 목적은 실행 가능한 직렬화 된 객체를 텍스트 파일에 저장하고 나중에 텍스트 파일에서 가져와 실행하는 것입니다.
final Runnable runnable =() -> { //Runnable object to serialize
client.publish("me/feed", GraphResponse.class,
Parameter.with("message", statusMessage));
};
final String gson = new Gson().toJson(runnable); // Serialized runnable as json. This works successfully.
final Runnable x = new Gson().fromJson(gson, Runnable.class); // error
오류는 다음과 같습니다
나는 오류를 이해java.lang.RuntimeException: Unable to invoke no-args constructor for interface java.lang.Runnable. Registering an InstanceCreator with Gson for this type may fix this problem.
는의 Runnable은 인터페이스이며 직렬화 할 수 없습니다. 그러나 내 문제를 해결할 수있는 다른 방법이 있습니까? 내가 당신에게 추천 할 수
해결 시도 1. ERROR
public class RunnableImplementation implements Runnable, Serializable {
Runnable runnable;
public RunnableImplementation() {
}
public RunnableImplementation(final Runnable runnable) {
this.runnable = runnable;
}
@Override
public void run() {
runnable.run();
}
}
public class ExampleClass {
public static void main(String[] args) {
final Runnable runnable =() -> {
client.publish("me/feed", GraphResponse.class,
Parameter.with("message", statusMessage));
};
RunnableImplementation x = new RunnableImplementation(runnable);
String gson = new Gson().toJson(x);
RunnableImplementation runnableImplementation = new Gson().fromJson(gson, RunnableImplementation.class); // causes same error as above
}
}
yours client.publish
에 의해 일반 텍스트로 저장해야 내 대체하거나 Java 또는 Protobuf 같은 바이너리 직렬화를 사용할 수 있습니까? –JSON에 직렬화하려고하십니까? 어떤 결과를 기대합니까? – shmosel
인스턴트 메신저하지만 난 아무것도 사용하는 유연한 ObjectOutputStream/writeObject를 시도하고 그 같은 오류를 제공합니다 @AbhijitSarkar –