Search in sources :

Example 1 with JsonbJsonpMapper

use of org.opensearch.client.json.jsonb.JsonbJsonpMapper in project opensearch-java by opensearch-project.

the class RequestEncodingTest method testParametersNotInJson.

@Test
public void testParametersNotInJson() {
    // This checks that path parameters ("q") are not serialized as json
    // and variant containers ser/deser
    SearchRequest request = new SearchRequest.Builder().q("blah").query(b1 -> b1.type(b2 -> b2.value("foo"))).aggregations("myagg", b2 -> b2.avg(b3 -> b3.field("foo"))).build();
    JsonbJsonpMapper mapper = new JsonbJsonpMapper();
    String str = toJson(request, mapper);
    assertEquals("{\"aggregations\":{\"myagg\":{\"avg\":{\"field\":\"foo\"}}},\"query\":{\"type\":{\"value\":\"foo\"}}}", str);
    request = fromJson(str, SearchRequest.class, mapper);
    assertTrue(request.query().isType());
    assertEquals("foo", request.query().type().value());
    assertNull(request.q());
}
Also used : SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) Test(org.junit.Test) SearchRequest(org.opensearch.client.opensearch.core.SearchRequest) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) Test(org.junit.Test)

Example 2 with JsonbJsonpMapper

use of org.opensearch.client.json.jsonb.JsonbJsonpMapper in project opensearch-java by opensearch-project.

the class JsonDataTest method testParsing.

@Test
public void testParsing() {
    JsonpMapper mapper = new JsonbJsonpMapper();
    String json = "{\"children\":[{\"doubleValue\":3.2,\"intValue\":2}],\"doubleValue\":2.1,\"intValue\":1," + "\"stringValue\":\"foo\"}";
    JsonParser parser = mapper.jsonProvider().createParser(new StringReader(json));
    JsonData data = JsonData.from(parser, mapper);
    assertEquals("foo", data.toJson().asJsonObject().getString("stringValue"));
    JsonpMapperTest.SomeClass to = data.to(JsonpMapperTest.SomeClass.class);
    assertEquals("foo", to.getStringValue());
}
Also used : JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonpMapper(org.opensearch.client.json.JsonpMapper) StringReader(java.io.StringReader) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonString(jakarta.json.JsonString) JsonData(org.opensearch.client.json.JsonData) JsonParser(jakarta.json.stream.JsonParser) Test(org.junit.Test)

Example 3 with JsonbJsonpMapper

use of org.opensearch.client.json.jsonb.JsonbJsonpMapper in project opensearch-java by opensearch-project.

the class InheritanceTest method testSerialization.

@Test
public void testSerialization() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonProvider provider = JsonProvider.provider();
    FinalClass fc = new FinalClass.Builder().baseField("baseValue").childField("childValue").finalField("finalValue").build();
    JsonGenerator generator = provider.createGenerator(baos);
    fc.serialize(generator, new JsonbJsonpMapper());
    generator.close();
    String str = baos.toString();
    assertEquals("{\"baseField\":\"baseValue\",\"childField\":\"childValue\",\"finalField\":\"finalValue\"}", str);
    baos.reset();
    ChildClass cc = new ChildClass.Builder().baseField("baseValue").childField("childValue").build();
    generator = provider.createGenerator(baos);
    cc.serialize(generator, new JsonbJsonpMapper());
    generator.close();
    str = baos.toString();
    assertEquals("{\"baseField\":\"baseValue\",\"childField\":\"childValue\"}", str);
}
Also used : FinalClass(org.opensearch.client.opensearch.experiments.inheritance.final_.FinalClass) ChildClass(org.opensearch.client.opensearch.experiments.inheritance.child.ChildClass) JsonGenerator(jakarta.json.stream.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonProvider(jakarta.json.spi.JsonProvider) Test(org.junit.Test)

Example 4 with JsonbJsonpMapper

use of org.opensearch.client.json.jsonb.JsonbJsonpMapper in project opensearch-java by opensearch-project.

the class SomeUnionTest method testSerialization.

@Test
public void testSerialization() {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    JsonProvider provider = JsonProvider.provider();
    JsonGenerator generator = provider.createGenerator(baos);
    su.serialize(generator, new JsonbJsonpMapper());
    generator.close();
    System.out.println(baos.toString());
    assertEquals(json, baos.toString());
}
Also used : JsonGenerator(jakarta.json.stream.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonProvider(jakarta.json.spi.JsonProvider) Test(org.junit.Test)

Example 5 with JsonbJsonpMapper

use of org.opensearch.client.json.jsonb.JsonbJsonpMapper in project opensearch-java by opensearch-project.

the class JsonpMapperTest method testJsonb.

@Test
public void testJsonb() {
    JsonpMapper mapper = new JsonbJsonpMapper();
    testSerialize(mapper, json);
    testDeserialize(mapper, json);
}
Also used : JacksonJsonpMapper(org.opensearch.client.json.jackson.JacksonJsonpMapper) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonpMapper(org.opensearch.client.json.JsonpMapper) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 JsonbJsonpMapper (org.opensearch.client.json.jsonb.JsonbJsonpMapper)11 JsonProvider (jakarta.json.spi.JsonProvider)5 JsonParser (jakarta.json.stream.JsonParser)5 JsonGenerator (jakarta.json.stream.JsonGenerator)4 StringReader (java.io.StringReader)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 JsonData (org.opensearch.client.json.JsonData)3 JsonpMapper (org.opensearch.client.json.JsonpMapper)3 JsonString (jakarta.json.JsonString)2 JsonParsingException (jakarta.json.stream.JsonParsingException)2 StandardCharsets (java.nio.charset.StandardCharsets)2 Assert (org.junit.Assert)2 ChildClass (org.opensearch.client.opensearch.experiments.inheritance.child.ChildClass)2 FinalClass (org.opensearch.client.opensearch.experiments.inheritance.final_.FinalClass)2 HttpServer (com.sun.net.httpserver.HttpServer)1 JsonValue (jakarta.json.JsonValue)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1