Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read the JSON file in the Jersey sources folder

I have Jersey project glassfish 2.18 and I am trying to read the JSON file routes.txt in this folder scr-main-sources with the Jackson 2.x . How can I get The InputStream of the text document file to get the data from it?

I appreciate any help

Code:

    FileReader fileReader = new FileReader("src/main/sources/routes.txt");
    ObjectMapper mapper = new ObjectMapper();
    Root readValue = mapper.readValue(fileReader, Root.class);

JSON simple:

[{
   "route": 1,
   "info": {
              "stops": [{
                          "arrival_time": {"mon-fri": ["04:24","05:10","05:40"],
                                       "sat": ["05:34","05:55","06:15"],
                                       "son": ["07:00","08:00","05:40"]

                                       },
                          "stops_name": "Tension Way"
                        }],
              "direction": "Surrey Quays"
           }
}]

Root class:

package org.busTracker.serverSide.json;

import java.util.HashMap;
import java.util.Map;
import javax.annotation.Generated;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Generated("org.jsonschema2pojo")
@JsonPropertyOrder({
"route",
"info"
})
public class Root {

@JsonProperty("route")
private Integer route;
@JsonProperty("info")
private Info info;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

/**
* 
* @return
* The route
*/
@JsonProperty("route")
public Integer getRoute() {
return route;
}

/**
* 
* @param route
* The route
*/
@JsonProperty("route")
public void setRoute(Integer route) {
this.route = route;
}

/**
* 
* @return
* The info
*/
@JsonProperty("info")
public Info getInfo() {
return info;
}

/**
* 
* @param info
* The info
*/
@JsonProperty("info")
public void setInfo(Info info) {
this.info = info;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}

}
like image 881
Mr Asker Avatar asked Jun 16 '15 18:06

Mr Asker


1 Answers

Few things to consider.

  • Deserializing and format of the JSON. Currently your JSON is a JSON array. Unless the class is some type of collection, say a subclass of List, the class will map to a JSON object. So you can either remove the [ ] from the JSON, or deserialize into a List<Root>. For example

    List<Root> roots = mapper.readValue(is, TypeFactory.defaultInstance()
                .constructCollectionType(List.class, Root.class));
    

    Of course if you remove the [ ], then you can deserialize to Root instead of List<Root>. But maybe you are expecting to have more JSON object in the array (who knows).

  • The format of main JSON object in relation to your Root class. There seems to be a discrepancy between the format of the JSON and the properties in the class. Here is the format of the class that I see (and would be more inclined to work with):

    public class Root {
    
        public int route;
        public Info info;
    
        public static class Info {
    
            public String direction;
            public Stops stops;
    
            public static class Stops {
                @JsonProperty("arrival_time")
                public Map<String, String[]> arrivalTime = new HashMap<>();
                @JsonProperty("stops_name")
                public String stopsName;
            }
        }
    }
    
  • Accessing the file. The file should be read from classpath instead of a file on the file system. For that you can use Class.getResourceAsStream(), which returns an InputStream. So for example, if your file is in src/main/resources (Maven structure), then the file would be at the root of the classpath, and you could access it like

    InputStream is = YourClass.class.getResourceAsStream("/file.json")
    

Here is a complete example with all the points mentioned above

public class Main {

    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        InputStream is = Main.class.getResourceAsStream("/file.json");

        List<Root> roots = mapper.readValue(is, TypeFactory.defaultInstance()
                .constructCollectionType(List.class, Root.class));
        Root root = roots.get(0);

        System.out.println("route: " + root.route);
        Map<String, String[]> arrivalTimes = root.info.stops.arrivalTime;
        for (Map.Entry<String, String[]> entry: arrivalTimes.entrySet()) {
            System.out.println(entry.getKey());
            for (String time: entry.getValue()) {
                System.out.println(time);
            }
        }
    }
}
like image 109
Paul Samsotha Avatar answered Oct 25 '22 21:10

Paul Samsotha