mapbox-java icon indicating copy to clipboard operation
mapbox-java copied to clipboard

FeatureCollection.fromJson() should also accept a 'Reader' parameter

Open spiekermax opened this issue 5 years ago • 1 comments

Currently a FeatureCollection can not be instantiated from a Reader. The only function provided requires a String in JSON format as can be seen here.

Such approach is inefficient when reading larger files since the entire file content has to be read and stored in memory before it can be transformed into a FeatureCollection. If a FeatureCollection could be instantiated from a Reader aswell, only parts of the read file would have to be stored in memory which makes this the more desirable approach in the presented situation.

As the used GSON library already enables the use of a Reader instead of a String, the implementation would be very simple and could look like this:

@Nullable
public static FeatureCollection fromJson(@NonNull Reader reader) { 
  GsonBuilder gson = new GsonBuilder();
  gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create());
  gson.registerTypeAdapterFactory(GeometryAdapterFactory.create());
  return gson.create().fromJson(reader, FeatureCollection.class);
}

spiekermax avatar Apr 03 '20 10:04 spiekermax

@spiekermax Did you have any workaround? I am trying to load a big geojson file and getting out of memory exceptions

gilgil28 avatar Mar 10 '22 13:03 gilgil28