From 80d7675059f87a5f67fe4a89789fcd1a2a07bdc2 Mon Sep 17 00:00:00 2001 From: coolneng Date: Tue, 15 Dec 2020 12:12:30 +0100 Subject: [PATCH] Implement JSON file parser --- src/main/java/org/RI/P2/Parser.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/RI/P2/Parser.java b/src/main/java/org/RI/P2/Parser.java index 642b190..ba5afca 100644 --- a/src/main/java/org/RI/P2/Parser.java +++ b/src/main/java/org/RI/P2/Parser.java @@ -2,12 +2,19 @@ package org.RI.P2; import java.util.List; import java.io.IOException; +import java.io.Reader; +import java.io.InputStream; +import java.io.InputStreamReader; import java.io.File; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.stream.Collectors; +import org.json.simple.JSONArray; +import org.json.simple.JSONValue; +import org.json.simple.parser.ParseException; + public class Parser { private static List files; @@ -16,17 +23,17 @@ public class Parser { .collect(Collectors.toList()); } + public JSONArray parseJSONFile(String filePath) throws IOException, ParseException { + InputStream jsonFile = getClass().getResourceAsStream(filePath); + Reader readerJson = new InputStreamReader(jsonFile); + Object fileObjects = JSONValue.parseWithException(readerJson); + JSONArray arrayObjects = (JSONArray) fileObjects; + return arrayObjects; + } + private static void usage() { System.out.println("Usage: Parser "); System.out.println("option directory: directory that contains JSON files"); System.exit(1); } - - public static void main(String[] args) throws IOException { - if (args.length != 1) { - usage(); - } - String directory = args[0]; - readFiles(directory); - } }