use of jakarta.json.stream.JsonGenerator in project micronaut-serialization by micronaut-projects.
the class JsonStreamMapper method writeValue.
@Override
public <T> void writeValue(OutputStream outputStream, Argument<T> type, T object) throws IOException {
try (JsonGenerator generator = Json.createGenerator(Objects.requireNonNull(outputStream, "Output stream cannot be null"))) {
if (object == null) {
generator.writeNull();
} else {
JsonStreamEncoder encoder = new JsonStreamEncoder(generator);
serialize(encoder, object, type);
}
generator.flush();
}
}
use of jakarta.json.stream.JsonGenerator in project yasson by eclipse-ee4j.
the class NumberTest method testJsonpBigNumber.
/**
* Tests that JSON-P RI itself does no big number (out of IEEE 754 quotation).
* This is why it is now must be done in Yasson to match the JSONB spec.
*/
@Test
public void testJsonpBigNumber() {
StringWriter w = new StringWriter();
JsonGenerator generator = Json.createGenerator(w);
Long maxJsSafeValue = Double.valueOf(Math.pow(2, 53)).longValue() - 1;
Long upperJsUnsafeValue = Long.MAX_VALUE;
generator.writeStartObject();
generator.write("safeLongValue", maxJsSafeValue);
generator.write("unsafeLongValue", upperJsUnsafeValue);
generator.write("safeBigDecimalValue", BigDecimal.TEN);
generator.write("unsafeBigDecimalValue", BigDecimal.valueOf(upperJsUnsafeValue));
generator.writeEnd();
generator.close();
assertEquals("{" + "\"safeLongValue\":9007199254740991," + "\"unsafeLongValue\":9223372036854775807," + "\"safeBigDecimalValue\":10," + "\"unsafeBigDecimalValue\":9223372036854775807}", w.toString());
w = new StringWriter();
JsonWriter writer = Json.createWriter(w);
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
objectBuilder.add("safeLongValue", maxJsSafeValue);
objectBuilder.add("unsafeLongValue", upperJsUnsafeValue);
objectBuilder.add("safeBigDecimalValue", BigDecimal.valueOf(maxJsSafeValue));
objectBuilder.add("unsafeBigDecimalValue", BigDecimal.valueOf(upperJsUnsafeValue));
JsonObject build = objectBuilder.build();
writer.write(build);
writer.close();
assertEquals("{" + "\"safeLongValue\":9007199254740991," + "\"unsafeLongValue\":9223372036854775807," + "\"safeBigDecimalValue\":9007199254740991," + "\"unsafeBigDecimalValue\":9223372036854775807}", w.toString());
}
use of jakarta.json.stream.JsonGenerator in project yasson by eclipse-ee4j.
the class PreinstantiatedJsonpTest method testInvalidGeneratorWrappedWithUserInteraction.
@Test
public void testInvalidGeneratorWrappedWithUserInteraction() {
ByteArrayOutputStream out = new ByteArrayOutputStream();
JsonGenerator generator = new SuffixJsonGenerator("Best dog ever!", out);
generator.writeStartObject();
// key not written
Assertions.shouldFail(() -> bindingYassonJsonb.toJson(dog, generator));
}
use of jakarta.json.stream.JsonGenerator in project jackrabbit-oak by apache.
the class ElasticIndexUtils method toString.
/**
* Provides a string with the serialisation of the object.
* Typically, used to obtain the DSL representation of Elasticsearch requests or partial requests.
*
* TODO: remove this when <a href="https://github.com/elastic/elasticsearch-java/issues/101">#101</a> gets implemented
*
* @return Json serialisation as a string.
*/
public static String toString(JsonpSerializable query) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JsonpMapper mapper = new JacksonJsonpMapper();
JsonGenerator generator = mapper.jsonProvider().createGenerator(baos);
query.serialize(generator, mapper);
generator.close();
try {
return baos.toString("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Error creating request entity", e);
}
}
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()));
}
Aggregations