/** * This takes the currently defined values found inside this instance and converts it to a GeoJson * string. * * @return a JSON string which represents this Feature * @since 1.0.0 */ @Override public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); // Empty properties -> should not appear in json string Feature feature = this; if (properties().size() == 0) { feature = new AutoValue_Feature(TYPE, bbox(), id(), geometry(), null); } return gson.create().toJson(feature); }
/** * This takes the currently defined values found inside this instance and converts it to a GeoJson * string. * * @return a JSON string which represents this Feature * @since 1.0.0 */ @Override public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); // Empty properties -> should not appear in json string Feature feature = this; if (properties().size() == 0) { feature = new AutoValue_Feature(TYPE, bbox(), id(), geometry(), null); } return gson.create().toJson(feature); }
/** * Create a new instance of this class by passing in a formatted valid JSON String. If you are * creating a Feature object from scratch it is better to use one of the other provided static * factory methods such as {@link #fromGeometry(Geometry)}. * * @param json a formatted valid JSON string defining a GeoJson Feature * @return a new instance of this class defined by the values passed inside this static factory * method * @since 1.0.0 */ public static Feature fromJson(@NonNull String json) { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); gson.registerTypeAdapter(Point.class, new PointDeserializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); Feature feature = gson.create().fromJson(json, Feature.class); // Even thought properties are Nullable, // Feature object will be created with properties set to an empty object, // so that addProperties() would work if (feature.properties() != null) { return feature; } return new AutoValue_Feature(TYPE, feature.bbox(), feature.id(), feature.geometry(), new JsonObject()); }
/** * Create a new instance of this class by passing in a formatted valid JSON String. If you are * creating a Feature object from scratch it is better to use one of the other provided static * factory methods such as {@link #fromGeometry(Geometry)}. * * @param json a formatted valid JSON string defining a GeoJson Feature * @return a new instance of this class defined by the values passed inside this static factory * method * @since 1.0.0 */ public static Feature fromJson(@NonNull String json) { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapterFactory(GeoJsonAdapterFactory.create()); gson.registerTypeAdapter(Point.class, new PointDeserializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxDeserializer()); gson.registerTypeAdapter(Geometry.class, new GeometryDeserializer()); Feature feature = gson.create().fromJson(json, Feature.class); // Even thought properties are Nullable, // Feature object will be created with properties set to an empty object, // so that addProperties() would work if (feature.properties() != null) { return feature; } return new AutoValue_Feature(TYPE, feature.bbox(), feature.id(), feature.geometry(), new JsonObject()); }