Permalink
Browse files

Remove BOM from GeoJSON files

While GeoJSON files should not have a BOM per the spec, sometimes
systems will include the UTF byte order marker (BOM). This breaks the
JSON parsing on both in the web client and app. This change will check
for and strip out the BOM before passing the JSON string along for
further processing.

Change-Id: I9452871e19b3985f9beea48b30cf49b60e4835cb
  • Loading branch information...
ewpatton authored and SusanRatiLane committed Nov 7, 2018
1 parent 0206b00 commit 560b3724a6c86bd2a5f678dfc270d9a185b0ed2f
@@ -180,6 +180,7 @@ private native void setNativeGeoJSON(JavaScriptObject map, String geojson)/*-{
if (collection) {
var self = this;
map.removeLayer(collection);
if (geojson.charCodeAt = 0xFEFF) geojson = geojson.substr(1); // strip byte order marker, if present
collection = top.L.geoJson(JSON.parse(geojson), {
pointToLayer: function(feature, latlng) {
for (var key in feature.properties) {
@@ -355,7 +355,7 @@ public void run() {

@SuppressWarnings("WeakerAccess")
protected void processGeoJSON(final String url, final String content) throws JSONException {
JSONObject parsedData = new JSONObject(content);
JSONObject parsedData = new JSONObject(stripBOM(content));
String type = parsedData.optString(GEOJSON_TYPE);
if (!GEOJSON_FEATURECOLLECTION.equals(type) && !GEOJSON_GEOMETRYCOLLECTION.equals(type)) {
$form().runOnUiThread(new Runnable() {
@@ -425,4 +425,12 @@ private YailList jsonArrayToYail(JSONArray array) throws JSONException {
return YailList.makeList(items);
}

private static String stripBOM(String content) {
if (content.charAt(0) == '\uFEFF') {
return content.substring(1);
} else {
return content;
}
}

}
@@ -201,6 +201,18 @@ public void testGetMap() {
assertEquals(getMap(), collection.getMap());
}

/**
* Tests that the FeatureCollection can load a GeoJSON file with an invalid BOM. Technically, BOM is not allowed
* by the JSON RFC, but who needs standards anyway?
*/
@Test
public void testGeoJSONWithBOM() {
ShadowEventDispatcher.clearEvents();
collection.FeaturesFromGeoJSON("\uFEFF{\"type\":\"FeatureCollection\",\"features\":[]}");
runAllEvents();
assertEventFiredAny(collection, "GotFeatures");
}

private void testFeatureListSetter(MapFeature feature) {
ShadowView view = Shadow.extract(getMap().getView());
view.clearWasInvalidated();

0 comments on commit 560b372

Please sign in to comment.