Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to configure support for custom ObjectMapper's #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/wololo/geojson/GeoJSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class GeoJSON {
private static final ObjectMapper mapper = new ObjectMapper();

@JsonProperty("type")
private String type;

Expand All @@ -20,6 +18,8 @@ public GeoJSON() {
}

public String toString() {
final ObjectMapper mapper = GeoJSONFactory.getConfig().getMapper();

try {
return mapper.writeValueAsString(this);
} catch (JsonGenerationException e) {
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/org/wololo/geojson/GeoJSONFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,25 @@
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.wololo.geojson.internals.Config;


public class GeoJSONFactory {
private static final ObjectMapper mapper = new ObjectMapper();
private static Config config = new Config();

public static Config getConfig() {
return config;
}

public static void setConfig(Config config) {
if (config != null) {
GeoJSONFactory.config = config;
}
}

public static GeoJSON create(String json) {
try {
JsonNode node = mapper.readTree(json);
JsonNode node = config.getMapper().readTree(json);
String type = node.get("type").asText();
if (type.equals("FeatureCollection")) {
return readFeatureCollection(node);
Expand Down Expand Up @@ -47,9 +57,9 @@ private static FeatureCollection readFeatureCollection(JsonNode node)
private static Feature readFeature(JsonNode node)
throws JsonParseException, JsonMappingException, IOException, ClassNotFoundException {
JsonNode geometryNode = node.get("geometry");
JavaType javaType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
JavaType javaType = config.getMapper().getTypeFactory().constructMapType(Map.class, String.class, Object.class);
Object id = node.get("id");
Map<String, Object> properties = mapper.readValue(node.get("properties").traverse(), javaType);
Map<String, Object> properties = config.getMapper().readValue(node.get("properties").traverse(), javaType);
Geometry geometry = readGeometry(geometryNode);
return new Feature(id, geometry, properties);
}
Expand All @@ -66,7 +76,7 @@ private static Geometry readGeometry(JsonNode node)

private static Geometry readGeometry(JsonNode node, String type)
throws JsonParseException, JsonMappingException, IOException, ClassNotFoundException {
return (Geometry) mapper.readValue(node.traverse(), Class.forName("org.wololo.geojson." + type));
return (Geometry) config.getMapper().readValue(node.traverse(), Class.forName("org.wololo.geojson." + type));
}

}
26 changes: 26 additions & 0 deletions src/main/java/org/wololo/geojson/internals/Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.wololo.geojson.internals;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* This is a volatile class.
* It may be changed if the configuration options change.
*/
public final class Config {
private final ObjectMapper mapper;

public Config() {
this(new ObjectMapper());
}

public Config(ObjectMapper mapper) {
if (mapper == null) {
throw new RuntimeException("ObjectMapper instance cannot be null");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be IllegelArgumentException instead of RuntimeException

}
this.mapper = mapper;
}

public ObjectMapper getMapper() {
return mapper;
}
}