From 630f3565c176ee837c0db155905c608335948bea Mon Sep 17 00:00:00 2001 From: besson Date: Sun, 15 Jan 2023 18:26:16 +0100 Subject: [PATCH] =?UTF-8?q?Transf=C3=A9rer=20les=20fichiers=20vers=20'src/?= =?UTF-8?q?JsonInspector'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/JsonInspector/Tree.java | 46 +++++++++++++++++++----------------- src/JsonInspector/Value.java | 5 ---- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/JsonInspector/Tree.java b/src/JsonInspector/Tree.java index 1ff9af8..1580093 100644 --- a/src/JsonInspector/Tree.java +++ b/src/JsonInspector/Tree.java @@ -6,13 +6,8 @@ public class Tree { private Node firstNode; - public Tree(String file) { - try { - firstNode = parseElement(file); - } catch (JsonSyntaxException e) { - e.printStackTrace(); - //System.err.println("Syntax error while parsing JSON file"); - } + public Tree(String file) throws JsonSyntaxException { + firstNode = parseElement(file); } @@ -22,12 +17,18 @@ public class Tree { if (keyValue.length == 2) { String key = keyValue[0], value = keyValue[1]; - if (value.charAt(0) == '[' && value.charAt(value.length()-1) == ']') { - return parseArray(key, value); - } else if (value.charAt(0) == '{' && value.charAt(value.length()-1) == '}') { - return parseObject(key, value); + if (0 < value.length()) { + if (value.charAt(0) == '[' && value.charAt(value.length() - 1) == ']') { + return parseArray(key, value); + + } else if (value.charAt(0) == '{' && value.charAt(value.length() - 1) == '}') { + return parseObject(key, value); + + } else { + return parsePair(key, value); + } } else { - return parsePair(key, value); + return null; } } else if (keyValue[0].equals("")) { @@ -45,20 +46,21 @@ public class Tree { private Node parseArray(String name, String rawValues) throws JsonSyntaxException { - Node array = new Node(name, Type.ARRAY); + Node array = new Node(cleanOpeningExpression(name), Type.ARRAY); ArrayList elements = splitList(rawValues); for (String value : elements) { + if (0 < value.length()) { + if (value.charAt(0) == '{') { + if (value.charAt(value.length() - 1) == '}') { + array.add(parseElement(value)); + } else { + throw new JsonSyntaxException(); + } - if (value.charAt(0) == '{') { - if (value.charAt(value.length()-1) == '}') { - array.add(parseElement(value)); } else { - throw new JsonSyntaxException(); + array.add(value); } - - } else { - array.add(value); } } @@ -67,7 +69,7 @@ public class Tree { private Node parseObject(String name, String rawValues) throws JsonSyntaxException { - Node object = new Node(name, Type.OBJECT); + Node object = new Node(cleanOpeningExpression(name), Type.OBJECT); ArrayList elements = splitList(rawValues); for (String value : elements) { @@ -98,7 +100,7 @@ public class Tree { } - private String cleanOpeningExpression(String strToClean) { + public static String cleanOpeningExpression(String strToClean) { StringBuilder cleanedString = new StringBuilder(strToClean); cleanedString.deleteCharAt(0); cleanedString.deleteCharAt(cleanedString.length()-1); diff --git a/src/JsonInspector/Value.java b/src/JsonInspector/Value.java index 6ef398d..c244c28 100644 --- a/src/JsonInspector/Value.java +++ b/src/JsonInspector/Value.java @@ -41,9 +41,4 @@ public class Value { public boolean isNumber() { return value.getClass().equals(Integer.class) || value.getClass().equals(Double.class); } - - - public boolean isBoolean() { - return value.getClass().equals(Boolean.class); - } }