Search in sources :

Example 6 with JsonGenerator

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();
    }
}
Also used : JsonGenerator(jakarta.json.stream.JsonGenerator)

Example 7 with JsonGenerator

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());
}
Also used : StringWriter(java.io.StringWriter) JsonGenerator(jakarta.json.stream.JsonGenerator) JsonObject(jakarta.json.JsonObject) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) JsonWriter(jakarta.json.JsonWriter)

Example 8 with JsonGenerator

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));
}
Also used : JsonGenerator(jakarta.json.stream.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 9 with JsonGenerator

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);
    }
}
Also used : JsonpMapper(co.elastic.clients.json.JsonpMapper) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) JacksonJsonpMapper(co.elastic.clients.json.jackson.JacksonJsonpMapper) JsonGenerator(jakarta.json.stream.JsonGenerator) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 10 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)

Aggregations

JsonGenerator (jakarta.json.stream.JsonGenerator)40 StringWriter (java.io.StringWriter)17 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Test (org.junit.Test)9 JsonProvider (jakarta.json.spi.JsonProvider)8 JsonParser (jakarta.json.stream.JsonParser)5 JsonbJsonpMapper (co.elastic.clients.json.jsonb.JsonbJsonpMapper)4 JsonGeneratorResult (org.eclipse.persistence.oxm.json.JsonGeneratorResult)4 JsonbJsonpMapper (org.opensearch.client.json.jsonb.JsonbJsonpMapper)4 JsonValue (jakarta.json.JsonValue)3 JsonGeneratorFactory (jakarta.json.stream.JsonGeneratorFactory)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 StringReader (java.io.StringReader)3 JsonpMapper (co.elastic.clients.json.JsonpMapper)2 NdJsonpSerializable (co.elastic.clients.json.NdJsonpSerializable)2 JacksonJsonpMapper (co.elastic.clients.json.jackson.JacksonJsonpMapper)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonObject (jakarta.json.JsonObject)2 JsonString (jakarta.json.JsonString)2 JsonParsingException (jakarta.json.stream.JsonParsingException)2