Changeset 16936 in josm


Ignore:
Timestamp:
2020-08-26T20:40:25+02:00 (4 weeks ago)
Author:
simon04
Message:

fix #19632 - GeoJSONWriter: write key={value} as JSON object (patch by taylor.smock, modified)

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/GeoJSONWriter.java

    r15905 r16936  
    22package org.openstreetmap.josm.io;
    33
     4import java.io.StringReader;
    45import java.io.StringWriter;
    56import java.math.BigDecimal;
     
    2223import javax.json.JsonWriter;
    2324import javax.json.stream.JsonGenerator;
     25import javax.json.stream.JsonParser;
     26import javax.json.stream.JsonParsingException;
    2427
    2528import org.openstreetmap.josm.data.Bounds;
     
    5558
    5659    /**
     60     * This is used to determine that a tag should be interpreted as a json
     61     * object or array. The tag should have both {@link #JSON_VALUE_START_MARKER}
     62     * and {@link #JSON_VALUE_END_MARKER}.
     63     */
     64    static final String JSON_VALUE_START_MARKER = "{";
     65    /**
     66     * This is used to determine that a tag should be interpreted as a json
     67     * object or array. The tag should have both {@link #JSON_VALUE_START_MARKER}
     68     * and {@link #JSON_VALUE_END_MARKER}.
     69     */
     70    static final String JSON_VALUE_END_MARKER = "}";
     71
     72    /**
    5773     * Constructs a new {@code GeoJSONWriter}.
    5874     * @param ds The OSM data set to save
     
    183199        final JsonObjectBuilder propObj = Json.createObjectBuilder();
    184200        for (Entry<String, String> t : p.getKeys().entrySet()) {
    185             propObj.add(t.getKey(), t.getValue());
     201            propObj.add(t.getKey(), convertValueToJson(t.getValue()));
    186202        }
    187203        final JsonObject prop = propObj.build();
     
    199215                    .add("geometry", geom.isEmpty() ? JsonValue.NULL : geom));
    200216        }
     217    }
     218
     219    private static JsonValue convertValueToJson(String value) {
     220        if (value.startsWith(JSON_VALUE_START_MARKER) && value.endsWith(JSON_VALUE_END_MARKER)) {
     221            try (JsonParser parser = Json.createParser(new StringReader(value))) {
     222                if (parser.hasNext() && parser.next() != null) {
     223                    return parser.getValue();
     224                }
     225            } catch (JsonParsingException e) {
     226                Logging.warn(e);
     227            }
     228        }
     229        return Json.createValue(value);
    201230    }
    202231
  • trunk/test/unit/org/openstreetmap/josm/io/GeoJSONWriterTest.java

    r16006 r16936  
    4040        node.put("name", "foo");
    4141        node.put("source", "code");
     42        node.put("data", "{\"foo\": 1, \"bar\": \"baz\"}");
    4243        final DataSet ds = new DataSet();
    4344        ds.addPrimitive(node);
     
    5253                "            'properties': {\n" +
    5354                "                'name': 'foo',\n" +
    54                 "                'source': 'code'\n" +
     55                "                'source': 'code',\n" +
     56                "                'data': {\n" +
     57                "                    'foo': 1,\n" +
     58                "                    'bar': 'baz'\n" +
     59                "                }\n" +
    5560                "            },\n" +
    5661                "            'geometry': {\n" +
Note: See TracChangeset for help on using the changeset viewer.