GeometryCollection implements Geometry { @Override public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); gson.registerTypeAdapterFactory(GeometryAdapterFactory.create()); return gson.create().toJson(this); } GeometryCollection(String type, @Nullable BoundingBox bbox, List<Geometry> geometries); static GeometryCollection fromJson(String json); static GeometryCollection fromGeometries(@NonNull List<Geometry> geometries); static GeometryCollection fromGeometries(@NonNull List<Geometry> geometries,
                                                  @Nullable BoundingBox bbox); static GeometryCollection fromGeometry(@NonNull Geometry geometry); static GeometryCollection fromGeometry(@NonNull Geometry geometry,
                                                @Nullable BoundingBox bbox); @NonNull @Override String type(); @Nullable @Override BoundingBox bbox(); @NonNull List<Geometry> geometries(); @Override String toJson(); static TypeAdapter<GeometryCollection> typeAdapter(Gson gson); @Override String toString(); @Override boolean equals(Object obj); @Override int hashCode();  }
@Test public void toJson() throws IOException { final String jsonOriginal = " { \"type\": \"GeometryCollection\"," + " \"bbox\": [-120, -40, 120, 40]," + " \"geometries\": [" + " { \"type\": \"Point\"," + " \"bbox\": [-110, -30, 110, 30]," + " \"coordinates\": [100, 0]}," + " { \"type\": \"LineString\"," + " \"bbox\": [-110, -30, 110, 30]," + " \"coordinates\": [[101, 0], [102, 1]]}]}"; List<Geometry> geometries = new ArrayList<>(2); geometries.add(Point.fromLngLat(100, 0, BoundingBox.fromLngLats(-110, -30,110, 30))); geometries.add(LineString.fromLngLats( Arrays.asList(Point.fromLngLat(101, 0), Point.fromLngLat(102, 1)), BoundingBox.fromLngLats(-110, -30,110, 30))); GeometryCollection geometryCollection = GeometryCollection.fromGeometries(geometries, BoundingBox.fromLngLats(-120, -40,120, 40)); String jsonString = geometryCollection.toJson(); compareJson(jsonOriginal, jsonString); }