I'm trying to build a JPA CriteriaQuery from a client-provided JSON filter; that is, to query this Person POJO:
@Entity
public class Person {
@Id
public Long id;
public String name;
public Date dateOfBirth;
}
a client would provide a JSON like this
{
"$or": [
{ "name": { "$beginsWith": "Jo" } },
{ "dateOfBirth": { "$lessThan": "1983-01-01T00:00:00Z" } }
]
}
When parsing this filter, I need to obtain the right Java type from the provided JSON values, e.g. a Date
for dateOfBirth
. Given that I can easily reach the JsonNode
for "1983-01-01T00:00:00Z"
and that Jackson's ObjectMapper
knows somewhere what type to deserialize for the dateOfBirth
field of Person.class
, how can I get it? Ideally it would be something in the lines of:
// jsonNode is the JsonNode for "1983-01-01T00:00:00Z"
ObjectMapper mapper = new ObjectMapper();
ObjectReader reader = mapper.readerFor(Person.class);
Date date = (Date) reader.asField("dateOfBirth").readValue(jsonNode);
That asField()
method would do the magic to return a reader which will read a JSON string
as Java Date
(or more generally the type that the ObjectMapper
would resolve applying [de]serializers annotated for that field).
Note: I'm not asking how to parse dates with Jackson, but how to read a value and get the object Jackson would deserialize normally, but for a nested field.