1

I'm trying to create a Hit<ObjectNode> from a json string returned by Elastic (for unit-testing purposes). When I try to deserialize this using .withJson I get a ClassCastException with the following message: "class co.elastic.clients.json.JsonDataImpl cannot be cast to class com.fasterxml.jackson.databind.node.ObjectNode (co.elastic.clients.json.JsonDataImpl and com.fasterxml.jackson.databind.node.ObjectNode are in unnamed module of loader 'app')"

Full code:

String hitJson = "{\"_index\":\"my-index\",\"_id\":\"ID:MYID\",\"_score\":1.0,\"_source\":{\"productCode\":\"MYID\",\"productName\":\"my product\",\"EffDate\":\"01/01/1900\",\"ExpDate\":\"12/31/9999\",\"Status\":\"Active\",\"productId\":1234567,\"_type\":\"my type\",\"type\":\"my type\",\"updateTime\":\"2021-07-08T11:40:18Z\"}}";
InputStream hitInput = new ByteArrayInputStream(hitJson.getBytes());

Hit<ObjectNode> aHit = Hit.of(builder -> builder.withJson(hitInput));
try {
    ObjectNode source = aHit.source();
} catch (Exception ex) {
    String msg = ex.getMessage();
}

I can see that JsonDataImpl does have all the properties, but I'm not sure why it can't deserialize it to ObjectNode. I'm not sure where to go from here - all my other attemps to use .withJson have succeeded without issue.

Formatted json response for reference:

{
    "_index": "my-index",
    "_id": "ID:MYID",
    "_score": 1.0,
    "_source": {
        "productCode": "MYID",
        "productName": "my product",
        "EffDate": "01/01/1900",
        "ExpDate": "12/31/9999",
        "Status": "Active",
        "productId": 1234567,
        "_type": "my type",
        "type": "my type",
        "updateTime": "2021-07-08T11:40:18Z"
    }
}
Mansfield
  • 14,445
  • 18
  • 76
  • 112
  • I believe this `JSONDataImpl` could be cast to the elastic search's own class `JsonData` [java doc](https://artifacts.elastic.co/javadoc/co/elastic/clients/elasticsearch-java/8.1.0/co/elastic/clients/json/JsonData.html). Not sure, why you are casting that to `ObjectData`. So, if you do `Hit` and then use `JsonValue value = hit.toJson()` you can then start consuming [Jakarta JsonValue](https://jakarta.ee/specifications/platform/9/apidocs/jakarta/json/class-use/jsonvalue). I don't have any setup to test this locally. Hence commenting now. – Nagaraj Tantri Feb 16 '23 at 21:07
  • @NagarajTantri Thanks for the comment. Unfortunately the code I have to test (not changeable) uses ObjectNode everywhere. I did try switching to JsonNode and got a similar exception: `"class co.elastic.clients.json.JsonDataImpl cannot be cast to class com.fasterxml.jackson.databind.JsonNode (co.elastic.clients.json.JsonDataImpl and com.fasterxml.jackson.databind.JsonNode are in unnamed module of loader 'app')"` – Mansfield Feb 16 '23 at 21:19
  • you should be able to import `co.elastic.clients.json.JsonNode` and not `com.fasterxml.jackson.databind.JsonNode` to convert. Also, if you want to convert a class X object to Jackson's ObjectNode then you need to use an ObjectMapper like https://stackoverflow.com/questions/18320419/how-to-directly-write-to-a-json-object-objectnode-from-objectmapper-in-jackson – Nagaraj Tantri Feb 16 '23 at 22:11

0 Answers0