Search in sources :

Example 1 with JsonProvider

use of jakarta.json.spi.JsonProvider in project opensearch-java by opensearch-project.

the class ModelTestCase method toJson.

public static <T> String toJson(T value, JsonpMapper mapper) {
    StringWriter sw = new StringWriter();
    JsonProvider provider = mapper.jsonProvider();
    JsonGenerator generator = provider.createGenerator(sw);
    mapper.serialize(value, generator);
    generator.close();
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(jakarta.json.stream.JsonGenerator) JsonProvider(jakarta.json.spi.JsonProvider)

Example 2 with JsonProvider

use of jakarta.json.spi.JsonProvider 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 3 with JsonProvider

use of jakarta.json.spi.JsonProvider 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 4 with JsonProvider

use of jakarta.json.spi.JsonProvider in project opensearch-java by opensearch-project.

the class ParsingTests method testFoo.

@Test
public void testFoo() throws Exception {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        FooRequest foo = FooRequest.builder().name("z").value(1).indices("a", "b", "c").bar(b -> b.name("Raise the bar")).build();
        JsonProvider provider = JsonProvider.provider();
        JsonGenerator generator = provider.createGenerator(baos);
        foo.serialize(generator, new JsonbJsonpMapper());
        generator.close();
        String str = baos.toString();
        assertEquals("{\"name\":\"z\",\"value\":1,\"indices\":[\"a\",\"b\",\"c\"],\"bar\":{\"name\":\"Raise the bar\"}}", str);
        JsonParser parser = provider.createParser(new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8)));
        FooRequest foo2 = FooRequest.parser().deserialize(parser, new JsonbJsonpMapper());
        assertEquals(foo.name(), foo2.name());
        assertEquals(foo.value(), foo2.value());
        assertNull(foo2.size());
        assertEquals(foo.indices(), foo2.indices());
        assertEquals("Raise the bar", foo.bar().name());
    } catch (JsonParsingException je) {
        throw new JsonParsingException(je.getMessage() + " at " + je.getLocation(), je, je.getLocation());
    }
}
Also used : JsonProvider(jakarta.json.spi.JsonProvider) JsonParser(jakarta.json.stream.JsonParser) FooRequest(org.opensearch.client.opensearch.experiments.api.FooRequest) JsonParsingException(jakarta.json.stream.JsonParsingException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) Test(org.junit.Test) JsonGenerator(jakarta.json.stream.JsonGenerator) Assert(org.junit.Assert) StandardCharsets(java.nio.charset.StandardCharsets) FooRequest(org.opensearch.client.opensearch.experiments.api.FooRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonGenerator(jakarta.json.stream.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonProvider(jakarta.json.spi.JsonProvider) JsonParser(jakarta.json.stream.JsonParser) JsonParsingException(jakarta.json.stream.JsonParsingException) Test(org.junit.Test)

Example 5 with JsonProvider

use of jakarta.json.spi.JsonProvider in project opensearch-java by opensearch-project.

the class InheritanceTest method testDeserialization.

@Test
public void testDeserialization() {
    JsonProvider provider = JsonProvider.provider();
    JsonParser parser = provider.createParser(new StringReader("{\"baseField\":\"baseValue\",\"childField\":\"childValue\",\"finalField\":\"finalValue\"}"));
    FinalClass fc = FinalClass.JSONP_PARSER.deserialize(parser, new JsonbJsonpMapper());
    assertEquals("baseValue", fc.baseField());
    assertEquals("childValue", fc.childField());
    assertEquals("finalValue", fc.finalField());
    parser = provider.createParser(new StringReader("{\"baseField\":\"baseValue\",\"childField\":\"childValue\"}"));
    ChildClass cc = ChildClass.JSONP_PARSER.deserialize(parser, new JsonbJsonpMapper());
    assertEquals("baseValue", cc.baseField());
    assertEquals("childValue", cc.childField());
}
Also used : FinalClass(org.opensearch.client.opensearch.experiments.inheritance.final_.FinalClass) ChildClass(org.opensearch.client.opensearch.experiments.inheritance.child.ChildClass) StringReader(java.io.StringReader) JsonbJsonpMapper(org.opensearch.client.json.jsonb.JsonbJsonpMapper) JsonProvider(jakarta.json.spi.JsonProvider) JsonParser(jakarta.json.stream.JsonParser) Test(org.junit.Test)

Aggregations

JsonProvider (jakarta.json.spi.JsonProvider)6 Test (org.junit.Test)5 JsonbJsonpMapper (org.opensearch.client.json.jsonb.JsonbJsonpMapper)5 JsonGenerator (jakarta.json.stream.JsonGenerator)4 JsonParser (jakarta.json.stream.JsonParser)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 JsonParsingException (jakarta.json.stream.JsonParsingException)2 StringReader (java.io.StringReader)2 ChildClass (org.opensearch.client.opensearch.experiments.inheritance.child.ChildClass)2 FinalClass (org.opensearch.client.opensearch.experiments.inheritance.final_.FinalClass)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringWriter (java.io.StringWriter)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Assert (org.junit.Assert)1 FooRequest (org.opensearch.client.opensearch.experiments.api.FooRequest)1