mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
add json stuff
This commit is contained in:
parent
bf4b01e681
commit
67b8ef5a9a
1 changed files with 30 additions and 13 deletions
|
|
@ -34,6 +34,7 @@ import jsprit.core.util.Coordinate;
|
|||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
|
@ -49,9 +50,29 @@ public class VrpJsonWriter {
|
|||
this.vrp = vrp;
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
try {
|
||||
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(stringWriter);
|
||||
write(jsonGenerator);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
return stringWriter.toString();
|
||||
}
|
||||
|
||||
public void write(File jsonFile){
|
||||
try {
|
||||
JsonGenerator jsonGenerator = new JsonFactory().createGenerator(new FileOutputStream(jsonFile), JsonEncoding.UTF8);
|
||||
write(jsonGenerator);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private void write(JsonGenerator jsonGenerator) throws IOException {
|
||||
jsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());
|
||||
jsonGenerator.writeStartObject();
|
||||
jsonGenerator.writeStringField(JsonConstants.FLEET,vrp.getFleetSize().toString());
|
||||
|
|
@ -59,13 +80,8 @@ public class VrpJsonWriter {
|
|||
writeVehicleTypes(jsonGenerator);
|
||||
writeServices(jsonGenerator);
|
||||
jsonGenerator.writeEndObject();
|
||||
|
||||
jsonGenerator.flush();
|
||||
jsonGenerator.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeVehicleTypes(JsonGenerator jsonGenerator) {
|
||||
|
|
@ -212,6 +228,7 @@ public class VrpJsonWriter {
|
|||
.build();
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(service).addJob(service2)
|
||||
.addVehicle(v1).addVehicle(v2).build();
|
||||
new VrpJsonWriter(vrp).write(new File("output/vrp.json"));
|
||||
// new VrpJsonWriter(vrp).write(new File("output/vrp.json"));
|
||||
System.out.println(new VrpJsonWriter(vrp).toString());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue