Ticket #19632: 19632.1.patch

File 19632.1.patch, 4.3 KB (added by taylor.smock, 4 years ago)

Use { and } to indicate a json value (note: patch was hand edited since #19624 modifies the one of the files, patch -p0 < ${FILE} worked with it)

  • src/org/openstreetmap/josm/io/GeoJSONReader.java

     
    335343
    336344                    if (value instanceof JsonString) {
    337345                        tags.put(stringJsonValueEntry.getKey(), ((JsonString) value).getString());
    338                     } else if (value instanceof JsonObject) {
    339                         Logging.warn(
    340                             "The GeoJSON contains an object with property '" + stringJsonValueEntry.getKey()
    341                                 + "' whose value has the unsupported type '" + value.getClass().getSimpleName()
    342                                 + "'. That key-value pair is ignored!"
    343                         );
     346                    } else if (value instanceof JsonObject || value instanceof JsonArray) {
     347                        /* When writing back to file, "{" and "}" are used to
     348                         * determine that it should be interpreted  as a json
     349                         * object or array. They are stored in GeoJSONWriter as variables. */
     350                        tags.put(stringJsonValueEntry.getKey(), GeoJSONWriter.JSON_VALUE_START_MARKER + value.toString() +
     351                                GeoJSONWriter.JSON_VALUE_END_MARKER);
    344352                    } else if (value.getValueType() != JsonValue.ValueType.NULL) {
    345353                        tags.put(stringJsonValueEntry.getKey(), value.toString());
    346354                    }
  • src/org/openstreetmap/josm/io/GeoJSONWriter.java

     
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.io;
    33
     4import java.io.StringReader;
    45import java.io.StringWriter;
    56import java.math.BigDecimal;
    67import java.math.RoundingMode;
     
    2122import javax.json.JsonValue;
    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;
    2629import org.openstreetmap.josm.data.coor.EastNorth;
     
    5457    private static final Set<Way> processedMultipolygonWays = new HashSet<>();
    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_END_MARKER}
     62     * and {@link #JSON_VALUE_START_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_END_MARKER}
     68     * and {@link #JSON_VALUE_START_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
    5975     * @since 12806
     
    182198        // Properties
    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();
    188204
     
    200216        }
    201217    }
    202218
     219    private static JsonValue convertValueToJson(String value) {
     220        if (value.startsWith(JSON_VALUE_START_MARKER) && value.endsWith(JSON_VALUE_END_MARKER)) {
     221            // Trim the markers
     222            String toParse = value.substring(1, value.length() - 2);
     223            try (JsonParser parser = Json.createParser(new StringReader(toParse))) {
     224                if (parser.hasNext() && parser.next() != null)
     225                    return parser.getValue();
     226            } catch (JsonParsingException e) {
     227                Logging.trace(e);
     228            }
     229        }
     230        return Json.createValue(value);
     231    }
     232
    203233    protected void appendLayerBounds(DataSet ds, JsonObjectBuilder object) {
    204234        if (ds != null) {
    205235            Iterator<Bounds> it = ds.getDataSourceBounds().iterator();