JsonListener.java
3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package com.ishchuk.antlr.json;
import com.ishchuk.antlr.json.model.*;
import com.ishchuk.antlr4.JSONgramBaseListener;
import com.ishchuk.antlr4.JSONgramLexer;
import com.ishchuk.antlr4.JSONgramParser;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import java.util.*;
public class JsonListener extends JSONgramBaseListener {
private JsonElement json;
private boolean read;
@Override
public void enterJson(JSONgramParser.JsonContext ctx) {
this.read = true;
}
public JsonElement getJsonFile() {
return this.json;
}
private JsonElement treatContext(JSONgramParser.ContentContext ctx) {
JsonElement elem;
if (Objects.nonNull(ctx.object())) {
elem = new JsonObject();
for (JSONgramParser.KeyValueContext kv : ctx.object().keyValue()) {
((JsonObject)elem).getMap().put(kv.CHAINE().getSymbol().getText(), treatContext(kv.content()));
}
} else if (Objects.nonNull(ctx.table())) {
elem = new JsonArray();
for (JSONgramParser.ContentContext ct : ctx.table().content()) {
((JsonArray)elem).getArray().add(treatContext(ct));
}
} else {
elem = new JsonPrimitive();
((JsonPrimitive)elem).setValue(ctx.getText());
}
return elem;
}
@Override
public void enterContent(JSONgramParser.ContentContext ctx) {
if (read) {
if (Objects.nonNull(ctx.object())) {
this.json = new JsonObject();
ctx.object().keyValue().forEach(kv -> {
((JsonObject)this.json).getMap().put(kv.CHAINE().getSymbol().getText(),
treatContext(kv.content()));
});
} else if (Objects.nonNull(ctx.table())) {
this.json = new JsonArray();
ctx.table().content().forEach(ct -> {
((JsonArray)this.json).getArray().add(treatContext(ct));
});
}
read = false;
}
}
public String convertToYml() {
StringBuilder builder = new StringBuilder();
builder.append("---\n");
if (Objects.nonNull(this.json)) {
builder.append(this.json.toYml(""));
}
return builder.toString();
}
public static void main(String[] args) {
System.out.println("Hello! With this application you can convert JSON file to YML file");
System.out.println("------------------------------------------------------------------");
Scanner in = new Scanner(System.in);
System.out.println("Please, copy your json file here");
String jsonString = "";
while(in.hasNextLine()){
jsonString += in.nextLine();
}
JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(jsonString));
CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
JSONgramParser JSONgramParser = new JSONgramParser(tokens);
ParseTreeWalker walker = new ParseTreeWalker();
JsonListener jsonWalker = new JsonListener();
walker.walk(jsonWalker, JSONgramParser.json());
System.out.println("\nYour file:\n");
System.out.println(jsonWalker.getJsonFile().toString());
System.out.println("\nYml file:\n");
System.out.println(jsonWalker.convertToYml());
}
}