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()));
}
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());
}
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();
}
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);
}
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());
}
Aggregations