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