Revize 56837af0
Přidáno uživatelem Jan Havlíček před asi 5 roky(ů)
project/Deserializer/bcha.json | ||
---|---|---|
3 | 3 |
"testDouble" : 1.0, |
4 | 4 |
"testInt" : 123, |
5 | 5 |
"intArray" : [ 5, 10, 15 ], |
6 |
"locInnerClassInstance" : { |
|
6 |
"locInnerClassInstance" : {
|
|
7 | 7 |
class samples.ok.Inner : { |
8 | 8 |
"vnitrni" : "INNER STRING AM I" |
9 | 9 |
} |
project/Deserializer/src/Converter.java | ||
---|---|---|
8 | 8 |
import java.util.List; |
9 | 9 |
import java.util.Map; |
10 | 10 |
|
11 |
import javafx.util.Pair; |
|
11 | 12 |
import jdeserialize.content; |
12 | 13 |
import jdeserialize.jdeserialize; |
13 | 14 |
|
... | ... | |
18 | 19 |
private File output; |
19 | 20 |
|
20 | 21 |
//Key as enum? |
21 |
private HashMap<String, String> html = new HashMap<String, String>(); |
|
22 |
private HashMap<String, String> json = new HashMap<String, String>(); |
|
22 |
private HashMap<String, String> htmlFormatting = new HashMap<String, String>();
|
|
23 |
private HashMap<String, String> jsonFormatting = new HashMap<String, String>();
|
|
23 | 24 |
|
24 | 25 |
public Converter(Deserializer main, File inputFilename, File outputFilename) { |
25 | 26 |
this.main = main; |
... | ... | |
31 | 32 |
|
32 | 33 |
private void fillInTestingDictionaries() |
33 | 34 |
{ |
34 |
html.put("indent", ""); |
|
35 |
html.put("lineBreak", ""); |
|
36 |
html.put("classCol", ""); |
|
37 |
html.put("fieldCol", ""); |
|
38 |
html.put("valCol", ""); |
|
39 |
html.put("keywordCol", ""); |
|
35 |
htmlFormatting.put("indent", "<span style=\"margin-left:2em\">"); |
|
36 |
htmlFormatting.put("lineBreak", "<br/>"); |
|
37 |
htmlFormatting.put("classCol", "<span style=\"color:blue;font-weight:bold\">"); |
|
38 |
htmlFormatting.put("fieldCol", "<span style=\"color:purple\">"); |
|
39 |
htmlFormatting.put("valCol", "<span style=\"color:orange\">"); |
|
40 |
htmlFormatting.put("keywordCol", "<span style=\"color:green\">"); |
|
41 |
htmlFormatting.put("closeTagCol", "</span>"); |
|
40 | 42 |
|
41 |
json.put("indent", "\t"); |
|
42 |
json.put("lineBreak", "\n"); |
|
43 |
json.put("classCol", ""); |
|
44 |
json.put("fieldCol", ""); |
|
45 |
json.put("valCol", ""); |
|
46 |
json.put("keywordCol", ""); |
|
43 |
jsonFormatting.put("indent", "\t"); |
|
44 |
jsonFormatting.put("lineBreak", "\n"); |
|
45 |
jsonFormatting.put("classCol", ""); |
|
46 |
jsonFormatting.put("fieldCol", ""); |
|
47 |
jsonFormatting.put("valCol", ""); |
|
48 |
jsonFormatting.put("keywordCol", ""); |
|
49 |
jsonFormatting.put("closeTagCol", ""); |
|
47 | 50 |
} |
48 | 51 |
|
49 | 52 |
@Override |
... | ... | |
51 | 54 |
super.run(); |
52 | 55 |
|
53 | 56 |
try { |
54 |
String json = convert(FileWorker.loadByteArray(input)); |
|
57 |
//The Pair is not the best way |
|
58 |
Pair<String, String> returned = convert(FileWorker.loadByteArray(input)); |
|
55 | 59 |
|
60 |
String json = returned.getKey(); |
|
61 |
String html = returned.getValue(); |
|
62 |
|
|
56 | 63 |
String title = "Uložení JSON"; |
57 | 64 |
if (FileWorker.saveJson(output, json)) { |
58 | 65 |
System.out.println("The JSON file was saved correctly."); |
... | ... | |
70 | 77 |
main.convertIsComplete(); |
71 | 78 |
} |
72 | 79 |
|
73 |
private String convert(byte[] buffer) {
|
|
80 |
private Pair<String, String> convert(byte[] buffer) {
|
|
74 | 81 |
String json = ""; |
82 |
String html = ""; |
|
83 |
|
|
75 | 84 |
try { |
76 | 85 |
System.out.println("Deserialization begins."); |
77 | 86 |
|
... | ... | |
85 | 94 |
// 'writes' into serialized object via objectOutputStream |
86 | 95 |
List<content> cntnts = deserializer.getContent(); |
87 | 96 |
|
88 |
// testing with only first of the contents |
|
89 |
//ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); |
|
90 |
|
|
91 | 97 |
for(content cnt : cntnts) |
92 | 98 |
{ |
93 | 99 |
if(cnt != null) |
94 | 100 |
{ |
95 | 101 |
//Parametrizovany toJson pomoci dictionary |
96 |
//replace VS parametr |
|
97 |
//Pustit 2X s parametrem nenabori vnitrni data (kde muzou byt html znacky) |
|
98 | 102 |
//Ciste HTML / Cisty JSON |
99 |
json += cnt.toJson("", null, this.json); |
|
103 |
json += cnt.toJson("", null, this.jsonFormatting); |
|
104 |
html += cnt.toJson("", null, this.htmlFormatting); |
|
100 | 105 |
} |
101 | 106 |
} |
102 | 107 |
|
... | ... | |
104 | 109 |
|
105 | 110 |
//just for tests |
106 | 111 |
System.out.println(json); |
112 |
System.out.println("*********************\n"); |
|
113 |
System.out.println(html); |
|
107 | 114 |
|
108 |
return json;
|
|
115 |
return new Pair<String, String>(json, html);
|
|
109 | 116 |
} catch (IOException e) { |
110 | 117 |
System.out.println("An error occurred while processing!"); |
111 | 118 |
e.printStackTrace(); |
project/Deserializer/src/jdeserialize/classdesc.java | ||
---|---|---|
234 | 234 |
indentation += formatting.get("indent"); |
235 | 235 |
|
236 | 236 |
// ClassName |
237 |
sb.append(indentation).append("class ").append(this.name); |
|
237 |
sb.append(indentation).append(formatting.get("keywordCol")).append("class ").append(formatting.get("closeTagCol")); |
|
238 |
sb.append(formatting.get("classCol")).append(this.name).append(formatting.get("closeTagCol")); |
|
238 | 239 |
// extends |
239 | 240 |
if (this.superclass != null) { |
240 |
sb.append(" extends ").append(this.superclass.name); |
|
241 |
sb.append(formatting.get("keywordCol")).append(" extends ").append(formatting.get("closeTagCol")); |
|
242 |
sb.append(formatting.get("classCol")).append(this.superclass.name).append(formatting.get("closeTagCol")); |
|
241 | 243 |
} |
242 | 244 |
// implements |
243 | 245 |
if (this.interfaces != null) { |
244 |
sb.append(" implements ");
|
|
246 |
sb.append(formatting.get("keywordCol")).append(" implements ").append(formatting.get("closeTagCol"));
|
|
245 | 247 |
for (String str : this.interfaces) { |
246 |
sb.append(str);
|
|
248 |
sb.append(formatting.get("classCol")).append(str).append(formatting.get("closeTagCol"));
|
|
247 | 249 |
} |
248 | 250 |
} |
249 | 251 |
sb.append(" : {"); // ending of first line |
... | ... | |
265 | 267 |
} |
266 | 268 |
|
267 | 269 |
sb.append(formatting.get("lineBreak")).append(indentation); |
268 |
sb.append("\"").append(f.name).append("\"");
|
|
270 |
sb.append(formatting.get("fieldCol")).append("\"").append(f.name).append("\"").append(formatting.get("closeTagCol"));
|
|
269 | 271 |
sb.append(" : "); |
270 | 272 |
sb.append(val); |
271 | 273 |
|
... | ... | |
273 | 275 |
sb.append(", "); |
274 | 276 |
} |
275 | 277 |
} |
276 |
indentation = indentation.substring(0, indentation.length() - 1);
|
|
278 |
indentation = indentation.replaceFirst(formatting.get("indent"), "");
|
|
277 | 279 |
|
278 | 280 |
sb.append(formatting.get("lineBreak")).append(indentation).append("}"); |
279 | 281 |
|
280 | 282 |
if(this.superclass != null) |
281 | 283 |
{ |
282 |
indentation = indentation.substring(0, indentation.length() - 1);
|
|
284 |
indentation = indentation.replaceFirst(formatting.get("indent"), "");
|
|
283 | 285 |
sb.append(formatting.get("lineBreak")).append("}"); |
284 | 286 |
sb.append("," + formatting.get("lineBreak")); |
285 | 287 |
sb.append(this.superclass.toJson(indentation, fielddata, formatting)); |
286 | 288 |
} |
287 | 289 |
else |
288 | 290 |
{ |
289 |
indentation = indentation.substring(0, indentation.length() - 1);
|
|
291 |
indentation = indentation.replaceFirst(formatting.get("indent"), "");
|
|
290 | 292 |
sb.append(formatting.get("lineBreak")).append(indentation).append("}"); |
291 | 293 |
} |
292 | 294 |
|
project/Deserializer/src/jdeserialize/instance.java | ||
---|---|---|
26 | 26 |
this.fielddata = new HashMap<classdesc, Map<field, Object>>(); |
27 | 27 |
} |
28 | 28 |
|
29 |
|
|
29 | 30 |
public @Override String toJson(String indentation, Map<classdesc, Map<field, Object>> fielddata, Map<String, String> formatting) |
30 | 31 |
{ |
31 |
StringBuffer sb = new StringBuffer(); |
|
32 |
String val = ""; |
|
33 |
|
|
34 |
sb.append("{" + formatting.get("lineBreak")); |
|
35 |
|
|
36 |
indentation += formatting.get("indent"); |
|
37 |
|
|
38 |
// ClassName |
|
39 |
sb.append(indentation).append("class ").append(classdesc.name); |
|
40 |
// extends |
|
41 |
if (classdesc.superclass != null) { |
|
42 |
sb.append(" extends ").append(classdesc.superclass.name); |
|
43 |
} |
|
44 |
// implements |
|
45 |
if (classdesc.interfaces != null) { |
|
46 |
sb.append(" implements "); |
|
47 |
for (String str : classdesc.interfaces) { |
|
48 |
sb.append(str); |
|
49 |
} |
|
50 |
} |
|
51 |
sb.append(" : {"); // ending of first line |
|
52 |
|
|
53 |
indentation += formatting.get("indent"); |
|
54 |
|
|
55 |
for (field f : classdesc.fields) { |
|
56 |
// v this.fielddata najit element, jehoz key == classdesc |
|
57 |
// v tomto prvku fielddata najdu value (element) podle key == f |
|
58 |
// jeho value je chtena hodnota |
|
59 |
var locVal = this.fielddata.get(classdesc).get(f); |
|
60 |
|
|
61 |
if(locVal instanceof content) |
|
62 |
{ |
|
63 |
val = ((content)locVal).toJson(indentation, this.fielddata, formatting); |
|
64 |
} |
|
65 |
else { |
|
66 |
val = locVal.toString(); |
|
67 |
} |
|
68 |
|
|
69 |
sb.append(formatting.get("lineBreak")).append(indentation); |
|
70 |
sb.append("\"").append(f.name).append("\""); |
|
71 |
sb.append(" : "); |
|
72 |
sb.append(val); |
|
73 |
|
|
74 |
if (!f.equals(classdesc.fields[classdesc.fields.length - 1])) { |
|
75 |
sb.append(", "); |
|
76 |
} |
|
77 |
} |
|
78 |
indentation = indentation.substring(0, indentation.length() - 1); |
|
79 |
|
|
80 |
sb.append(formatting.get("lineBreak")).append(indentation).append("}"); |
|
81 |
|
|
82 |
if(classdesc.superclass != null) |
|
83 |
{ |
|
84 |
indentation = indentation.substring(0, indentation.length() - 1); |
|
85 |
sb.append(formatting.get("lineBreak") + "}"); |
|
86 |
sb.append(",").append(formatting.get("lineBreak")); |
|
87 |
sb.append(classdesc.superclass.toJson(indentation, this.fielddata, formatting)); |
|
88 |
} |
|
89 |
else |
|
90 |
{ |
|
91 |
indentation = indentation.substring(0, indentation.length() - 1); |
|
92 |
sb.append(formatting.get("lineBreak")).append(indentation).append("}"); |
|
93 |
} |
|
94 |
|
|
95 |
return sb.toString(); |
|
32 |
return this.classdesc.toJson(indentation, this.fielddata, formatting); |
|
96 | 33 |
} |
97 | 34 |
|
98 | 35 |
public String toString() { |
Také k dispozici: Unified diff
re#7868 HTML formatting implemented.
The Converter.convert returnes Pair<String, String> value.
The first is json, second html - for further work.