I have a javax.json.JsonObject
and want to validate it against a JSON schema. So I've found the com.github.fge.json-schema-validator
. But it works only with com.fasterxml.jackson.databind.JsonNode
.
Is there a way to convert my JsonObject
into a JsonNode
?
The following solution parses a javax.json.JsonObject
into a JSON string and then parses the JSON string into a com.fasterxml.jackson.databind.JsonNode
using Jackson's ObjectMapper
:
public JsonNode toJsonNode(JsonObject jsonObject) {
// Parse a JsonObject into a JSON string
StringWriter stringWriter = new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) {
jsonWriter.writeObject(jsonObject);
}
String json = stringWriter.toString();
// Parse a JSON string into a JsonNode
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(json);
return jsonNode;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments