diff --git a/src/JsonInspector/GraphicFile.java b/src/JsonInspector/GraphicFile.java index 5d62238..6437633 100644 --- a/src/JsonInspector/GraphicFile.java +++ b/src/JsonInspector/GraphicFile.java @@ -31,21 +31,21 @@ public class GraphicFile extends JPanel { } - private void createFileRecursive(Node node, int depth, boolean inList) { + private void createFileRecursive(Node node, int depth, boolean virgule) { String indentation = ""; for (int i = 0; i < depth; i++) { indentation += Parameters.IHM_INDENTATION; } - if (node.getType() == Type.OBJECT) { - createObject(node, depth, indentation); + if (node.getType() == Type.OBJECT || node.getType() == Type.ELEMENT) { + createObjectElement(node, depth, indentation, virgule); } else if (node.getType() == Type.ARRAY) { - createArray(node, depth, indentation); + createArray(node, depth, indentation, virgule); } else if (node.getType() == Type.PAIR) { - createPair(node, depth, indentation, inList); + createPair(node, depth, indentation, virgule); } } @@ -57,7 +57,7 @@ public class GraphicFile extends JPanel { line.add(": "); if (node.getSize() != 0) { - line.add("\"" + node.get(0) + "\"", Parameters.STRING_COLOR); + createValue(line, node.get(0)); } else { line.add("null" , Parameters.OTHER_COLOR); } @@ -70,52 +70,93 @@ public class GraphicFile extends JPanel { } - private void createObject(Node node, int depth, String indentation) { + private void createObjectElement(Node node, int depth, String indentation, boolean virgule) { Line line = new Line(node, depth); line.add(indentation); - if (node.getName().equals("")) { + if (node.getType() == Type.ELEMENT) { line.add("{"); } else { line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR); line.add(": {"); } - lines.add(line); - callNextNodes(node, depth); - lines.add(new Line(node, indentation + "}", depth)); + if (node.getSize() == 0) { + if (virgule) { + line.add(" },"); + } else { + line.add(" }"); + } + lines.add(line); + + } else { + lines.add(line); + callNextNodes(node, depth); + Line endLine = new Line(node, indentation + "}", depth); + + if (virgule) { + endLine.add(","); + } + + lines.add(endLine); + } } - private void createArray(Node node, int depth, String indentation) { + private void createArray(Node node, int depth, String indentation, boolean virgule) { Line line = new Line(node, depth); line.add(indentation); line.add("\"" + node.getName() + "\"", Parameters.KEY_COLOR); line.add(": ["); - lines.add(line); - - for (int i = 0; i < node.getSize(); i++) { - Line valueLine = new Line(node, depth + 1); - - if (node.get(i).isObjectOrArray()) { - callNextNodes(node, depth); + if (node.getSize() == 0) { + if (virgule) { + line.add(" ],"); } else { - valueLine.add( - indentation + Parameters.IHM_INDENTATION + "\"" + node.get(i).getValue() + "\"", - Parameters.STRING_COLOR - ); + line.add(" ]"); + } + lines.add(line); + } else { + lines.add(line); + + for (int i = 0; i < node.getSize(); i++) { + Line valueLine = new Line(node, depth + 1); + + if (node.get(i).isNode()) { + callNextNodes(node, depth); + } else { + String valueString = indentation + Parameters.IHM_INDENTATION; + valueLine.add(valueString, Parameters.STRING_COLOR); + createValue(valueLine, node.get(i)); + + if (i != node.getSize() - 1) { + valueLine.add(","); + } + } + + lines.add(valueLine); } - if (i != node.getSize() - 1) { - valueLine.add(","); + Line endLine = new Line(node, indentation + "]", depth); + + if (virgule) { + endLine.add(","); } - lines.add(valueLine); + lines.add(endLine); } + } - lines.add(new Line(node, indentation + "]", depth)); + + private void createValue(Line line, Value value) { + if (value.isNumber()) { + line.add("" + value.getValue(), Parameters.NUMBER_COLOR); + } else if (value.isString()) { + line.add("\"" + value.getValue() + "\"", Parameters.STRING_COLOR); + } else { + line.add("" + value.getValue(), Parameters.OTHER_COLOR); + } } @@ -191,13 +232,17 @@ public class GraphicFile extends JPanel { private Node createTree() { - Node beginning = new Node("", Type.OBJECT); + Node beginning = new Node("", Type.ELEMENT); Node d = new Node("d", Type.OBJECT); Node result = new Node("result", Type.ARRAY); Node metadata = new Node("", Type.ELEMENT); Node type = new Node("type", Type.PAIR); Node e = new Node("e", Type.ARRAY); - String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35"; + Node key = new Node("key", Type.PAIR); + Node key2 = new Node("key2", Type.PAIR); + Node object = new Node("object", Type.OBJECT); + Integer number = 12; + String value1 = "EmployeeDetails.Employe", value2 = "E12012", value3 = "35", value4 = "string"; beginning.add(d); beginning.add(e); @@ -207,6 +252,11 @@ public class GraphicFile extends JPanel { result.add(value1); result.add(value2); result.add(value3); + key.add(number); + beginning.add(key); + beginning.add(key2); + beginning.add(object); + key2.add(value4); System.out.println(Tree.printTree(beginning, 0)); diff --git a/src/JsonInspector/Tree.java b/src/JsonInspector/Tree.java index 91f02eb..47d7003 100644 --- a/src/JsonInspector/Tree.java +++ b/src/JsonInspector/Tree.java @@ -3,7 +3,7 @@ package JsonInspector; import java.util.ArrayList; public class Tree { - private Node firstNode; + private final Node firstNode; public Tree(String file) { @@ -88,11 +88,11 @@ public class Tree { } - if (node.getType() == Type.OBJECT){ - line += printObject(node, depth, indentation); + if (node.getType() == Type.OBJECT || node.getType() == Type.ELEMENT) { + line += printObjectElement(node, depth, indentation); } else if (node.getType() == Type.PAIR){ - line += printPair(node, depth, indentation); + line += printPair(node); } else if (node.getType() == Type.ARRAY){ line += printArray(node, depth, indentation); @@ -109,17 +109,26 @@ public class Tree { if (node.get(i).isNode()) { line += "\n" + printTree((Node) node.get(i).getValue(), depth + 1); } + + if (i != node.getSize() - 1) { + line += ","; + } } return line; } - private static String printPair(Node node, int depth, String indentation) { + private static String printPair(Node node) { String line = ""; if (node.getSize() != 0) { - line += ": " + node.get(0); + if (node.get(0).isString()) { + line += ": \"" + node.get(0).getValue() + "\""; + } else { + line += ": " + node.get(0).getValue(); + } + } else { line += ": null"; } @@ -128,17 +137,21 @@ public class Tree { } - private static String printObject(Node node, int depth, String indentation) { + private static String printObjectElement(Node node, int depth, String indentation) { String line = ""; - if (node.getName().equals("")) { + if (node.getType() == Type.ELEMENT) { line += "{"; } else { line += ": {"; } - line += callNextNodes(node, depth); - line += "\n" + indentation + "}"; + if (node.getSize() == 0) { + line += "}"; + } else { + line += callNextNodes(node, depth); + line += "\n" + indentation + "}"; + } return line; } @@ -146,20 +159,31 @@ public class Tree { private static String printArray(Node node, int depth, String indentation) { String line = ""; + line += ": ["; - line += ": [\n"; + if (node.getSize() == 0) { + line += "]"; - for (int i = 0; i < node.getSize(); i++) { - //si la valeur a l'indice i est la dernière alors pas de virgule - if (i != node.getSize()-1) { - line += indentation + Parameters.CONSOLE_INDENTATION + node.get(i).getValue() + ",\n"; - } else { - line += indentation + Parameters.CONSOLE_INDENTATION + node.get(i).getValue(); + } else { + for (int i = 0; i < node.getSize(); i++) { + if (node.get(i).isNode()) { + line += "\n" + printTree((Node) node.get(i).getValue(), depth + 1); + } else { + line += indentation + Parameters.CONSOLE_INDENTATION; + if (node.get(i).isString()) { + line += "\"" + node.get(i).getValue() + "\""; + } else { + line += node.get(i).getValue(); + } + } + + if (i != node.getSize() - 1) { + line += ",\n"; + } } - } - line += callNextNodes(node, depth); - line += "\n" + indentation + "]"; + line += "\n" + indentation + "]"; + } return line; }