Skip to content

Commit

Permalink
added UnknownJsonKeyException
Browse files Browse the repository at this point in the history
  • Loading branch information
qcdyx committed Feb 5, 2025
1 parent 58fce8c commit 325c19a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice;

/**
* Duplicated elements in locations.geojson file.
*/
@GtfsValidationNotice(severity = ERROR)
public class GeoJsonDuplicatedElementNotice extends ValidationNotice {
/**
* The name of the file where the duplicated element was found.
*/
private final String filename;

/**
* The duplicated element in the GeoJSON file.
*/
private final String duplicatedElement;

public GeoJsonDuplicatedElementNotice(String filename, String duplicatedElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@

import org.mobilitydata.gtfsvalidator.annotation.GtfsValidationNotice;

/**
* Unknown elements in locations.geojson file.
*/
@GtfsValidationNotice(severity = ERROR)
public class GeoJsonUnknownElementNotice extends ValidationNotice {
/**
* The name of the file where the unknown element was found.
*/
private final String filename;

/**
* The unknown element in the GeoJSON file.
*/
private final String unknownElement;

public GeoJsonUnknownElementNotice(String filename, String unknownElement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ public GtfsEntityContainer load(
try {
List<GtfsGeoJsonFeature> entities = extractFeaturesFromStream(inputStream, noticeContainer);
return geoJsonFileDescriptor.createContainerForEntities(entities, noticeContainer);
} catch (JsonSyntaxException jsonSyntaxException) {
noticeContainer.addValidationNotice(
new GeoJsonDuplicatedElementNotice(GtfsGeoJsonFeature.FILENAME, "type"));
return fileDescriptor.createContainerForInvalidStatus(TableStatus.UNPARSABLE_ROWS);
} catch (DuplicateJsonKeyException duplicateJsonKeyException) {
noticeContainer.addValidationNotice(
new GeoJsonDuplicatedElementNotice(GtfsGeoJsonFeature.FILENAME, duplicateJsonKeyException.getKey()));
return fileDescriptor.createContainerForInvalidStatus(TableStatus.UNPARSABLE_ROWS);
} catch (JsonParseException jpex) {
noticeContainer.addValidationNotice(
new MalformedJsonNotice(GtfsGeoJsonFeature.FILENAME, jpex.getMessage()));
Expand Down Expand Up @@ -110,14 +102,11 @@ public List<GtfsGeoJsonFeature> extractFeaturesFromStream(
}
}
} catch (DuplicateJsonKeyException exception) {
System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
// String message = exception.getMessage();
// if (message.contains("Duplicated")) {
//
// } else if (message.contains("Unknown")) {
// noticeContainer.addValidationNotice(
// new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, "id"));
// }
noticeContainer.addValidationNotice(
new GeoJsonDuplicatedElementNotice(GtfsGeoJsonFeature.FILENAME, exception.getKey()));
} catch (UnknownJsonKeyException exception) {
noticeContainer.addValidationNotice(
new GeoJsonUnknownElementNotice(GtfsGeoJsonFeature.FILENAME, exception.getKey()));
}
if (hasUnparsableFeature) {
throw new UnparsableGeoJsonFeatureException("Unparsable GeoJSON feature");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
* A custom JSON type adapter for parsing JSON objects with duplicate keys. The target class is
Expand All @@ -16,6 +17,8 @@
*/
public class MapJsonTypeAdapter extends TypeAdapter<Map<String, Object>> {

private static final Set<String> ALLOWED_KEYS = Set.of("type", "features", "coordinates", "id", "properties", "geometry");

@Override
public void write(JsonWriter out, Map<String, Object> value) throws IOException {
new Gson().toJson(value, Map.class, out);
Expand All @@ -37,6 +40,10 @@ private Map<String, Object> parseJsonObject(JsonReader in) throws IOException {
throw new DuplicateJsonKeyException(key);
}

if (!ALLOWED_KEYS.contains(key)) {
throw new UnknownJsonKeyException(key);
}

Object value = parseJsonValue(in);
map.put(key, value);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.mobilitydata.gtfsvalidator.util.geojson;

public class UnknownJsonKeyException extends RuntimeException {
private String key;

public UnknownJsonKeyException(String key) {
this.key = key;
}

public String getKey() {
return key;
}
}

0 comments on commit 325c19a

Please sign in to comment.