Search in sources :

Example 1 with JsonWriter

use of jakarta.json.JsonWriter in project resteasy by resteasy.

the class JsonObjectProvider method writeTo.

@Override
public void writeTo(JsonObject jsonObject, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    LogMessages.LOGGER.debugf("Provider : %s,  Method : writeTo", getClass().getName());
    JsonWriter writer = findWriter(mediaType, entityStream);
    try {
        writer.writeObject(jsonObject);
    } finally {
        writer.close();
    }
}
Also used : JsonWriter(jakarta.json.JsonWriter)

Example 2 with JsonWriter

use of jakarta.json.JsonWriter in project resteasy by resteasy.

the class JsonStructureProvider method writeTo.

@Override
public void writeTo(JsonStructure jsonStructure, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    LogMessages.LOGGER.debugf("Provider : %s,  Method : writeTo", getClass().getName());
    JsonWriter writer = findWriter(mediaType, entityStream);
    try {
        writer.write(jsonStructure);
    } finally {
        writer.close();
    }
}
Also used : JsonWriter(jakarta.json.JsonWriter)

Example 3 with JsonWriter

use of jakarta.json.JsonWriter in project zilla by aklivity.

the class KafkaCachePartition method writeEntryFinish.

public void writeEntryFinish(ArrayFW<KafkaHeaderFW> headers, KafkaDeltaType deltaType) {
    final Node head = sentinel.previous;
    assert head != sentinel;
    final KafkaCacheSegment headSegment = head.segment;
    assert headSegment != null;
    final KafkaCacheFile logFile = headSegment.logFile();
    final KafkaCacheFile deltaFile = headSegment.deltaFile();
    final KafkaCacheFile hashFile = headSegment.hashFile();
    final KafkaCacheFile indexFile = headSegment.indexFile();
    final int logAvailable = logFile.available();
    final int logRequired = headers.sizeof();
    assert logAvailable >= logRequired : String.format("%s %d >= %d", headSegment, logAvailable, logRequired);
    logFile.appendBytes(headers);
    final long offsetDelta = (int) (progress - headSegment.baseOffset());
    final long indexEntry = (offsetDelta << 32) | logFile.markValue();
    if (!headers.isEmpty()) {
        final DirectBuffer buffer = headers.buffer();
        final ByteBuffer byteBuffer = buffer.byteBuffer();
        assert byteBuffer != null;
        byteBuffer.clear();
        headers.forEach(h -> {
            final long hash = computeHash(h);
            final long hashEntry = (hash << 32) | logFile.markValue();
            hashFile.appendLong(hashEntry);
        });
    }
    assert indexFile.available() >= Long.BYTES;
    indexFile.appendLong(indexEntry);
    final KafkaCacheEntryFW headEntry = logFile.readBytes(logFile.markValue(), headEntryRO::wrap);
    if (deltaType == JSON_PATCH && ancestorEntry != null && ancestorEntry.valueLen() != -1 && headEntry.valueLen() != -1) {
        final OctetsFW ancestorValue = ancestorEntry.value();
        final OctetsFW headValue = headEntry.value();
        assert headEntry.offset$() == progress;
        final JsonProvider json = JsonProvider.provider();
        ancestorIn.wrap(ancestorValue.buffer(), ancestorValue.offset(), ancestorValue.sizeof());
        final JsonReader ancestorReader = json.createReader(ancestorIn);
        final JsonStructure ancestorJson = ancestorReader.read();
        ancestorReader.close();
        headIn.wrap(headValue.buffer(), headValue.offset(), headValue.sizeof());
        final JsonReader headReader = json.createReader(headIn);
        final JsonStructure headJson = headReader.read();
        headReader.close();
        final JsonPatch diff = json.createDiff(ancestorJson, headJson);
        final JsonArray diffJson = diff.toJsonArray();
        diffOut.wrap(diffBuffer, Integer.BYTES);
        final JsonWriter writer = json.createWriter(diffOut);
        writer.write(diffJson);
        writer.close();
        // TODO: signal delta.sizeof > head.sizeof via null delta, otherwise delta file can exceed log file
        final int deltaLength = diffOut.position();
        diffBuffer.putInt(0, deltaLength);
        deltaFile.appendBytes(diffBuffer, 0, Integer.BYTES + deltaLength);
    }
    headSegment.lastOffset(progress);
}
Also used : OctetsFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW) ByteBuffer(java.nio.ByteBuffer) JsonProvider(jakarta.json.spi.JsonProvider) JsonPatch(jakarta.json.JsonPatch) JsonWriter(jakarta.json.JsonWriter) MutableDirectBuffer(org.agrona.MutableDirectBuffer) DirectBuffer(org.agrona.DirectBuffer) JsonArray(jakarta.json.JsonArray) JsonReader(jakarta.json.JsonReader) KafkaCacheEntryFW(io.aklivity.zilla.runtime.binding.kafka.internal.types.cache.KafkaCacheEntryFW) JsonStructure(jakarta.json.JsonStructure)

Example 4 with JsonWriter

use of jakarta.json.JsonWriter in project glassfish-hk2 by eclipse-ee4j.

the class JsonParser method marshal.

/* (non-Javadoc)
     * @see org.glassfish.hk2.xml.spi.XmlServiceParser#marshall(java.io.OutputStream, org.glassfish.hk2.xml.api.XmlRootHandle)
     */
@Override
public <T> void marshal(OutputStream outputStream, XmlRootHandle<T> rootHandle, Map<String, Object> options) throws IOException {
    JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
    T root = rootHandle.getRoot();
    JsonObject rootObject = createJsonObject((BaseHK2JAXBBean) root, objectBuilder);
    Map<String, Object> config = new HashMap<String, Object>();
    config.put(JsonGenerator.PRETTY_PRINTING, Boolean.TRUE);
    JsonWriterFactory writerFactory = Json.createWriterFactory(config);
    JsonWriter writer = writerFactory.createWriter(outputStream);
    try {
        writer.writeObject(rootObject);
    } finally {
        writer.close();
    }
}
Also used : HashMap(java.util.HashMap) JsonObject(jakarta.json.JsonObject) JsonObject(jakarta.json.JsonObject) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) JsonWriter(jakarta.json.JsonWriter) JsonWriterFactory(jakarta.json.JsonWriterFactory)

Example 5 with JsonWriter

use of jakarta.json.JsonWriter in project avro-util by linkedin.

the class JsonPropertiesContainerImpl method toString.

@Override
public String toString() {
    if (props.isEmpty()) {
        return "{}";
    }
    JsonObjectBuilder ob = Json.createObjectBuilder();
    props.forEach(ob::add);
    JsonObject object = ob.build();
    StringWriter sw = new StringWriter();
    try (JsonWriter writer = JSON_FACTORY.createWriter(sw)) {
        writer.write(object);
    }
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) JsonObject(jakarta.json.JsonObject) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) JsonWriter(jakarta.json.JsonWriter)

Aggregations

JsonWriter (jakarta.json.JsonWriter)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 JsonObject (jakarta.json.JsonObject)3 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)3 SQLiteConnection (com.almworks.sqlite4java.SQLiteConnection)2 SQLiteStatement (com.almworks.sqlite4java.SQLiteStatement)2 JsonArray (jakarta.json.JsonArray)2 JsonArrayBuilder (jakarta.json.JsonArrayBuilder)2 JsonException (jakarta.json.JsonException)2 JsonPatch (jakarta.json.JsonPatch)2 JsonReader (jakarta.json.JsonReader)2 IOException (java.io.IOException)2 OutputStream (java.io.OutputStream)2 StringWriter (java.io.StringWriter)2 GeneralSecurityException (java.security.GeneralSecurityException)2 DeviceInfo (app.attestation.server.AttestationProtocol.DeviceInfo)1 HTTPRequestWriterException (com.github.ljtfreitas.julian.http.codec.HTTPRequestWriterException)1 JsonValueExt (com.linkedin.avroutil1.parser.jsonpext.JsonValueExt)1 OctetsFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.OctetsFW)1 KafkaCacheEntryFW (io.aklivity.zilla.runtime.binding.kafka.internal.types.cache.KafkaCacheEntryFW)1