0

this is my first time building a Jersey Restful web service.

The project is supposed to be a server that provides information about Reddit posts. For now, this information is stored in a large JSON file, and because it is not supposed to be manipulated, my idea is to store this information in the dao class in form of Post instances.

here is my projects' folder organisation: filesView

So my idea is to populate the dao class by reading the json file something like this:

public enum PostDao {
    instance;
    
    private Map<String, Post> posts = new HashMap<String, Post>();

    private PostDao() {
        //JSON parser object to parse read file
        System.out.println("init");
        JSONParser jsonParser = new JSONParser();
         
        try {
            
            FileReader reader = new FileReader("src/main/resources/data/posts.json");
            //Read JSON file
            Object obj = jsonParser.parse(reader);
 
            JSONArray postsArr = (JSONArray) obj;

            for (Object p : postsArr) {
                JSONObject post = (JSONObject) p;
                Post pobj = new Post(post.get("title"), post.get("author"));
                posts.put(pobj.getId(), pobj);
            }
 
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    
    public Map<String, Post> getModel() {
        return posts;
    }

The problem is that I do not know where to put the json file and what the path would be.

Is there a standard folder and path to store this kind of files? Or is there a different way to populate the dao class?

  • Don't read from src. This folder will not be available when you build. Make the file and embedded resource and read it with [`getResourceAsStream()`](https://stackoverflow.com/q/1464291/2587435). You path will be `/data/posts.json`. You can get a `Reader` by passing the `InputStream` to the `InputStreamReader(stream)` constructor. – Paul Samsotha Jun 05 '21 at 01:47

1 Answers1

0

I managed to find a solution which was pretty simple:

JSONParser jsonParser = new JSONParser();
     
    try {
        //get JSON file's path
        InputStream in = this.getClass().getResourceAsStream("/data/posts.json");
        BufferedReader br = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8));
        
        //Read JSON file
        Object obj = jsonParser.parse(br.readLine());
        
        for (Object p : (JSONArray) obj) {
            JSONObject post = (JSONObject) p;
            
            //add post to mapping
            this.addPost(new Post(generateId(), 
                    (String)post.get("user"), 
                    (String)post.get("title"), 
                    (String)post.get("date")));
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

Note: my json file consisted of only one line (as text file) so that is why BufferedReader.readLine() was enough