Search in sources :

Example 91 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project vespa by vespa-engine.

the class JRTServerConfigRequestV3 method addOkResponse.

@Override
public void addOkResponse(Payload payload, long generation, String configMd5) {
    boolean changedConfig = !configMd5.equals(getRequestConfigMd5());
    boolean changedConfigAndNewGeneration = changedConfig && ConfigUtils.isGenerationNewer(generation, getRequestGeneration());
    Payload responsePayload = payload.withCompression(getCompressionType());
    ByteArrayOutputStream byteArrayOutputStream = new NoCopyByteArrayOutputStream(4096);
    try {
        JsonGenerator jsonGenerator = createJsonGenerator(byteArrayOutputStream);
        jsonGenerator.writeStartObject();
        addCommonReturnValues(jsonGenerator);
        setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_CONFIG_MD5, configMd5);
        setResponseField(jsonGenerator, SlimeResponseData.RESPONSE_CONFIG_GENERATION, generation);
        jsonGenerator.writeObjectFieldStart(SlimeResponseData.RESPONSE_COMPRESSION_INFO);
        if (responsePayload == null) {
            throw new RuntimeException("Payload is null for ' " + this + ", not able to create response");
        }
        CompressionInfo compressionInfo = responsePayload.getCompressionInfo();
        // If payload is not being sent, we must adjust compression info to avoid client confusion.
        if (!changedConfigAndNewGeneration) {
            compressionInfo = CompressionInfo.create(compressionInfo.getCompressionType(), 0);
        }
        compressionInfo.serialize(jsonGenerator);
        jsonGenerator.writeEndObject();
        if (log.isLoggable(LogLevel.SPAM)) {
            log.log(LogLevel.SPAM, getConfigKey() + ": response dataXXXXX" + payload.withCompression(CompressionType.UNCOMPRESSED) + "XXXXX");
        }
        jsonGenerator.writeEndObject();
        jsonGenerator.close();
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not add OK response for " + this);
    }
    request.returnValues().add(createResponseValue(byteArrayOutputStream));
    if (changedConfigAndNewGeneration) {
        request.returnValues().add(new DataValue(responsePayload.getData().getBytes()));
    } else {
        request.returnValues().add(new DataValue(new byte[0]));
    }
}
Also used : DataValue(com.yahoo.jrt.DataValue) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 92 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project vespa by vespa-engine.

the class AccessLogRequestHandler method handle.

@Override
public HttpResponse handle(HttpRequest request) {
    final List<String> uris = circularArrayAccessLogKeeper.getUris();
    return new HttpResponse(200) {

        @Override
        public void render(OutputStream outputStream) throws IOException {
            JsonGenerator generator = jsonFactory.createGenerator(outputStream);
            generator.writeStartObject();
            generator.writeArrayFieldStart("entries");
            for (String uri : uris) {
                generator.writeStartObject();
                generator.writeStringField("url", uri);
                generator.writeEndObject();
            }
            generator.writeEndArray();
            generator.writeEndObject();
            generator.close();
        }
    };
}
Also used : OutputStream(java.io.OutputStream) HttpResponse(com.yahoo.container.jdisc.HttpResponse) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator)

Example 93 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project endpoints-java by cloudendpoints.

the class ServletResponseResultWriter method getWriteDateAndTimeAsStringModule.

private static SimpleModule getWriteDateAndTimeAsStringModule() {
    JsonSerializer<DateAndTime> dateAndTimeSerializer = new JsonSerializer<DateAndTime>() {

        @Override
        public void serialize(DateAndTime value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
            jgen.writeString(value.toRfc3339String());
        }
    };
    SimpleModule writeDateAsStringModule = new SimpleModule("writeDateAsStringModule", new Version(1, 0, 0, null, null, null));
    writeDateAsStringModule.addSerializer(DateAndTime.class, dateAndTimeSerializer);
    return writeDateAsStringModule;
}
Also used : Version(com.fasterxml.jackson.core.Version) DateAndTime(com.google.api.server.spi.types.DateAndTime) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SerializerProvider(com.fasterxml.jackson.databind.SerializerProvider) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 94 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project walkmod-core by walkmod.

the class YAMLConfigurationProvider method write.

public void write(JsonNode node) throws TransformerException {
    if (node != null) {
        File cfg = new File(fileName);
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(cfg);
            JsonGenerator generator = mapper.getFactory().createGenerator(fos);
            generator.useDefaultPrettyPrinter();
            mapper.writeTree(generator, node);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new TransformerException("Error writting the configuration", e);
                }
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) IOException(java.io.IOException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ConfigurationException(org.walkmod.conf.ConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Example 95 with JsonGenerator

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator in project kripton by xcesco.

the class DaoBean05Impl method serializer1.

/**
 * for param serializer1 serialization
 */
private byte[] serializer1(Long value) {
    if (value == null) {
        return null;
    }
    KriptonJsonContext context = KriptonBinder.jsonBind();
    try (KriptonByteArrayOutputStream stream = new KriptonByteArrayOutputStream();
        JacksonWrapperSerializer wrapper = context.createSerializer(stream)) {
        JsonGenerator jacksonSerializer = wrapper.jacksonGenerator;
        int fieldCount = 0;
        jacksonSerializer.writeStartObject();
        if (value != null) {
            jacksonSerializer.writeNumberField("element", value);
        }
        jacksonSerializer.writeEndObject();
        jacksonSerializer.flush();
        return stream.toByteArray();
    } catch (Exception e) {
        throw (new KriptonRuntimeException(e.getMessage()));
    }
}
Also used : JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException) KriptonByteArrayOutputStream(com.abubusoft.kripton.common.KriptonByteArrayOutputStream) KriptonJsonContext(com.abubusoft.kripton.KriptonJsonContext) JacksonWrapperSerializer(com.abubusoft.kripton.persistence.JacksonWrapperSerializer) KriptonRuntimeException(com.abubusoft.kripton.exception.KriptonRuntimeException)

Aggregations

JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)704 KriptonJsonContext (com.abubusoft.kripton.KriptonJsonContext)257 KriptonByteArrayOutputStream (com.abubusoft.kripton.common.KriptonByteArrayOutputStream)257 KriptonRuntimeException (com.abubusoft.kripton.exception.KriptonRuntimeException)257 JacksonWrapperSerializer (com.abubusoft.kripton.persistence.JacksonWrapperSerializer)257 IOException (java.io.IOException)169 StringWriter (java.io.StringWriter)144 JsonFactory (com.fasterxml.jackson.core.JsonFactory)101 ByteArrayOutputStream (java.io.ByteArrayOutputStream)66 Map (java.util.Map)57 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)54 HashMap (java.util.HashMap)40 Test (org.junit.Test)30 File (java.io.File)27 ArrayList (java.util.ArrayList)25 OutputStream (java.io.OutputStream)24 Writer (java.io.Writer)22 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)21 SerializerProvider (com.fasterxml.jackson.databind.SerializerProvider)20 FileOutputStream (java.io.FileOutputStream)19