Projekt

Obecné

Profil

Stáhnout (5.63 KB) Statistiky
| Větev: | Tag: | Revize:
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
        String res = app.objectToJSON(new samples.ok.Simple());
26
        System.out.println(res);
27
        
28
        //test with calling the name of original jd project
29
        //app.jdCallMain();
30

    
31
        // test directly through code call
32
        //app.jdCallLib("serialized/simple.out", true, "deserializedResults/simple.json");
33
        //System.out.println("Vše OK, díky.");
34
    }
35

    
36
    /**
37
     * Calls the jdeserialize through library approach.
38
     * 
39
     * SHOULD NOT BE HERE
40
     * **/
41
    private void jdCallLib(String filename, boolean dump, String outputFileJSON)
42
    {
43
        try
44
        {
45
            //FileInputStream as usual
46
            FileInputStream fis = null;
47
            fis = new FileInputStream(filename);
48
            
49
            //creating an instance of jdeserialize with given filename
50
            jdeserialize jd = new jdeserialize(filename);
51
            //running the main process of deserialization
52
            jd.run(fis, false);
53
            
54
            //gets the "contents" into an array - returnes the deserialization of all
55
            // 'writes' into serialized object via objectOutputStream
56
            List cntnts = jd.getContent();
57
            String classname = cntnts.get(0).getClass().getName();
58
            
59
            //testing with only first of the contents
60
            String json = objectToJSON(cntnts.get(0));
61

    
62
            if(dump) 
63
            {
64
                System.out.println(json);
65
            }
66
            if(outputFileJSON != null && !outputFileJSON.isEmpty())
67
            {
68
                File toWrite = new File(outputFileJSON);
69
                toWrite.createNewFile();
70

    
71
                FileOutputStream fos = new FileOutputStream(toWrite, false);
72
                ObjectOutputStream oos = new ObjectOutputStream(fos);
73

    
74
                oos.writeObject(json);
75
                oos.flush();
76
                oos.close();
77
                System.out.println("Soubor " + outputFileJSON + " s deserializovaným objektem vytvořen.");
78
            }
79

    
80
        }
81
        catch (IOException e) 
82
        {
83

    
84
        }
85
    }
86

    
87
    /**
88
     * Parses any object into String in JSON format
89
     * 
90
     * SHOULD NOT BE HERE
91
     * **/
92
    private String objectToJSON(Object in)
93
    {
94
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
95
        String json = "";
96
        try 
97
        {
98
            json = ow.writeValueAsString(in);
99
        } 
100
        catch (IOException e) 
101
        {
102
            // TODO Auto-generated catch block
103
            e.printStackTrace();
104
        }
105

    
106
        return json;
107
    }
108

    
109
    /**
110
     * Basically, does the dump of deserialized object
111
     * Just to check if everything works well
112
     * and well, it does
113
     * 
114
     * SHOULD NOT BE HERE
115
     * NOT NEEDED
116
     * **/
117
    private void jdCallMain(String filename)
118
    {
119
        String[] myArgs= new String[1];
120
        myArgs[0] = filename;
121
        jdeserialize.main(myArgs); //testing call without any options. Outputs the deserialized data into console.
122
    }
123

    
124
    /**
125
     * Serializes and deserializes when the object remains untouched.
126
     * The package names used in this example shows the result (nearly clear)
127
     * 
128
     * SHOULD NOT BE HERE
129
     * NOT NEEDED
130
     * **/
131
    private void standartSerializationDeserialization() {
132
        try {
133
            System.out.println("Starting...");
134

    
135
            this.serialize(new samples.ok.Simple(), "serialized/simple.out");
136

    
137
            //samples.ok.SimpleChild test1 = this.deserialize("serialized/simple.out");
138

    
139
        } catch (Exception e) {
140
            System.out.println(e.toString());
141
            System.out.println(e.getMessage());
142
        }
143
    }
144

    
145
    /**
146
     * Standard way of deserializing objects with java.
147
     * 
148
     * Taken from example method, doc
149
     * 
150
     * SHOULD NOT BE HERE
151
     * NOT NEEDED
152
     **/
153
    private <T> T deserialize(String inputFilePath) {
154
        try {
155
            FileInputStream fis = new FileInputStream(inputFilePath);
156
            ObjectInputStream ois = new ObjectInputStream(fis);
157
            T deserialized = (T) ois.readObject();
158
            System.out.println(deserialized.toString());
159
            ois.close();
160

    
161
            return deserialized;
162
        } catch (IOException e) {
163
            e.printStackTrace();
164
        } catch (ClassNotFoundException e) {
165
            e.printStackTrace();
166
        }
167
        return null;
168
    }
169

    
170
    /**
171
     * Standard way of serializing objects with java.
172
     * 
173
     * Taken from example method, doc
174
     * 
175
     * SHOULD NOT BE HERE
176
     **/
177
    private void serialize(Object obj, String outputPath) {
178
        try {
179
            File toWrite = new File(outputPath);
180
            toWrite.createNewFile();
181

    
182
            FileOutputStream fos = new FileOutputStream(toWrite, false);
183
            ObjectOutputStream oos = new ObjectOutputStream(fos);
184

    
185
            oos.writeObject(obj);
186
            oos.flush();
187
            oos.close();
188
            System.out.println("serialized.");
189
        } catch (Exception e) {
190
            e.printStackTrace();
191
        }
192
    }
193
}
    (1-1/1)