Serialize classes
Posted: Sun Aug 04, 2019 8:15 pm
Hi, trying to save some presets but get problems when trying to serialize a class. Class code looks like this:
class Effect implements java.io.Serializable {
private static final long serialVersionUID = 1;
private int algorithmIndex = 0;
private double amount = 0;
public Effect() {
}
public Effect(int algorithmIndex, double amount) {
this.algorithmIndex = algorithmIndex;
this.amount = amount;
}
public void setAlgorithmIndex (int algorithmIndex) {
this.algorithmIndex = algorithmIndex;
}
public int getAlgorithmIndex () {
return this.algorithmIndex;
}
public void setAmount (double amount) {
this.amount = amount;
}
public double getAmount () {
return this.amount;
}
}
Serialization code (added to the init function for a simple example):
Effect serializeObject = new Effect(1, 0.5);
byte[] stream = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);) {
oos.writeObject(serializeObject);
stream = baos.toByteArray();
Log("Serialization ok");
} catch (IOException e) {
// Error in serialization
e.printStackTrace();
Log("IOException is caught: " + e);
}
The only other added code is:
import java.io.*;
Code works well when running it with java in a separate file but fails in the designer.
Running jdk1.8.0_202 on windows 10.
Would be really happy for some help on this or sample code on how to serialize an array of objects that works in module designer (not a great java developer, module designer is the only reason I use java ).
class Effect implements java.io.Serializable {
private static final long serialVersionUID = 1;
private int algorithmIndex = 0;
private double amount = 0;
public Effect() {
}
public Effect(int algorithmIndex, double amount) {
this.algorithmIndex = algorithmIndex;
this.amount = amount;
}
public void setAlgorithmIndex (int algorithmIndex) {
this.algorithmIndex = algorithmIndex;
}
public int getAlgorithmIndex () {
return this.algorithmIndex;
}
public void setAmount (double amount) {
this.amount = amount;
}
public double getAmount () {
return this.amount;
}
}
Serialization code (added to the init function for a simple example):
Effect serializeObject = new Effect(1, 0.5);
byte[] stream = null;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);) {
oos.writeObject(serializeObject);
stream = baos.toByteArray();
Log("Serialization ok");
} catch (IOException e) {
// Error in serialization
e.printStackTrace();
Log("IOException is caught: " + e);
}
The only other added code is:
import java.io.*;
Code works well when running it with java in a separate file but fails in the designer.
Running jdk1.8.0_202 on windows 10.
Would be really happy for some help on this or sample code on how to serialize an array of objects that works in module designer (not a great java developer, module designer is the only reason I use java ).