I'm currently trying to optimize a json parsing in java.
Is is somehow possible to parse a Json from end-to-start?
I use the faster-xml/jackson-core (2.2.3) library to parse kinds of following json objects These are in fact nested objects grouped by Year, and then followed by month. See sample below, there are years where only some months are relevant, so others are omited. The json Object is part of an website I am parsing in Java. Let's say I only want to parse the latest 5 entrys, staring by the current year (2014) descending. Problem: The json object is in ascending order, so to get the latest 5 entrys I have to get trough the whole json Object from start to end.
Is there a way to do it the other way round? Or to reoder the json object? Other ideas to solve this?
var calendarGroupYearMonth = $.parseJSON('{"2000":{"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2001":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2002":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2003":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2004":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2005":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2006":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2007":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2008":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2009":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2010":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2011":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2012":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2013":{"1":1,"2":1,"3":1,"4":1,"5":1,"6":1,"7":1,"8":1,"9":1,"10":1,"11":1,"12":1},"2014":{"1":1,"2":1,"3":1}}');
I don't want it as an object strucutre, so I am parsing it token by token.
Here is my json parsing code which works fine.
JsonParser parser = jf.createParser(jsonSTring);
JsonToken currentToken = parser.nextToken();
String month = "";
String year = "";
final int YEAR_LENGTH = 4;
while (parser.hasCurrentToken()) {
if (currentToken == JsonToken.FIELD_NAME) {
String text = parser.getText();
if (text.length() == YEAR_LENGTH) {
year = text;
} else {
month = text;
}
//Do some stuff with the year and month
}
currentToken = parser.nextToken();
You can simply do it using @JsonAnySetter
annotation. Your POJO
class should look like this:
class Pojo {
private static final int MAX_SIZE = 5;
private TreeMap<Integer, Map<String, String>> yearsMap = new TreeMap<>(new Comparator<Integer>() {
@Override
public int compare(Integer value1, Integer value2) {
return value2.compareTo(value1);
};
});
@JsonAnySetter
public void setMonthFromLastYear(String year, Map<String, String> value) {
Integer currentYear = Integer.valueOf(year);
if (yearsMap.size() <= MAX_SIZE) {
yearsMap.put(currentYear, value);
} else if (currentYear > yearsMap.lastKey()) {
yearsMap.remove(yearsMap.lastKey());
yearsMap.put(currentYear, value);
}
}
public Map<String, String> getValuesForYear(int year) {
return yearsMap.get(year);
}
@Override
public String toString() {
return String.valueOf(yearsMap);
}
}
Simple usage:
ObjectMapper om = new ObjectMapper();
Pojo demoClass = om.readValue(json, Pojo.class);
System.out.println(demoClass);
System.out.println("Year 2014: " + demoClass.getValuesForYear(2014));
Above program prints:
{2014={1=1, 2=1, 3=1}, 2013={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2012={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2011={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2010={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}, 2009={1=1, 2=1, 3=1, 4=1, 5=1, 6=1, 7=1, 8=1, 9=1, 10=1, 11=1, 12=1}}
Year 2014: {1=1, 2=1, 3=1}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments