Transférer les fichiers vers 'src/JsonInspector'

This commit is contained in:
Romain BESSON 2023-01-15 18:26:04 +01:00
parent d8e3440fb7
commit 9fe24c2f98
3 changed files with 127 additions and 1 deletions

View File

@ -0,0 +1,86 @@
package JsonInspector;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Main {
public static void main(String[] args) {
if (args.length == 0) {
new Frame();
} else {
try {
URL url = new URL(args[0]);
String file = getJsonInOneLine(url);
System.out.println(new Tree(file));
} catch (MalformedURLException e) {
System.err.println("Invalid URL");
System.exit(1);
} catch (JsonSyntaxException j) {
System.err.println("Invalid syntax in file");
System.exit(1);
}
}
}
public static String getJsonInOneLine(URL f) {
StringBuilder sb = new StringBuilder();
try {
BufferedReader buff = new BufferedReader(new InputStreamReader(f.openStream()));
String line;
while ((line = buff.readLine()) != null) {
sb.append(line);
}
buff.close();
} catch (IOException e) {
System.err.println("[!] Probleme lors de l'ouverture du fichier");
}
return sb.toString();
}
public static String cleanFile(String file) {
char[] chars = file.toCharArray();
boolean inString = false, inValue = false;
String cleanedFile = "";
for (char currentChar : chars) {
if (!inString && !inValue) {
if (currentChar == '"') {
inString = true;
cleanedFile += currentChar;
} else if (isJsonSyntax(currentChar)) {
cleanedFile += currentChar;
}
} else if (currentChar == '"') {
inString = false;
cleanedFile += currentChar;
} else if (currentChar == ':') {
inValue = true;
cleanedFile += currentChar;
} else {
cleanedFile += currentChar;
}
}
return cleanedFile;
}
private static boolean isJsonSyntax(char character) {
boolean openingArray = character == '[', closingArray = character == ']';
boolean openingObject = character == '{', closingObject = character == '}';
boolean virgule = character == ',', deuxPoints = character == ':';
if (openingArray||closingArray||openingObject||closingObject||virgule||deuxPoints) {
return true;
}
return false;
}
}

View File

@ -20,8 +20,32 @@ public class Node {
} }
private void findType(String value) {
if (value.charAt(0) == '"' && value.charAt(value.length()-1) == '"') {
values.add(new Value<String>(Tree.cleanOpeningExpression(value)));
return;
}
try {
values.add(new Value<Integer>(Integer.parseInt(value)));
} catch (NumberFormatException nfeInt) {
try {
values.add(new Value<Double>(Double.parseDouble(value)));
} catch (NumberFormatException nfeDouble) {
values.add(new Value<Other>(new Other(value)));
}
}
}
public <T> void add(T value) { public <T> void add(T value) {
if (value != null) {
if (value.getClass().equals(Node.class)) {
values.add(new Value<T>(value)); values.add(new Value<T>(value));
} else {
findType((String) value);
}
}
} }

View File

@ -0,0 +1,16 @@
package JsonInspector;
public class Other {
String value;
public Other(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}