Changeset 17570 in josm for trunk/src


Ignore:
Timestamp:
2021-03-16T21:56:55+01:00 (4 years ago)
Author:
simon04
Message:

GeoJSONWriter: directly write to Writer

Skip writing to intermediate StringWriter.

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/io/importexport/GeoJSONExporter.java

    r12806 r17570  
    1111
    1212import org.openstreetmap.josm.actions.ExtensionFileFilter;
     13import org.openstreetmap.josm.data.osm.DataSet;
    1314import org.openstreetmap.josm.gui.layer.Layer;
    1415import org.openstreetmap.josm.gui.layer.OsmDataLayer;
     
    3536    public void exportData(File file, Layer layer) throws IOException {
    3637        if (layer instanceof OsmDataLayer) {
     38            DataSet data = ((OsmDataLayer) layer).data;
    3739            try (Writer out = Files.newBufferedWriter(file.toPath(), StandardCharsets.UTF_8)) {
    38                 out.write(new GeoJSONWriter(((OsmDataLayer) layer).data).write());
     40                new GeoJSONWriter(data).write(true, out);
    3941            }
    4042        } else {
  • trunk/src/org/openstreetmap/josm/io/GeoJSONWriter.java

    r17320 r17570  
    44import java.io.StringReader;
    55import java.io.StringWriter;
     6import java.io.Writer;
    67import java.math.BigDecimal;
    78import java.math.RoundingMode;
     
    9697    public String write(boolean pretty) {
    9798        StringWriter stringWriter = new StringWriter();
     99        write(pretty, stringWriter);
     100        return stringWriter.toString();
     101    }
     102
     103    /**
     104     * Writes OSM data as a GeoJSON string (prettified or not).
     105     * @param pretty {@code true} to have pretty output, {@code false} otherwise
     106     * @param writer The writer used to write results
     107     */
     108    public void write(boolean pretty, Writer writer) {
    98109        Map<String, Object> config = Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, pretty);
    99         try (JsonWriter writer = Json.createWriterFactory(config).createWriter(stringWriter)) {
     110        try (JsonWriter jsonWriter = Json.createWriterFactory(config).createWriter(writer)) {
    100111            JsonObjectBuilder object = Json.createObjectBuilder()
    101112                    .add("type", "FeatureCollection")
     
    103114            appendLayerBounds(data, object);
    104115            appendLayerFeatures(data, object);
    105             writer.writeObject(object.build());
    106             return stringWriter.toString();
     116            jsonWriter.writeObject(object.build());
    107117        }
    108118    }
Note: See TracChangeset for help on using the changeset viewer.