Ignore:
Timestamp:
2018-07-08T22:49:51+02:00 (6 years ago)
Author:
Don-vip
Message:

taginfo: use embedded javax.json instead of groovy-json 2.5.0: unable to make it work with Ant anymore

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/scripts/TagInfoExtract.groovy

    r14021 r14024  
    1818
    1919import javax.imageio.ImageIO
     20import javax.json.Json
     21import javax.json.stream.JsonGenerator
    2022
    2123import org.openstreetmap.josm.Main
     
    5658
    5759import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
    58 import groovy.json.JsonBuilder
    5960
    6061class TagInfoExtract {
     
    360361    }
    361362
    362     void write_json(name, description, tags) {
    363         def json = new JsonBuilder()
    364         def project = [
    365                 name: name,
    366                 description: description,
    367                 project_url: "https://josm.openstreetmap.de/",
    368                 icon_url: "https://josm.openstreetmap.de/export/7770/josm/trunk/images/logo_16x16x8.png",
    369                 contact_name: "JOSM developer team",
    370                 contact_email: "josm-dev@openstreetmap.org",
    371         ]
    372         json data_format: 1, data_updated: DateTimeFormatter.ofPattern("yyyyMMdd'T'hhmmss'Z'").withZone(ZoneId.of("Z")).format(Instant.now()), project: project, tags: tags
     363    void write_json(String name, String description, List<Map<String, ?>> tags) {
     364        def config = [:]
     365        config[JsonGenerator.PRETTY_PRINTING] = output_file == null
     366        def writer = output_file != null ? output_file : new StringWriter()
     367        Json.createWriterFactory(config).createWriter(writer).withCloseable {json ->
     368            def project = Json.createObjectBuilder()
     369                .add("name", name)
     370                .add("description", description)
     371                .add("project_url", "https://josm.openstreetmap.de/")
     372                .add("icon_url", "https://josm.openstreetmap.de/export/7770/josm/trunk/images/logo_16x16x8.png")
     373                .add("contact_name", "JOSM developer team")
     374                .add("contact_email", "josm-dev@openstreetmap.org")
     375            def jsonTags = Json.createArrayBuilder()
     376            for (def t : tags) {
     377                def o = Json.createObjectBuilder()
     378                for (def e : t.entrySet()) {
     379                    def val = e.getValue()
     380                    if (e.getValue() instanceof List) {
     381                        def arr = Json.createArrayBuilder()
     382                        for (def v : e.getValue()) {
     383                            arr.add(v)
     384                        }
     385                        val = arr.build()
     386                    }
     387                    o.add(e.getKey(), val)
     388                }
     389                jsonTags.add(o.build())
     390            }
     391            json.writeObject(Json.createObjectBuilder()
     392                .add("data_format", 1)
     393                .add("data_updated", DateTimeFormatter.ofPattern("yyyyMMdd'T'hhmmss'Z'").withZone(ZoneId.of("Z")).format(Instant.now()))
     394                .add("project", project.build())
     395                .add("tags", jsonTags.build())
     396                .build())
     397        }
    373398
    374399        if (output_file != null) {
    375             json.writeTo(output_file)
    376400            output_file.close()
    377401        } else {
    378             print json.toPrettyString()
     402            print writer.toString()
    379403        }
    380404    }
Note: See TracChangeset for help on using the changeset viewer.