Search in sources :

Example 1 with JsonGenerator

use of jakarta.json.stream.JsonGenerator in project opensearch-java by opensearch-project.

the class JsonDataImpl method getParser.

private JsonParser getParser(JsonpMapper mapper) {
    // FIXME: inefficient roundtrip through a string. Should be replaced by an Event buffer structure.
    StringWriter sw = new StringWriter();
    JsonGenerator generator = mapper.jsonProvider().createGenerator(sw);
    if (value instanceof JsonValue) {
        generator.write((JsonValue) value);
    } else {
        mapper.serialize(value, generator);
    }
    generator.close();
    return mapper.jsonProvider().createParser(new StringReader(sw.toString()));
}
Also used : StringWriter(java.io.StringWriter) JsonValue(jakarta.json.JsonValue) StringReader(java.io.StringReader) JsonGenerator(jakarta.json.stream.JsonGenerator)

Example 2 with JsonGenerator

use of jakarta.json.stream.JsonGenerator in project opensearch-java by opensearch-project.

the class JsonpMapperTest method testSerialize.

private void testSerialize(JsonpMapper mapper, String expected) {
    SomeClass something = new SomeClass();
    something.setIntValue(1);
    something.setDoubleValue(2.1);
    something.setStringValue("foo");
    SomeClass other = new SomeClass();
    other.setIntValue(2);
    other.setDoubleValue(3.2);
    something.setChildren(Collections.singletonList(other));
    StringWriter strw = new StringWriter();
    JsonGenerator generator = mapper.jsonProvider().createGenerator(strw);
    mapper.serialize(something, generator);
    generator.close();
    assertEquals(expected, strw.getBuffer().toString());
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(jakarta.json.stream.JsonGenerator)

Example 3 with JsonGenerator

use of jakarta.json.stream.JsonGenerator 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 4 with JsonGenerator

use of jakarta.json.stream.JsonGenerator 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 5 with JsonGenerator

use of jakarta.json.stream.JsonGenerator 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)

Aggregations

JsonGenerator (jakarta.json.stream.JsonGenerator)9 JsonProvider (jakarta.json.spi.JsonProvider)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 StringWriter (java.io.StringWriter)4 Test (org.junit.Test)4 JsonbJsonpMapper (org.opensearch.client.json.jsonb.JsonbJsonpMapper)4 JsonParser (jakarta.json.stream.JsonParser)2 StringReader (java.io.StringReader)2 NdJsonpSerializable (org.opensearch.client.json.NdJsonpSerializable)2 JsonString (jakarta.json.JsonString)1 JsonValue (jakarta.json.JsonValue)1 JsonParsingException (jakarta.json.stream.JsonParsingException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)1 Assert (org.junit.Assert)1 RequestOptions (org.opensearch.client.RequestOptions)1 JsonData (org.opensearch.client.json.JsonData)1 JsonpMapper (org.opensearch.client.json.JsonpMapper)1 FooRequest (org.opensearch.client.opensearch.experiments.api.FooRequest)1