0

Context:

So I have a method call that I want to save into a text file. The purpose of this is saving a runnable serialized object into a text file and getting it later from the text file to execute.

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

Error is:

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.

I understand the error, Runnable is an interface and it cannot be serialized. However is there something else that I can do that can solve my problem?

Solution Attempt 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
}
}
Jill Mathers
  • 33
  • 1
  • 7
  • Must you save as plain text or can you use binary serialization like Java or Protobuf? – Abhijit Sarkar Dec 13 '17 at 19:05
  • You're trying to serialize to JSON? What output are you expecting? – shmosel Dec 13 '17 at 19:09
  • Im flexible to use anything however I tried ObjectOutputStream/ writeObject and that gives the same error @AbhijitSarkar – Jill Mathers Dec 13 '17 at 19:09
  • @shmosel It does not have to be in JSON but using java serialization still fails. – Jill Mathers Dec 13 '17 at 19:10
  • 1
    @shmosel You marked it as duplicate but the accepted answer for the question does not solve my error. Please remove. – Jill Mathers Dec 13 '17 at 19:25
  • Because you're trying to serialize to JSON. But you said you're ok with Java serialization. Did you try that? – shmosel Dec 13 '17 at 19:27
  • 1
    I think, that mark this question as duplicate is kinda stupid. People which will be looking for Gson serialization will find this question and no proper anwear. So please remove duplicate flag because it does not solve anything... – Majlanky Dec 14 '17 at 08:17
  • And BTW, serialization is saved state of instance so result of serialization into Json is kinda obvious> all attributes and its values will be written into json as key:value pairs. And other BTW, this can be usefull for transfer states and what more even between two classes with similar attributes... – Majlanky Dec 14 '17 at 08:24

1 Answers1

-1

One blind try i can recommend to you is to create some implementation of runnable and try your code with it. For example:

    public class Main {

    public static void main(String[] args) {

        Runnable a = new A();

        String gson = new Gson().toJson(a);
        Runnable runnable = new Gson().fromJson(gson, A.class);

        runnable.run();
    }

    public static class A implements Runnable{

        public void run() {
            System.out.println("Here im");
        }



    }
}

BTW replace my System.out.println("Here im"); by yours client.publish

Majlanky
  • 198
  • 2
  • 9
  • Yes, I'm trying this now. Wanted to know if there are any other solutions to this. Preferably more 'elegant' – Jill Mathers Dec 13 '17 at 18:40
  • Tried this but failed. – Jill Mathers Dec 13 '17 at 19:05
  • Whoever gave me minus one, thx very much. Jill you did implementation of Runnable little bit messy. I edit my post for exact tutorial how to do it... The code you made contains again attribute of Runnable... – Majlanky Dec 14 '17 at 08:13
  • And last info, you will have troubles with your client. Client atttribute have to be "serializable" in same way. If it is not possible to do it, you really need to go another way because Gson is not prepared for this usecase (it is designed for pojo classes). – Majlanky Dec 14 '17 at 11:42
  • Thanks for the replies. I am most likely going to try another approach as I don't think serialising Runnable will work in this case. – Jill Mathers Dec 14 '17 at 11:47