diff --git a/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java b/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java index 604bf2791e..38a03a0742 100644 --- a/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java +++ b/java-client/src/main/java/org/opensearch/client/json/JsonpUtils.java @@ -41,10 +41,13 @@ import jakarta.json.stream.JsonParser.Event; import jakarta.json.stream.JsonParsingException; import java.io.StringReader; +import java.io.StringWriter; import java.util.AbstractMap; import java.util.Map; import java.util.stream.Collectors; import javax.annotation.Nullable; + +import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import org.opensearch.client.util.ObjectBuilder; public class JsonpUtils { @@ -238,4 +241,14 @@ public static void serializeIntOrNull(JsonGenerator generator, int value, int de generator.write(value); } } + + public static String toJson(JsonpSerializable obj) { + StringWriter stringWriter = new StringWriter(); + JsonbJsonpMapper mapper = new JsonbJsonpMapper(); + JsonGenerator generator = mapper.jsonProvider().createGenerator(stringWriter); + mapper.serialize(obj, generator); + generator.close(); + return stringWriter.toString(); + } + } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java index af17210b97..0116c26b2d 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/json/JsonDataTest.java @@ -36,11 +36,18 @@ import jakarta.json.JsonValue; import jakarta.json.stream.JsonParser; import java.io.StringReader; + import org.junit.Assert; import org.junit.Test; import org.opensearch.client.json.JsonData; import org.opensearch.client.json.JsonpMapper; +import org.opensearch.client.json.JsonpUtils; import org.opensearch.client.json.jsonb.JsonbJsonpMapper; +import org.opensearch.client.opensearch._types.FieldValue; +import org.opensearch.client.opensearch._types.query_dsl.TermQuery; +import org.opensearch.client.opensearch.core.IndexRequest; +import org.opensearch.client.opensearch.core.SearchRequest; +import org.opensearch.client.opensearch.experiments.api.query2.BoolQuery; import org.opensearch.client.opensearch.model.ModelTestCase; public class JsonDataTest extends Assert { @@ -89,4 +96,65 @@ public void testConvert() { assertEquals(JsonValue.ValueType.STRING, value.getValueType()); assertEquals("foo", ((JsonString) value).getString()); } + + + + public class Person { + private int aga; + private String name; + + + public Person(int aga, String name) { + this.aga = aga; + this.name = name; + } + + public int getAga() { + return aga; + } + + public String getName() { + return name; + } + } + + @Test + public void indexRequestToJsonTest() { + String expectedJson = "{\"aga\":42,\"name\":\"koko\"}"; + Person person = new Person(42, "koko"); + IndexRequest indexRequest = new IndexRequest.Builder() + .index("sample-index") + .id("1") + .document(person) + .build(); + + String json = JsonpUtils.toJson(indexRequest); + System.out.println(json); + + assertEquals(expectedJson, json); + } + + @Test + public void searchRequestToJsonTest() { + String expectedJson = "{\"query\":{\"term\":{\"key-field\":{\"value\":\"value-filed\"}}}}"; + + SearchRequest searchRequest = new SearchRequest + .Builder() + .index("my-index") + .query(new TermQuery + .Builder() + .field("key-field") + .value(FieldValue.of("value-filed")) + .build() + ._toQuery() + ) + .build(); + + String json = JsonpUtils.toJson(searchRequest); + System.out.println(json); + + assertEquals(expectedJson, json); + } + + }