Arthur Eirich Arthur Eirich - 1 month ago 6
Java Question

Deserializing into Map<java.lang.String, java.net.URI> with Jackson

I have this JSON snippet:

{
"uploadData": {
"25967391": {
"jobid": "jobId",
"numberOfFiles": "1",
"outputFiles": {
"1": "file.pdf"
},
"originalFiles": {
"1": "./target/test-classes/com/tools/myProject/input/File.pdf",
"2": "./target/test-classes/com/tools/myProject/input/File.csv"
}
}
}
}


What I'd like to do is to deserialize "originalFiles" into a Map. Because "1" and "2" in "originalFiles" are both non absolute URIs I already have a custom class
URIStringDeserializer
which does

Paths.get(".").toUri().resolve(uriString).normalize().toASCIIString()


which gives me

file:/C:/Users/userName/IdeaProjects/myProject/target/test-classes/com/tools/myProject/input/File.pdf


for the first uri of the
originalFiles
.
Here is the class with
uploadData
field:

@JsonIgnoreProperties(ignoreUnknown = true)
public class UploadData{

@JsonProperty("originalFiles")
private Map<Integer, URI> originalFiles;

//getters and setters omitted
}


Annotating the
uploadData
field with
@JsonDeserialize(using = CustomDeserializer.class)
results in a
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.net.URI out of START_OBJECT token
. How can I get my custom deserializer working with tose map?

Answer

After some extern help the solution was found. Adding

@JsonDeserializer(contentUsing = CustomDeserializer.class)

has done what I wanted.