001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.plugins.streetside.utils.api; 003 004import java.lang.reflect.Array; 005import java.util.function.Function; 006 007import javax.json.JsonArray; 008import javax.json.JsonNumber; 009import javax.json.JsonObject; 010import javax.json.JsonString; 011import javax.json.JsonValue; 012 013import org.openstreetmap.josm.data.coor.LatLon; 014import org.openstreetmap.josm.plugins.streetside.StreetsideImage; 015import org.openstreetmap.josm.plugins.streetside.StreetsideSequence; 016import org.openstreetmap.josm.plugins.streetside.utils.StreetsideURL.APIv3; 017 018/** 019 * Decodes the JSON returned by {@link APIv3} into Java objects. 020 * Takes a {@link JsonObject} and {@link #decodeSequence(JsonObject)} tries to convert it to a {@link StreetsideSequence}. 021 */ 022public final class JsonSequencesDecoder { 023 private JsonSequencesDecoder() { 024 // Private constructor to avoid instantiation 025 } 026 027 /** 028 * Parses a given {@link JsonObject} as a GeoJSON Feature into a {@link StreetsideSequence}. 029 * @param json the {@link JsonObject} to be parsed 030 * @return a {@link Streetsideequence} that is parsed from the given {@link JsonObject}. If mandatory information is 031 * missing from the JSON or it's not meeting the expecting format in another way, <code>null</code> will be 032 * returned. 033 */ 034 public static StreetsideSequence decodeSequence(final JsonObject json) { 035 if (json == null || !"Feature".equals(json.getString("type", null))) { 036 return null; 037 } 038 StreetsideSequence result = null; 039 final JsonObject properties = json.getJsonObject("properties"); 040 final Long ca = properties == null ? null : JsonDecoder.decodeTimestamp(properties.getString("cd", null)); 041 if (properties != null && properties.getString("id", null) != null && properties.getString("user_key", null) != null && ca != null) { 042 result = new StreetsideSequence(properties.getString("id", null), ca); 043 044 final Double[] hes = decodeCoordinateProperty( 045 properties, 046 "hes", 047 val -> val instanceof JsonNumber ? ((JsonNumber) val).doubleValue() : null, 048 Double.class 049 ); 050 final String[] imageIds = decodeCoordinateProperty( 051 properties, 052 "image_ids", 053 val -> val instanceof JsonString ? ((JsonString) val).getString() : null, 054 String.class 055 ); 056 final LatLon[] geometry = decodeLatLons(json.getJsonObject("geometry")); 057 final int sequenceLength = Math.min(Math.min(hes.length, imageIds.length), geometry.length); 058 for (int i = 0; i < sequenceLength; i++) { 059 if (hes[i] != null && imageIds[i] != null && geometry[i] != null) { 060 final StreetsideImage img = new StreetsideImage(imageIds[i], geometry[i], hes[i]); 061 result.add(img); 062 } 063 } 064 if (result.getImages().isEmpty()) { 065 result = null; 066 } 067 } 068 return result; 069 } 070 071 /** 072 * Converts a {@link JsonArray} to a java array. 073 * The conversion from {@link JsonValue} to a java type is done by the supplied function. 074 * @param array the array to be converted 075 * @param decodeValueFunction the function used for conversion from {@link JsonValue} to the desired type. 076 * @param clazz the desired type that the elements of the resulting array should have 077 * @return the supplied array converted from {@link JsonArray} to a java array of the supplied type, converted using 078 * the supplied function. Never <code>null</code>, in case of array==null, an array of length 0 is returned. 079 */ 080 @SuppressWarnings("unchecked") 081 private static <T> T[] decodeJsonArray(final JsonArray array, final Function<JsonValue, T> decodeValueFunction, final Class<T> clazz) { 082 final T[] result; 083 if (array == null) { 084 result = (T[]) Array.newInstance(clazz, 0); 085 } else { 086 result = (T[]) Array.newInstance(clazz, array.size()); 087 for (int i = 0; i < result.length; i++) { 088 result[i] = decodeValueFunction.apply(array.get(i)); 089 } 090 } 091 return result; 092 } 093 094 /** 095 * Given the JSON object representing the `properties` of a sequence, this method converts you one attribute from the 096 * `coordinateProperties` object to an array of appropriate type. 097 * 098 * For example this is used to convert the `image_keys` JSON array to a String[] array or the `cas` JSON array to a 099 * Double[] array. 100 * @param json the JSON object representing the `properties` of a sequence 101 * @param key the key, which identifies the desired array inside the `coordinateProperties` object to be converted 102 * @param decodeValueFunction a function that converts the {@link JsonValue}s in the JSON array to java objects of the 103 * desired type 104 * @param clazz the {@link Class} object of the desired type, that the entries of the resulting array should have 105 * @return the resulting array, when converting the desired entry of the `coordinateProperties`. 106 * Never <code>null</code>. If no `coordinateProperties` are set, or if the desired key is not set or is not 107 * an array, then an empty array of the desired type is returned. 108 */ 109 @SuppressWarnings("unchecked") 110 private static <T> T[] decodeCoordinateProperty( 111 final JsonObject json, final String key, final Function<JsonValue, T> decodeValueFunction, final Class<T> clazz 112 ) { 113 final JsonValue coordinateProperties = json == null ? null : json.get("coordinateProperties"); 114 if (coordinateProperties instanceof JsonObject) { 115 JsonValue valueArray = ((JsonObject) coordinateProperties).get(key); 116 if (valueArray instanceof JsonArray) { 117 return decodeJsonArray((JsonArray) valueArray, decodeValueFunction, clazz); 118 } 119 } 120 return (T[]) Array.newInstance(clazz, 0); 121 } 122 123 private static LatLon[] decodeLatLons(final JsonObject json) { 124 final JsonValue coords = json == null ? null : json.get("coordinates"); 125 if (coords instanceof JsonArray && "LineString".equals(json.getString("type", null))) { 126 final LatLon[] result = new LatLon[((JsonArray) coords).size()]; 127 for (int i = 0; i < ((JsonArray) coords).size(); i++) { 128 final JsonValue coord = ((JsonArray) coords).get(i); 129 if (coord instanceof JsonArray) { 130 result[i] = JsonDecoder.decodeLatLon((JsonArray) coord); 131 } 132 } 133 return result; 134 } 135 return new LatLon[0]; 136 } 137}