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 org.codehaus.jackson.map.ObjectMapper;
|
12
|
import org.codehaus.jackson.map.ObjectWriter;
|
13
|
import org.unsynchronized.jdeserialize;
|
14
|
|
15
|
public class App {
|
16
|
|
17
|
/**
|
18
|
* When the Serialized and deserialized classes are same, but placed in diff packages, the
|
19
|
* deserialization does not work. When they are
|
20
|
**/
|
21
|
public static void main(String[] args) throws Exception {
|
22
|
App app = new App();
|
23
|
|
24
|
app.standartSerializationDeserialization();
|
25
|
|
26
|
|
27
|
//test with calling the name of original jd project
|
28
|
//app.jdCallMain();
|
29
|
|
30
|
// test directly through code call
|
31
|
//app.jdCallLib("serialized/simple.out", true, "deserializedResults/simple.json");
|
32
|
//System.out.println("Vše OK, díky.");
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* Calls the jdeserialize through library approach.
|
37
|
*
|
38
|
* SHOULD NOT BE HERE
|
39
|
* **/
|
40
|
private void jdCallLib(String filename, boolean dump, String outputFileJSON)
|
41
|
{
|
42
|
try
|
43
|
{
|
44
|
//FileInputStream as usual
|
45
|
FileInputStream fis = null;
|
46
|
fis = new FileInputStream(filename);
|
47
|
|
48
|
//creating an instance of jdeserialize with given filename
|
49
|
jdeserialize jd = new jdeserialize(filename);
|
50
|
//running the main process of deserialization
|
51
|
jd.run(fis, false);
|
52
|
|
53
|
//gets the "contents" into an array - returnes the deserialization of all
|
54
|
// 'writes' into serialized object via objectOutputStream
|
55
|
List cntnts = jd.getContent();
|
56
|
String classname = cntnts.get(0).getClass().getName();
|
57
|
|
58
|
//testing with only first of the contents
|
59
|
String json = objectToJSON(cntnts.get(0));
|
60
|
|
61
|
if(dump)
|
62
|
{
|
63
|
System.out.println(json);
|
64
|
}
|
65
|
if(outputFileJSON != null && !outputFileJSON.isEmpty())
|
66
|
{
|
67
|
File toWrite = new File(outputFileJSON);
|
68
|
toWrite.createNewFile();
|
69
|
|
70
|
FileOutputStream fos = new FileOutputStream(toWrite, false);
|
71
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
72
|
|
73
|
oos.writeObject(json);
|
74
|
oos.flush();
|
75
|
oos.close();
|
76
|
System.out.println("Soubor " + outputFileJSON + " s deserializovaným objektem vytvořen.");
|
77
|
}
|
78
|
|
79
|
}
|
80
|
catch (IOException e)
|
81
|
{
|
82
|
|
83
|
}
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* Parses any object into String in JSON format
|
88
|
*
|
89
|
* SHOULD NOT BE HERE
|
90
|
* **/
|
91
|
private String objectToJSON(Object in)
|
92
|
{
|
93
|
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
94
|
String json = "";
|
95
|
try
|
96
|
{
|
97
|
json = ow.writeValueAsString(in);
|
98
|
}
|
99
|
catch (IOException e)
|
100
|
{
|
101
|
// TODO Auto-generated catch block
|
102
|
e.printStackTrace();
|
103
|
}
|
104
|
|
105
|
return json;
|
106
|
}
|
107
|
|
108
|
/**
|
109
|
* Basically, does the dump of deserialized object
|
110
|
* Just to check if everything works well
|
111
|
* and well, it does
|
112
|
*
|
113
|
* SHOULD NOT BE HERE
|
114
|
* NOT NEEDED
|
115
|
* **/
|
116
|
private void jdCallMain(String filename)
|
117
|
{
|
118
|
String[] myArgs= new String[1];
|
119
|
myArgs[0] = filename;
|
120
|
jdeserialize.main(myArgs); //testing call without any options. Outputs the deserialized data into console.
|
121
|
}
|
122
|
|
123
|
/**
|
124
|
* Serializes and deserializes when the object remains untouched.
|
125
|
* The package names used in this example shows the result (nearly clear)
|
126
|
*
|
127
|
* SHOULD NOT BE HERE
|
128
|
* NOT NEEDED
|
129
|
* **/
|
130
|
private void standartSerializationDeserialization() {
|
131
|
try {
|
132
|
System.out.println("Starting...");
|
133
|
|
134
|
this.serialize(new samples.ok.Simple(), "serialized/simple.out");
|
135
|
|
136
|
//samples.ok.SimpleChild test1 = this.deserialize("serialized/simple.out");
|
137
|
|
138
|
} catch (Exception e) {
|
139
|
System.out.println(e.toString());
|
140
|
System.out.println(e.getMessage());
|
141
|
}
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Standard way of deserializing objects with java.
|
146
|
*
|
147
|
* Taken from example method, doc
|
148
|
*
|
149
|
* SHOULD NOT BE HERE
|
150
|
* NOT NEEDED
|
151
|
**/
|
152
|
private <T> T deserialize(String inputFilePath) {
|
153
|
try {
|
154
|
FileInputStream fis = new FileInputStream(inputFilePath);
|
155
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
156
|
T deserialized = (T) ois.readObject();
|
157
|
System.out.println(deserialized.toString());
|
158
|
ois.close();
|
159
|
|
160
|
return deserialized;
|
161
|
} catch (IOException e) {
|
162
|
e.printStackTrace();
|
163
|
} catch (ClassNotFoundException e) {
|
164
|
e.printStackTrace();
|
165
|
}
|
166
|
return null;
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* Standard way of serializing objects with java.
|
171
|
*
|
172
|
* Taken from example method, doc
|
173
|
*
|
174
|
* SHOULD NOT BE HERE
|
175
|
**/
|
176
|
private void serialize(Object obj, String outputPath) {
|
177
|
try {
|
178
|
File toWrite = new File(outputPath);
|
179
|
toWrite.createNewFile();
|
180
|
|
181
|
FileOutputStream fos = new FileOutputStream(toWrite, false);
|
182
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
183
|
|
184
|
oos.writeObject(obj);
|
185
|
oos.flush();
|
186
|
oos.close();
|
187
|
System.out.println("serialized.");
|
188
|
} catch (Exception e) {
|
189
|
e.printStackTrace();
|
190
|
}
|
191
|
}
|
192
|
}
|