0

I have to encode gpstracks to json. They are stored in a mySql database, but getting them out auf there is quick, ~400ms. Encoding to json takes me 10s now, which is very ugly.

List<series> Tracks = new ArrayList<series>();
...
@Override
public String toString(){
    JsonObjectBuilder outerbuilder = Json.createObjectBuilder();
    //System.out.println("   TrackList::toString() 1: "+ZonedDateTime.now().toString());
    for (series s : Tracks){
        outerbuilder.add(s.Name, s.toArray());  
    }
    //System.out.println("   TrackList::toString() 1: "+ZonedDateTime.now().toString()); ~10s
    return outerbuilder.build().toString();
}

public class series {
    public JsonArray toArray(){
        JsonArrayBuilder array = Json.createArrayBuilder();
        for (sample s : samples){
            JsonArrayBuilder arrI = Json.createArrayBuilder(); //Building so many arrays has bad performance ~10s
            arrI.add(s.getLat());
            arrI.add(s.getLon());
            array.add(arrI.build());
            //array.add("["+s.getLat()+","+s.getLon()+"]"); // fast ~1s
            //{"854":["[47.6,11.8]","[47.6,11.8]"
            //is wrong should be:
            //{"854":[[47.6,11.8],[47.6,11.8],[4
        }
        return array.build();
    }
}

I would like to improve the performance. I tried to build a string directly, that takes 1s. Why is jsonbuilder so much slower and how can I use it right?

(Building a string directly would be possible of course, but I would like to use jsonbuilder if possible.

rjdkolb
  • 10,377
  • 11
  • 69
  • 89
matmuc
  • 11
  • 1
  • 2
  • It's best to use a profiler to figure out why it is so slow and what exactly is causing the slowness. – rjdkolb Mar 17 '18 at 13:33
  • Maybe that could help you: https://stackoverflow.com/questions/31101951/performance-issue-in-converting-java-object-to-json-object – Sim Mar 17 '18 at 13:39

0 Answers0