elasticsearch-java icon indicating copy to clipboard operation
elasticsearch-java copied to clipboard

Add serializeToString() method

Open dadoonet opened this issue 2 years ago • 5 comments

This method serializeToString() helps to generate the json String representation of any response or request object. This could be useful for debugging or for other usages like consuming the search response objects within a JavaScript based frontend.

dadoonet avatar Mar 21 '22 17:03 dadoonet

@swallez I think we should try to think something even more useful. Let me know if it's doable.

I'd love to have the method serializeToString() available for every request or response object out of the box.

Instead of writing:

SearchResponse<Person> resp = ...;
String json = new JacksonJsonpMapper().serializeToString(resp);

We could write:

SearchResponse<Person> resp = ...;
String json = resp.toJson();

Would it be good for you?

dadoonet avatar Mar 21 '22 17:03 dadoonet

Would it be good for you?

I'd like to limit the number of framework-related methods on API classes to the bare minimum, and I don't expect serializeToString to be widely used. So better keep it on the mapper object (similar to what exists on Jackson's ObjectMapper).

swallez avatar Mar 24 '22 17:03 swallez

I don't really agree with the quote of @swallez '...and I don't expect serializeToString to be widely used....' The 'serializeToString' method is very useful for people not using Jackson but Gson for example. Even more useful would be to make the Json library used pluggable. Thanks for taking this into consideration!

frank-montyne avatar Jun 27 '22 09:06 frank-montyne

@dadoonet When can we expect this to get merged?

rharsh-arkin avatar Mar 01 '23 12:03 rharsh-arkin

@swallez I think that we can now use:

JsonpUtils.toJsonString(response, jacksonJsonpMapper);

So should we close this one?

dadoonet avatar May 17 '23 10:05 dadoonet