19 |
19 |
/**
|
20 |
20 |
* When the Serialized and deserialized classes are same, but placed in diff packages, the
|
21 |
21 |
* deserialization does not work. When they are
|
|
22 |
*
|
|
23 |
* !CAREFUL! For testing purposes the somehow standartized package path (org.blah.etc...)
|
|
24 |
* is not followed. Repair in the future - because of used libs.
|
|
25 |
*
|
|
26 |
* jdeserializer also as .jar module!
|
22 |
27 |
**/
|
23 |
28 |
public static void main(String[] args) throws Exception {
|
24 |
29 |
App app = new App();
|
... | ... | |
33 |
38 |
app.jdCallLib("serialized/simple.out");
|
34 |
39 |
}
|
35 |
40 |
|
|
41 |
/**
|
|
42 |
* Calls the jdeserialize through library approach.
|
|
43 |
*
|
|
44 |
* SHOULD NOT BE HERE
|
|
45 |
* **/
|
36 |
46 |
private void jdCallLib(String filename)
|
37 |
47 |
{
|
38 |
48 |
try
|
... | ... | |
51 |
61 |
List cntnts = jd.getContent();
|
52 |
62 |
String classname = cntnts.get(0).getClass().getName();
|
53 |
63 |
|
54 |
|
//For my example it is alway an instance.
|
55 |
|
instance locInst = (instance)cntnts.get(0);
|
|
64 |
//testing with only first of the contents
|
|
65 |
String json = objectToJSON(cntnts.get(0));
|
56 |
66 |
|
57 |
|
//parsing the instance of instance data type into JSON
|
58 |
|
jdInstanceToJSON(locInst);
|
59 |
67 |
|
60 |
68 |
int c = 0;
|
61 |
69 |
}
|
... | ... | |
65 |
73 |
}
|
66 |
74 |
}
|
67 |
75 |
|
68 |
|
private void jdInstanceToJSON(instance in)
|
|
76 |
/**
|
|
77 |
* Parses any object into String in JSON format
|
|
78 |
*
|
|
79 |
* SHOULD NOT BE HERE
|
|
80 |
* **/
|
|
81 |
private String objectToJSON(Object in)
|
69 |
82 |
{
|
|
83 |
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
|
|
84 |
String json = ow.writeValueAsString(in);
|
70 |
85 |
|
|
86 |
return json;
|
71 |
87 |
}
|
72 |
88 |
|
|
89 |
/**
|
|
90 |
* Basically, does the dump of deserialized object
|
|
91 |
* Just to check if everything works well
|
|
92 |
* and well, it does
|
|
93 |
*
|
|
94 |
* SHOULD NOT BE HERE
|
|
95 |
* NOT NEEDED
|
|
96 |
* **/
|
73 |
97 |
private void jdCallMain(String filename)
|
74 |
98 |
{
|
75 |
99 |
String[] myArgs= new String[1];
|
... | ... | |
77 |
101 |
jdeserialize.main(myArgs); //testing call without any options. Outputs the deserialized data into console.
|
78 |
102 |
}
|
79 |
103 |
|
|
104 |
/**
|
|
105 |
* Serializes and deserializes when the object remains untouched.
|
|
106 |
* The package names used in this example shows the result (nearly clear)
|
|
107 |
*
|
|
108 |
* SHOULD NOT BE HERE
|
|
109 |
* NOT NEEDED
|
|
110 |
* **/
|
80 |
111 |
private void standartSerializationDeserialization() {
|
81 |
112 |
try {
|
82 |
113 |
System.out.println("Starting...");
|
... | ... | |
95 |
126 |
* Standard way of deserializing objects with java.
|
96 |
127 |
*
|
97 |
128 |
* Taken from example method, doc
|
|
129 |
*
|
|
130 |
* SHOULD NOT BE HERE
|
|
131 |
* NOT NEEDED
|
98 |
132 |
**/
|
99 |
133 |
private <T> T deserialize(String inputFilePath) {
|
100 |
134 |
try {
|
... | ... | |
117 |
151 |
* Standard way of serializing objects with java.
|
118 |
152 |
*
|
119 |
153 |
* Taken from example method, doc
|
|
154 |
*
|
|
155 |
* SHOULD NOT BE HERE
|
120 |
156 |
**/
|
121 |
157 |
private void serialize(Object obj, String outputPath) {
|
122 |
158 |
try {
|
#7768
added jackson JSON libraries