1
|
package deserialize;
|
2
|
|
3
|
import java.io.BufferedWriter;
|
4
|
import java.io.FileWriter;
|
5
|
import java.io.IOException;
|
6
|
import java.nio.ByteBuffer;
|
7
|
import java.util.HashMap;
|
8
|
|
9
|
import org.json.simple.JSONArray;
|
10
|
import org.json.simple.JSONObject;
|
11
|
|
12
|
import general.Constants;
|
13
|
|
14
|
public class ToJSON extends Thread {
|
15
|
|
16
|
private final static byte TC_CLASSDESC = 0x72;
|
17
|
private final static byte TC_ENDBLOCKDATA = 0x78;
|
18
|
private final static byte SC_SERIALIZABLE = 0x02;
|
19
|
|
20
|
HashMap<Character, String> typeCodes;
|
21
|
|
22
|
private byte buffer[];
|
23
|
|
24
|
public ToJSON(byte buffer[]) {
|
25
|
this.buffer = buffer;
|
26
|
|
27
|
typeCodes = new HashMap<Character, String>();
|
28
|
typeCodes.put('B', "byte");
|
29
|
typeCodes.put('C', "char");
|
30
|
typeCodes.put('D', "double");
|
31
|
typeCodes.put('F', "float");
|
32
|
typeCodes.put('I', "integer");
|
33
|
typeCodes.put('J', "long");
|
34
|
typeCodes.put('S', "short");
|
35
|
typeCodes.put('Z', "boolean");
|
36
|
// ---
|
37
|
typeCodes.put('[', "array");
|
38
|
typeCodes.put('L', "object");
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
public void run() {
|
43
|
super.run();
|
44
|
|
45
|
for (int i = 0; i < buffer.length; i++) {
|
46
|
if (buffer[i] == TC_CLASSDESC) {
|
47
|
// Class name
|
48
|
int nameLength = Formatter.createInt(buffer[++i], buffer[++i]);
|
49
|
String name = new String(buffer, ++i, nameLength);
|
50
|
i += nameLength - 1;
|
51
|
System.out.println(name);
|
52
|
|
53
|
// UID
|
54
|
ByteBuffer toLong = ByteBuffer.allocate(Long.BYTES);
|
55
|
toLong.put(buffer, i + 1, 8);
|
56
|
toLong.flip();
|
57
|
System.out.println(toLong.getLong());
|
58
|
|
59
|
// implements ...
|
60
|
i += 9;
|
61
|
System.out.println(buffer[i] == SC_SERIALIZABLE);
|
62
|
|
63
|
// Variables
|
64
|
int vCount = Formatter.createInt(buffer[++i], buffer[++i]);
|
65
|
for (int j = 0; j < vCount; j++) {
|
66
|
// Type
|
67
|
char vType = (char) (buffer[++i]);
|
68
|
// Name
|
69
|
int vNameLength = Formatter.createInt(buffer[++i], buffer[++i]);
|
70
|
String vName = new String(buffer, ++i, vNameLength);
|
71
|
i += vNameLength - 1;
|
72
|
// toString()
|
73
|
System.out.println(typeCodes.get(vType) + " " + new String(vName));
|
74
|
}
|
75
|
|
76
|
// TODO
|
77
|
System.out.println(buffer[++i] == TC_ENDBLOCKDATA);
|
78
|
}
|
79
|
}
|
80
|
}
|
81
|
|
82
|
@SuppressWarnings("unchecked")
|
83
|
private void save() {
|
84
|
/*
|
85
|
JSONObject countryObj = new JSONObject();
|
86
|
countryObj.put("Name", "India");
|
87
|
countryObj.put("Population", new Integer(1000000));
|
88
|
|
89
|
JSONArray listOfStates = new JSONArray();
|
90
|
listOfStates.add("Madhya Pradesh");
|
91
|
listOfStates.add("Maharastra");
|
92
|
listOfStates.add("Rajasthan");
|
93
|
|
94
|
countryObj.put("States", listOfStates);
|
95
|
|
96
|
try {
|
97
|
// Writing to a file
|
98
|
FileWriter fw = new FileWriter(Constants.JSON_FILE);
|
99
|
BufferedWriter bw = new BufferedWriter(fw);
|
100
|
System.out.println("Zapisuji JSON objekt do souboru.");
|
101
|
bw.write(countryObj.toJSONString());
|
102
|
bw.flush();
|
103
|
bw.close();
|
104
|
} catch (IOException e) {
|
105
|
e.printStackTrace();
|
106
|
}
|
107
|
*/
|
108
|
}
|
109
|
|
110
|
}
|