Blame view

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