Last active
June 6, 2016 17:09
-
-
Save benjaminaaron/273ae159a22d0baeffc9c226f7089a0f to your computer and use it in GitHub Desktop.
using jackson for json to object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AttributesExample { | |
private String str = "foo"; | |
private double dbl = 0.8; | |
private boolean bool = false; | |
public String toString() { return str + ", " + dbl + ", " + bool; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.fasterxml.jackson.annotation.JsonAutoDetect; | |
import com.fasterxml.jackson.annotation.PropertyAccessor; | |
import com.fasterxml.jackson.core.JsonParser; | |
import com.fasterxml.jackson.core.type.TypeReference; | |
import com.fasterxml.jackson.databind.DeserializationContext; | |
import com.fasterxml.jackson.databind.JsonDeserializer; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.fasterxml.jackson.databind.module.SimpleModule; | |
import com.google.gson.JsonElement; | |
import java.io.IOException; | |
import java.util.Map; | |
public class Main { | |
public static void main(String[] args) { | |
// ----------- instantiate class AttributesExample | |
String json = "{" + | |
"\"str\": \"hi\", " + | |
"\"dbl\": 2.0, " + | |
"\"bool\": true" + | |
"}"; | |
ObjectMapper mapper = new ObjectMapper(); | |
mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY); // otherwise private fields won't be usable | |
mapper.registerModule(new SimpleModule().addDeserializer(boolean.class, new JsonDeserializer<Boolean>() { // makes the boolean-parsing more solid, no more numbers allowed, but e.g. "True" or "FALSE" are valid | |
@Override | |
public Boolean deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { | |
String value = jsonParser.getValueAsString().toLowerCase(); | |
if (value.equals("true")) | |
return true; | |
if (value.equals("false")) | |
return false; | |
return null; | |
} | |
})); | |
try { | |
AttributesExample ae = mapper.readValue(json, AttributesExample.class); | |
System.out.println(ae.toString()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
// ----------- instantiate Map<ModelType, JsonElement> | |
json = "{" + | |
"\"OPTIMAL_STEPS_MODEL\": { "+ | |
"\"stepCircleResolution\": 18.0" + | |
"}," + | |
"\"FLOORFIELD\": {" + | |
"\"potentialFieldResolution\": 0.1," + | |
"\"timeCostAttributes\": {" + | |
"\"standardDerivation\": 0.7" + | |
"}}}"; | |
try { | |
Map<ModelType, JsonElement> result = mapper.readValue(json, new TypeReference<Map<ModelType, JsonElement>>() {}); | |
System.out.println(result.toString()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
// --> ERROR: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.google.gson.JsonElement, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum ModelType { | |
OPTIMAL_STEPS_MODEL, | |
FLOORFIELD | |
//... | |
} |
in zeile 30: toLower() ist glaub ich falsch. bool in JSON darf nur "true" oder "false" sein (case sensitive!). siehe http://jsonlint.com/ und rfc 4627.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in zeile 35: nicht besser throw parse exception? wir wollen ja einen fehler erhalten, wenn ungültige eingaben sind. weiß aber nicht, welche exception class jackson verwendet.