1
|
package app;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileInputStream;
|
5
|
import java.io.FileOutputStream;
|
6
|
import java.io.IOException;
|
7
|
import java.io.ObjectInputStream;
|
8
|
import java.io.ObjectOutputStream;
|
9
|
import java.util.ArrayList;
|
10
|
import java.util.List;
|
11
|
import jdeserialize.Getopt;
|
12
|
import jdeserialize.classdesc;
|
13
|
import jdeserialize.content;
|
14
|
import jdeserialize.instance;
|
15
|
import jdeserialize.jdeserialize;
|
16
|
|
17
|
public class App {
|
18
|
|
19
|
/**
|
20
|
* When the Serialized and deserialized classes are same, but placed in diff packages, the
|
21
|
* deserialization does not work. When they are
|
22
|
**/
|
23
|
public static void main(String[] args) throws Exception {
|
24
|
App app = new App();
|
25
|
|
26
|
// app.standartSerializationDeserialization();
|
27
|
|
28
|
// https://code.google.com/archive/p/jdeserialize/wikis/Documentation.wiki
|
29
|
//test with calling the name of original jd project
|
30
|
//app.jdCallMain();
|
31
|
|
32
|
// test directly through code call
|
33
|
app.jdCallLib("serialized/simple.out");
|
34
|
}
|
35
|
|
36
|
private void jdCallLib(String filename)
|
37
|
{
|
38
|
try
|
39
|
{
|
40
|
//FileInputStream as usual
|
41
|
FileInputStream fis = null;
|
42
|
fis = new FileInputStream(filename);
|
43
|
|
44
|
//creating an instance of jdeserialize with given filename
|
45
|
jdeserialize jd = new jdeserialize(filename);
|
46
|
//running the main process of deserialization
|
47
|
jd.run(fis, false);
|
48
|
|
49
|
//gets the "contents" into an array - returnes the deserialization of all
|
50
|
// 'writes' into serialized object via objectOutputStream
|
51
|
List cntnts = jd.getContent();
|
52
|
String classname = cntnts.get(0).getClass().getName();
|
53
|
|
54
|
//For my example it is alway an instance.
|
55
|
instance locInst = (instance)cntnts.get(0);
|
56
|
|
57
|
//parsing the instance of instance data type into JSON
|
58
|
jdInstanceToJSON(locInst);
|
59
|
|
60
|
int c = 0;
|
61
|
}
|
62
|
catch (IOException e)
|
63
|
{
|
64
|
|
65
|
}
|
66
|
}
|
67
|
|
68
|
private void jdInstanceToJSON(instance in)
|
69
|
{
|
70
|
|
71
|
}
|
72
|
|
73
|
private void jdCallMain(String filename)
|
74
|
{
|
75
|
String[] myArgs= new String[1];
|
76
|
myArgs[0] = filename;
|
77
|
jdeserialize.main(myArgs); //testing call without any options. Outputs the deserialized data into console.
|
78
|
}
|
79
|
|
80
|
private void standartSerializationDeserialization() {
|
81
|
try {
|
82
|
System.out.println("Starting...");
|
83
|
|
84
|
this.serialize(new samples.ok.Simple(), "serialized/simple.out");
|
85
|
|
86
|
samples.nok.Simple test1 = this.deserialize("serialized/simple.out");
|
87
|
|
88
|
} catch (Exception e) {
|
89
|
System.out.println(e.toString());
|
90
|
System.out.println(e.getMessage());
|
91
|
}
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Standard way of deserializing objects with java.
|
96
|
*
|
97
|
* Taken from example method, doc
|
98
|
**/
|
99
|
private <T> T deserialize(String inputFilePath) {
|
100
|
try {
|
101
|
FileInputStream fis = new FileInputStream(inputFilePath);
|
102
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
103
|
T deserialized = (T) ois.readObject();
|
104
|
System.out.println(deserialized.toString());
|
105
|
ois.close();
|
106
|
|
107
|
return deserialized;
|
108
|
} catch (IOException e) {
|
109
|
e.printStackTrace();
|
110
|
} catch (ClassNotFoundException e) {
|
111
|
e.printStackTrace();
|
112
|
}
|
113
|
return null;
|
114
|
}
|
115
|
|
116
|
/**
|
117
|
* Standard way of serializing objects with java.
|
118
|
*
|
119
|
* Taken from example method, doc
|
120
|
**/
|
121
|
private void serialize(Object obj, String outputPath) {
|
122
|
try {
|
123
|
File toWrite = new File(outputPath);
|
124
|
toWrite.createNewFile();
|
125
|
|
126
|
FileOutputStream fos = new FileOutputStream(toWrite, false);
|
127
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
128
|
|
129
|
oos.writeObject(obj);
|
130
|
oos.flush();
|
131
|
oos.close();
|
132
|
System.out.println("serialized.");
|
133
|
} catch (Exception e) {
|
134
|
e.printStackTrace();
|
135
|
}
|
136
|
}
|
137
|
}
|