Commit 33a6a1e0e3653c9ee8c11b077e0cc49e60344491

Authored by eishchuk
1 parent ea3b85f2

Converter to Yml

src/main/antlr4/com/ishchuk/antlr4/JSONgram.g4
... ... @@ -12,7 +12,7 @@ content: CHAINE | NUMBER | object | table | 'true' | 'false' | 'null';
12 12 fragment INT: '0' | [1-9] [0-9]*;
13 13 fragment EXP: [Ee] [+\-]? INT;
14 14  
15   -CHAINE : '"' [a-zA-Z0-9_!@#$%^&*()\-+=/.,<>;':] [a-zA-Z0-9_!@#$%^&*()\-+=/.,<>;':]* '"' ;
  15 +CHAINE : '"' [a-zA-Z0-9_!@#$%^&*()\-+=/.,<>;': ] [a-zA-Z0-9_!@#$%^&*()\-+=/.,<>;': ]* '"' ;
16 16 NUMBER : '-'? INT ('.' [0-9] +)? EXP?;
17 17  
18 18 WS : [ \t\n\r] + -> skip;
... ...
src/main/antlr4/com/ishchuk/antlr4/Log.g4 deleted
... ... @@ -1,16 +0,0 @@
1   -grammar Log;
2   -
3   -log : entry+;
4   -entry : timestamp ' ' level ' ' message CRLF;
5   -timestamp : DATE ' ' TIME;
6   -level : 'ERROR' | 'INFO' | 'DEBUG';
7   -message : (TEXT | ' ')+;
8   -
9   -fragment DIGIT : [0-9];
10   -fragment TWODIGIT : DIGIT DIGIT;
11   -fragment LETTER : [A-Za-z];
12   -
13   -DATE : TWODIGIT TWODIGIT '-' LETTER LETTER LETTER '-' TWODIGIT;
14   -TIME : TWODIGIT ':' TWODIGIT ':' TWODIGIT;
15   -TEXT : LETTER+;
16   -CRLF : '\r'? '\n' | '\r';
17 0 \ No newline at end of file
src/main/java/com/ishchuk/antlr/json/JsonListener.java
... ... @@ -2,122 +2,108 @@ package com.ishchuk.antlr.json;
2 2  
3 3 import com.ishchuk.antlr.json.model.*;
4 4 import com.ishchuk.antlr4.JSONgramBaseListener;
  5 +import com.ishchuk.antlr4.JSONgramLexer;
5 6 import com.ishchuk.antlr4.JSONgramParser;
  7 +import org.antlr.v4.runtime.CharStreams;
  8 +import org.antlr.v4.runtime.CommonTokenStream;
  9 +import org.antlr.v4.runtime.tree.ParseTreeWalker;
6 10  
7 11 import java.util.*;
8 12  
9 13  
10 14 public class JsonListener extends JSONgramBaseListener {
11 15  
12   - private JsonModel jsonFile;
13   - private Map<String, JsonElement> json;
14   - private List<String> var;
  16 + private JsonElement json;
15 17 private boolean read;
16 18  
17 19 @Override
18 20 public void enterJson(JSONgramParser.JsonContext ctx) {
19   - this.jsonFile = new JsonModel();
20   - this.json = new HashMap<>();
21   - this.var = new ArrayList<>();
22 21 this.read = true;
23 22  
24 23  
25 24 }
26 25  
27   - public JsonModel getJsonFile() {
28   - return this.jsonFile;
  26 + public JsonElement getJsonFile() {
  27 + return this.json;
29 28 }
30 29  
31   - @Override
32   - public void enterContent(JSONgramParser.ContentContext ctx) {
  30 + private JsonElement treatContext(JSONgramParser.ContentContext ctx) {
33 31  
34   - if (read && Objects.nonNull(ctx.object())) {
35   - ctx.object().keyValue().forEach(kv -> {
36   - JsonElement elem = new JsonElement();
37   - elem.setContent(kv.content());
38   - json.put(kv.CHAINE().getSymbol().getText(), elem);
39   - });
40   - read = false;
41   - } else {
42   - System.out.println("content: " + ctx.getText());
43   - String parent = getParent(ctx);
44   - System.out.println("parent: " + parent);
45   -
46   -
47   - if (json.containsKey(parent)) {
48   - System.out.println("parent in json");
49   - searchMap(json, parent);
50   -
51   - } else {
52   - System.out.println("NOT in json");
53   -
54   - json.values().forEach(kv -> {
55   - if (kv instanceof JsonObject) {
56   - searchMap(((JsonObject) kv).getMap(), parent);
57   - } else if (kv instanceof JsonArray) {
58   - ((JsonArray) kv).getArray().forEach(obj -> {
59   - searchMap(obj.getMap(), parent);
60   - });
61   - }
62   - });
  32 + JsonElement elem;
  33 + if (Objects.nonNull(ctx.object())) {
  34 + elem = new JsonObject();
  35 + for (JSONgramParser.KeyValueContext kv : ctx.object().keyValue()) {
  36 + ((JsonObject)elem).getMap().put(kv.CHAINE().getSymbol().getText(), treatContext(kv.content()));
63 37 }
  38 +
  39 + } else if (Objects.nonNull(ctx.table())) {
  40 + elem = new JsonArray();
  41 + for (JSONgramParser.ContentContext ct : ctx.table().content()) {
  42 + ((JsonArray)elem).getArray().add(treatContext(ct));
  43 + }
  44 + } else {
  45 + elem = new JsonPrimitive();
  46 + ((JsonPrimitive)elem).setValue(ctx.getText());
64 47 }
  48 +
  49 + return elem;
65 50 }
66 51  
67   - private void searchMap(Map<String, JsonElement> json, final String parent) {
68   - if (json.containsKey(parent)) {
69   - System.out.println("parent in json");
70   - if (json.get(parent).getContent() instanceof JSONgramParser.ContentContext) {
71   - JSONgramParser.ContentContext content =
72   - (JSONgramParser.ContentContext) json.get(parent).getContent();
73   - if (Objects.nonNull(content.object())) {
74   - json.replace(parent, prepareJsonObject(content));
75   - } else if (Objects.nonNull(content.table())) {
76   - JsonArray arr = new JsonArray();
77   - content.table().content().forEach(cont -> {
78   - JsonObject obj = prepareJsonObject(cont);
79   - arr.getArray().add(obj);
80   - });
81   - arr.getArray().forEach(temp -> {
82   - searchMap(temp.getMap(), parent);
83   - });
84   - json.replace(parent, arr);
85   - } else {
86   - JsonPrimitive prim = new JsonPrimitive();
87   - prim.setValue(content.getText());
88   - json.replace(parent, prim);
89   - }
  52 + @Override
  53 + public void enterContent(JSONgramParser.ContentContext ctx) {
  54 +
  55 + if (read) {
  56 + if (Objects.nonNull(ctx.object())) {
  57 + this.json = new JsonObject();
  58 + ctx.object().keyValue().forEach(kv -> {
  59 + ((JsonObject)this.json).getMap().put(kv.CHAINE().getSymbol().getText(),
  60 + treatContext(kv.content()));
  61 + });
  62 + } else if (Objects.nonNull(ctx.table())) {
  63 + this.json = new JsonArray();
  64 + ctx.table().content().forEach(ct -> {
  65 + ((JsonArray)this.json).getArray().add(treatContext(ct));
  66 + });
90 67 }
  68 +
  69 + read = false;
  70 +
91 71 }
92 72 }
93 73  
94   - private JsonObject prepareJsonObject(JSONgramParser.ContentContext cont) {
95   - JsonObject obj = new JsonObject();
96   - cont.object().keyValue().forEach(kv -> {
97   - JsonElement elem = new JsonElement();
98   - elem.setContent(kv.content());
99   - obj.getMap().put(kv.CHAINE().getSymbol().getText(), elem);
100   - });
101   - return obj;
  74 + public String convertToYml() {
  75 + StringBuilder builder = new StringBuilder();
  76 + builder.append("---\n");
  77 + if (Objects.nonNull(this.json)) {
  78 + builder.append(this.json.toYml(""));
  79 + }
  80 + return builder.toString();
102 81 }
103 82  
  83 + public static void main(String[] args) {
  84 + System.out.println("Hello! With this application you can convert JSON file to YML file");
  85 + System.out.println("------------------------------------------------------------------");
104 86  
105   - private String getParent(JSONgramParser.ContentContext ctx) {
106   - String parent;
107   - if (Objects.nonNull(ctx.table()) || Objects.nonNull(ctx.object())) {
108   - if (ctx.getParent().children.get(0).getText().equals("[")) {
109   - parent = ctx.getParent().getParent().getParent().children.get(0).getText();
110   - } else {
111   - parent = ctx.getParent().children.get(0).getText();
112   - }
113   - } else {
114   - parent = ctx.getParent().children.get(0).getText();
  87 + Scanner in = new Scanner(System.in);
  88 + System.out.println("Please, copy your json file here");
  89 + String jsonString = "";
  90 + while(in.hasNextLine()){
  91 + jsonString += in.nextLine();
115 92 }
116   - return parent;
117   - }
118 93  
  94 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(jsonString));
  95 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  96 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  97 + ParseTreeWalker walker = new ParseTreeWalker();
  98 + JsonListener jsonWalker = new JsonListener();
  99 + walker.walk(jsonWalker, JSONgramParser.json());
119 100  
  101 + System.out.println("\nYour file:\n");
  102 + System.out.println(jsonWalker.getJsonFile().toString());
120 103  
  104 + System.out.println("\nYml file:\n");
  105 + System.out.println(jsonWalker.convertToYml());
121 106  
  107 + }
122 108  
123 109 }
... ...
src/main/java/com/ishchuk/antlr/json/model/JsonArray.java
... ... @@ -5,17 +5,48 @@ import java.util.List;
5 5  
6 6 public class JsonArray extends JsonElement {
7 7  
8   - private List<JsonObject> array;
  8 + private List<JsonElement> array;
9 9  
10   - public List<JsonObject> getArray() {
  10 + public List<JsonElement> getArray() {
11 11 return array;
12 12 }
13 13  
14   - public void setArray(List<JsonObject> array) {
15   - this.array = array;
16   - }
17   -
18 14 public JsonArray() {
19 15 this.array = new ArrayList<>();
20 16 }
  17 +
  18 + public String toString() {
  19 + StringBuilder builder = new StringBuilder();
  20 + builder.append("[");
  21 + this.array.forEach(obj ->{
  22 + builder.append(obj.toString()).append(",");
  23 + });
  24 + int lastIndex = builder.lastIndexOf(",");
  25 + if (lastIndex > 0) {
  26 + builder.deleteCharAt(lastIndex);
  27 + }
  28 + builder.append("]");
  29 + return builder.toString();
  30 + }
  31 +
  32 + @Override
  33 + public String toYml(String delay) {
  34 + StringBuilder builder = new StringBuilder();
  35 + this.array.forEach(obj ->{
  36 + builder.append(delay).append("- ");
  37 + if (obj instanceof JsonPrimitive) {
  38 + builder.append(obj.toString()).append("\n");
  39 + } else {
  40 + builder.append(obj.toYml(""));
  41 + }
  42 + });
  43 +
  44 + return builder.toString();
  45 + }
  46 +
  47 + public static void main() {
  48 + System.out.println("Hello! With this application you can parse a JSON file and convert it to YAML");
  49 + System.out.println("-----------------------------------------------------------------------------");
  50 + System.out.println("Put ");
  51 + }
21 52 }
... ...
src/main/java/com/ishchuk/antlr/json/model/JsonElement.java
... ... @@ -10,4 +10,8 @@ public class JsonElement {
10 10 public void setContent(Object content) {
11 11 this.content = content;
12 12 }
  13 +
  14 + public String toYml(String delay) {
  15 + return "";
  16 + }
13 17 }
... ...
src/main/java/com/ishchuk/antlr/json/model/JsonModel.java deleted
... ... @@ -1,22 +0,0 @@
1   -package com.ishchuk.antlr.json.model;
2   -
3   -import java.util.ArrayList;
4   -import java.util.List;
5   -
6   -public class JsonModel {
7   -
8   - private List<Object> list;
9   -
10   - public List<Object> getList() {
11   - return list;
12   - }
13   -
14   - public void setList(List<Object> list) {
15   - this.list = list;
16   - }
17   -
18   - public JsonModel() {
19   - this.list = new ArrayList<>();
20   - }
21   -
22   -}
src/main/java/com/ishchuk/antlr/json/model/JsonObject.java
... ... @@ -2,6 +2,7 @@ package com.ishchuk.antlr.json.model;
2 2  
3 3 import java.util.HashMap;
4 4 import java.util.Map;
  5 +import java.util.Set;
5 6  
6 7 public class JsonObject extends JsonElement {
7 8 private Map<String, JsonElement> map;
... ... @@ -17,4 +18,36 @@ public class JsonObject extends JsonElement {
17 18 public JsonObject() {
18 19 this.map = new HashMap<>();
19 20 }
  21 +
  22 + public String toString() {
  23 + StringBuilder builder = new StringBuilder();
  24 + builder.append("{");
  25 + this.map.keySet().forEach(key -> {
  26 + builder.append(key)
  27 + .append(":")
  28 + .append(map.get(key).toString())
  29 + .append(",");
  30 + });
  31 + int lastInd = builder.lastIndexOf(",");
  32 + if (lastInd > 0) {
  33 + builder.deleteCharAt(lastInd);
  34 + }
  35 + builder.append("}");
  36 +
  37 + return builder.toString();
  38 + }
  39 +
  40 + @Override
  41 + public String toYml(String delay) {
  42 + StringBuilder builder = new StringBuilder();
  43 + this.map.keySet().forEach(key -> {
  44 + builder.append(delay).append(key).append(": ");
  45 + if (this.map.get(key) instanceof JsonPrimitive) {
  46 + builder.append(this.map.get(key).toString()).append("\n");
  47 + } else {
  48 + builder.append("\n").append(this.map.get(key).toYml(delay + " "));
  49 + }
  50 + });
  51 + return builder.toString();
  52 + }
20 53 }
... ...
src/main/java/com/ishchuk/antlr/json/model/JsonPrimitive.java
... ... @@ -12,4 +12,13 @@ public class JsonPrimitive extends JsonElement {
12 12 this.value = value;
13 13 }
14 14  
  15 + public String toString() {
  16 + return this.value;
  17 + }
  18 +
  19 + @Override
  20 + public String toYml(String delay) {
  21 + return delay + this.value;
  22 + }
  23 +
15 24 }
... ...
src/main/java/com/ishchuk/antlr/json/model/intermediate/Intermediate.java deleted
... ... @@ -1,23 +0,0 @@
1   -package com.ishchuk.antlr.json.model.intermediate;
2   -
3   -public class Intermediate {
4   - private boolean wait;
5   - private String name;
6   -
7   - public boolean isWait() {
8   - return wait;
9   - }
10   -
11   - public void setWait(boolean wait) {
12   - this.wait = wait;
13   - }
14   -
15   - public String getName() {
16   - return name;
17   - }
18   -
19   - public void setName(String name) {
20   - this.name = name;
21   - }
22   -}
23   -
src/main/java/com/ishchuk/antlr/log/LogListener.java deleted
... ... @@ -1,51 +0,0 @@
1   -package com.ishchuk.antlr.log;
2   -
3   -import com.ishchuk.antlr4.LogBaseListener;
4   -import com.ishchuk.antlr4.LogParser;
5   -import com.ishchuk.antlr.log.model.LogLevel;
6   -import com.ishchuk.antlr.log.model.LogEntry;
7   -
8   -import java.time.LocalDateTime;
9   -import java.time.format.DateTimeFormatter;
10   -import java.util.ArrayList;
11   -import java.util.Collections;
12   -import java.util.List;
13   -import java.util.Locale;
14   -
15   -public class LogListener extends LogBaseListener {
16   -
17   - private static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER
18   - = DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
19   -
20   - private List<LogEntry> entries = new ArrayList<>();
21   - private LogEntry currentLogEntry;
22   -
23   - @Override
24   - public void enterEntry(LogParser.EntryContext ctx) {
25   - this.currentLogEntry = new LogEntry();
26   - }
27   -
28   - @Override
29   - public void exitEntry(LogParser.EntryContext ctx) {
30   - entries.add(currentLogEntry);
31   - }
32   -
33   - @Override
34   - public void enterTimestamp(LogParser.TimestampContext ctx) {
35   - currentLogEntry.setTimestamp(LocalDateTime.parse(ctx.getText(), DEFAULT_DATETIME_FORMATTER));
36   - }
37   -
38   - @Override
39   - public void enterMessage(LogParser.MessageContext ctx) {
40   - currentLogEntry.setMessage(ctx.getText());
41   - }
42   -
43   - @Override
44   - public void enterLevel(LogParser.LevelContext ctx) {
45   - currentLogEntry.setLevel(LogLevel.valueOf(ctx.getText()));
46   - }
47   -
48   - public List<LogEntry> getEntries() {
49   - return Collections.unmodifiableList(entries);
50   - }
51   -}
52 0 \ No newline at end of file
src/main/java/com/ishchuk/antlr/log/model/LogEntry.java deleted
... ... @@ -1,35 +0,0 @@
1   -package com.ishchuk.antlr.log.model;
2   -
3   -import java.time.LocalDateTime;
4   -
5   -
6   -public class LogEntry {
7   -
8   - private LogLevel level;
9   - private String message;
10   - private LocalDateTime timestamp;
11   -
12   - public LogLevel getLevel() {
13   - return level;
14   - }
15   -
16   - public void setLevel(LogLevel level) {
17   - this.level = level;
18   - }
19   -
20   - public String getMessage() {
21   - return message;
22   - }
23   -
24   - public void setMessage(String message) {
25   - this.message = message;
26   - }
27   -
28   - public LocalDateTime getTimestamp() {
29   - return timestamp;
30   - }
31   -
32   - public void setTimestamp(LocalDateTime timestamp) {
33   - this.timestamp = timestamp;
34   - }
35   -}
src/main/java/com/ishchuk/antlr/log/model/LogLevel.java deleted
... ... @@ -1,5 +0,0 @@
1   -package com.ishchuk.antlr.log.model;
2   -
3   -public enum LogLevel {
4   - DEBUG, INFO, ERROR
5   -}
src/test/java/com/ishchuk/antlr/JsonParserUnitTest.java
... ... @@ -11,28 +11,234 @@ import org.antlr.v4.runtime.tree.ParseTreeWalker;
11 11 import org.junit.Test;
12 12  
13 13  
14   -
  14 +import static junit.framework.TestCase.assertTrue;
15 15 import static org.hamcrest.CoreMatchers.is;
16 16 import static org.hamcrest.MatcherAssert.assertThat;
17 17  
18 18 public class JsonParserUnitTest {
19 19  
20 20 @Test
21   - public void whenJsonObjectWithString_thenStringIsDetected() throws Exception {
22   - // String line = "{\"menu\": \"light134-()%4praer---'$\"}";
23   - String line = "{\n" + "\"line\": \"contentLine\"," +
24   - "\"table\":[{\"lineInTable\":\"contentLineIntTable\"},{}]," +
25   - " \"menu\": {\n" +
26   - " \"id\": \"file\",\n" +
27   - " \"value\": \"File\",\n" +
28   - " \"popup\": {\n" +
29   - " \"menuitem\": [\n" +
30   - " { \"value\": \"New\", \"onclick\": \"CreateNewDoc()\" },\n" +
31   - " { \"value\": \"Open\", \"onclick\": \"OpenDoc()\" },\n" +
32   - " { \"value\": \"Close\", \"onclick\": \"CloseDoc()\" }\n" +
33   - " ]\n" +
34   - " }\n" +
35   - " }\n" +
  21 + public void whenJsonPrimitive_thenStringIsPrimitive() throws Exception {
  22 + String line = "{\"key\":\"value\"}";
  23 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  24 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  25 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  26 + ParseTreeWalker walker = new ParseTreeWalker();
  27 + JsonListener jsonWalker = new JsonListener();
  28 + walker.walk(jsonWalker, JSONgramParser.json());
  29 +
  30 + System.out.println(jsonWalker.getJsonFile().toString());
  31 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  32 +
  33 + }
  34 +
  35 +
  36 + @Test
  37 + public void whenPrimitiveBoolean_thenStringIsBoolean() throws Exception {
  38 + String line = "{\"key\":true}";
  39 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  40 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  41 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  42 + ParseTreeWalker walker = new ParseTreeWalker();
  43 + JsonListener jsonWalker = new JsonListener();
  44 + walker.walk(jsonWalker, JSONgramParser.json());
  45 +
  46 + System.out.println(jsonWalker.getJsonFile().toString());
  47 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  48 +
  49 + }
  50 +
  51 + @Test
  52 + public void testJsonFileAndValidate() throws Exception {
  53 + String line = "{\n" +
  54 + " \"_id\": \"5cec1abe96b0733b10248f9b\",\n" +
  55 + " \"index\": 1,\n" +
  56 + " \"guid\": \"cfd9fab4-d291-4974-ac2e-e5abd97a732b\",\n" +
  57 + " \"isActive\": false,\n" +
  58 + " \"balance\": \"$2,208.96\",\n" +
  59 + " \"picture\": \"http://placehold.it/32x32\",\n" +
  60 + " \"age\": 38,\n" +
  61 + " \"eyeColor\": \"blue\",\n" +
  62 + " \"name\": \"Monroe Hines\",\n" +
  63 + " \"gender\": \"male\",\n" +
  64 + " \"company\": \"ZIPAK\",\n" +
  65 + " \"email\": \"monroehines@zipak.com\",\n" +
  66 + " \"phone\": \"+1 (893) 496-3736\",\n" +
  67 + " \"address\": \"930 Broome Street, Alafaya, Nevada, 3963\",\n" +
  68 + " \"about\": \"Voluptate aliquip est aute irure cupidatat tempor pariatur occaecat sint est anim. Cupidatat minim fugiat sit fugiat dolore ad adipisicing voluptate. Amet occaecat eiusmod tempor proident minim id. Minim nulla consequat sunt sit voluptate incididunt mollit reprehenderit proident velit sunt. Id voluptate nisi non irure reprehenderit anim. Ad est excepteur minim est fugiat cillum do commodo nisi. In pariatur duis commodo ipsum consequat consectetur est sunt.\",\n" +
  69 + " \"registered\": \"2015-03-12T09:28:25 -01:00\",\n" +
  70 + " \"latitude\": -16.118259,\n" +
  71 + " \"longitude\": 85.157901,\n" +
  72 + " \"tags\": [\n" +
  73 + " \"irure\",\n" +
  74 + " \"ut\",\n" +
  75 + " \"elit\",\n" +
  76 + " \"eiusmod\",\n" +
  77 + " \"tempor\",\n" +
  78 + " \"aliqua\",\n" +
  79 + " \"dolore\"\n" +
  80 + " ],\n" +
  81 + " \"friends\": [\n" +
  82 + " {\n" +
  83 + " \"id\": 0,\n" +
  84 + " \"name\": \"Tammie West\"\n" +
  85 + " },\n" +
  86 + " {\n" +
  87 + " \"id\": 1,\n" +
  88 + " \"name\": \"Pacheco Holt\"\n" +
  89 + " },\n" +
  90 + " {\n" +
  91 + " \"id\": 2,\n" +
  92 + " \"name\": \"Houston Travis\"\n" +
  93 + " }\n" +
  94 + " ],\n" +
  95 + " \"greeting\": \"Hello, Monroe Hines! You have 3 unread messages.\",\n" +
  96 + " \"favoriteFruit\": \"apple\"\n" +
  97 + " }";
  98 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  99 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  100 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  101 + ParseTreeWalker walker = new ParseTreeWalker();
  102 + JsonListener jsonWalker = new JsonListener();
  103 + walker.walk(jsonWalker, JSONgramParser.json());
  104 +
  105 + System.out.println(jsonWalker.getJsonFile().toString());
  106 + assertTrue(!jsonWalker.getJsonFile().toString().isEmpty());
  107 +
  108 + }
  109 +
  110 + @Test
  111 + public void whenJsonObject_thenStringIsObject() throws Exception {
  112 + String line = "{\"key\":{\"key\":\"value\"}}";
  113 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  114 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  115 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  116 + ParseTreeWalker walker = new ParseTreeWalker();
  117 + JsonListener jsonWalker = new JsonListener();
  118 + walker.walk(jsonWalker, JSONgramParser.json());
  119 +
  120 + System.out.println(jsonWalker.getJsonFile().toString());
  121 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  122 +
  123 + }
  124 +
  125 + @Test
  126 + public void whenJsonTable_thenStringIsTable() throws Exception {
  127 + String line = "{\"key\":[{\"key\":\"value\"},{\"key2\":\"value2\"}]}";
  128 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  129 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  130 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  131 + ParseTreeWalker walker = new ParseTreeWalker();
  132 + JsonListener jsonWalker = new JsonListener();
  133 + walker.walk(jsonWalker, JSONgramParser.json());
  134 +
  135 + System.out.println(jsonWalker.getJsonFile().toString());
  136 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  137 +
  138 + }
  139 +
  140 + @Test
  141 + public void whenTableWithJsonObjects() throws Exception {
  142 + String line = "[{\"key\":456},{\"key1\":false},{\"key3\":\"tretret\"}]";
  143 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  144 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  145 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  146 + ParseTreeWalker walker = new ParseTreeWalker();
  147 + JsonListener jsonWalker = new JsonListener();
  148 + walker.walk(jsonWalker, JSONgramParser.json());
  149 +
  150 + System.out.println(jsonWalker.getJsonFile().toString());
  151 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  152 +
  153 + }
  154 +
  155 + @Test
  156 + public void whenPrimitiveNumber() throws Exception {
  157 + String line = "{\"key\":456}";
  158 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  159 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  160 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  161 + ParseTreeWalker walker = new ParseTreeWalker();
  162 + JsonListener jsonWalker = new JsonListener();
  163 + walker.walk(jsonWalker, JSONgramParser.json());
  164 +
  165 + System.out.println(jsonWalker.getJsonFile().toString());
  166 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  167 +
  168 + }
  169 +
  170 + @Test
  171 + public void whenPrimitiveNull() throws Exception {
  172 + String line = "{\"key\":null}";
  173 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  174 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  175 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  176 + ParseTreeWalker walker = new ParseTreeWalker();
  177 + JsonListener jsonWalker = new JsonListener();
  178 + walker.walk(jsonWalker, JSONgramParser.json());
  179 +
  180 + System.out.println(jsonWalker.getJsonFile().toString());
  181 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  182 +
  183 + }
  184 +
  185 + @Test
  186 + public void whenObjectEmpty() throws Exception {
  187 + String line = "{}";
  188 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  189 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  190 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  191 + ParseTreeWalker walker = new ParseTreeWalker();
  192 + JsonListener jsonWalker = new JsonListener();
  193 + walker.walk(jsonWalker, JSONgramParser.json());
  194 +
  195 + System.out.println(jsonWalker.getJsonFile().toString());
  196 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  197 +
  198 + }
  199 +
  200 + @Test
  201 + public void whenTableEmpty() throws Exception {
  202 + String line = "[]";
  203 + JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
  204 + CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
  205 + JSONgramParser JSONgramParser = new JSONgramParser(tokens);
  206 + ParseTreeWalker walker = new ParseTreeWalker();
  207 + JsonListener jsonWalker = new JsonListener();
  208 + walker.walk(jsonWalker, JSONgramParser.json());
  209 +
  210 + System.out.println(jsonWalker.getJsonFile().toString());
  211 + assertThat(jsonWalker.getJsonFile().toString(), is(line));
  212 +
  213 + }
  214 +
  215 + @Test
  216 + public void jsonToYml() throws Exception {
  217 + String line = "{\n" +
  218 + " \"json\": [\n" +
  219 + " \"rigid\",\n" +
  220 + " \"better for data interchange\"\n" +
  221 + " ],\n" +
  222 + " \"yaml\": [\n" +
  223 + " \"slim and flexible\",\n" +
  224 + " \"better for configuration\"\n" +
  225 + " ],\n" +
  226 + " \"object\": {\n" +
  227 + " \"key\": \"value\",\n" +
  228 + " \"array\": [\n" +
  229 + " {\n" +
  230 + " \"null_value\": null\n" +
  231 + " },\n" +
  232 + " {\n" +
  233 + " \"boolean\": true\n" +
  234 + " },\n" +
  235 + " {\n" +
  236 + " \"integer\": 1\n" +
  237 + " }\n" +
  238 + " ]\n" +
  239 + " },\n" +
  240 + " \"paragraph\": \"Blank lines denote paragraph breaks\",\n" +
  241 + " \"content\": \"Or we can autoconvert line breaks to save space\"\n" +
36 242 "}";
37 243 JSONgramLexer serverJSONgramLexer = new JSONgramLexer(CharStreams.fromString(line));
38 244 CommonTokenStream tokens = new CommonTokenStream( serverJSONgramLexer );
... ... @@ -41,9 +247,13 @@ public class JsonParserUnitTest {
41 247 JsonListener jsonWalker = new JsonListener();
42 248 walker.walk(jsonWalker, JSONgramParser.json());
43 249  
44   - System.out.println(jsonWalker.getJsonFile().getList().size());
45   - jsonWalker.getJsonFile().getList().forEach(System.out::println);
46   - assertThat(jsonWalker.getJsonFile(), is(line));
  250 + System.out.println(jsonWalker.getJsonFile().toString());
  251 + System.out.println(jsonWalker.convertToYml());
  252 + assertTrue(!jsonWalker.convertToYml().isEmpty());
47 253  
48 254 }
  255 +
  256 +
  257 +
  258 +
49 259 }
... ...
src/test/java/com/ishchuk/antlr/LogParserUnitTest.java deleted
... ... @@ -1,38 +0,0 @@
1   -package com.ishchuk.antlr;
2   -
3   -import static org.hamcrest.CoreMatchers.is;
4   -import static org.hamcrest.MatcherAssert.assertThat;
5   -
6   -import com.ishchuk.antlr.log.LogListener;
7   -import com.ishchuk.antlr.log.model.LogLevel;
8   -import com.ishchuk.antlr.log.model.LogEntry;
9   -import com.ishchuk.antlr4.LogLexer;
10   -import com.ishchuk.antlr4.LogParser;
11   -import org.antlr.v4.runtime.CharStreams;
12   -import org.antlr.v4.runtime.CommonTokenStream;
13   -import org.antlr.v4.runtime.tree.ParseTreeWalker;
14   -import org.junit.Test;
15   -
16   -import java.time.LocalDateTime;
17   -
18   -
19   -public class LogParserUnitTest {
20   -
21   - @Test
22   - public void whenLogContainsOneErrorLogEntry_thenOneErrorIsReturned() throws Exception {
23   - String logLines = "2018-May-05 14:20:21 DEBUG entering awesome method\r\n" +
24   - "2018-May-05 14:20:24 ERROR Bad thing happened\r\n";
25   - LogLexer serverLogLexer = new LogLexer(CharStreams.fromString(logLines));
26   - CommonTokenStream tokens = new CommonTokenStream( serverLogLexer );
27   - LogParser logParser = new LogParser(tokens);
28   - ParseTreeWalker walker = new ParseTreeWalker();
29   - LogListener logWalker = new LogListener();
30   - walker.walk(logWalker, logParser.log());
31   -
32   - assertThat(logWalker.getEntries().size(), is(2));
33   - LogEntry error = logWalker.getEntries().get(1);
34   - assertThat(error.getLevel(), is(LogLevel.ERROR));
35   - assertThat(error.getMessage(), is("Bad thing happened"));
36   - assertThat(error.getTimestamp(), is(LocalDateTime.of(2018,5,5,14,20,24)));
37   - }
38   -}