Example usage for com.fasterxml.jackson.core JsonGenerator writeRaw

List of usage examples for com.fasterxml.jackson.core JsonGenerator writeRaw

Introduction

In this page you can find the example usage for com.fasterxml.jackson.core JsonGenerator writeRaw.

Prototype

public void writeRaw(SerializableString raw) throws IOException, JsonGenerationException 

Source Link

Document

Method that will force generator to copy input text verbatim with no modifications (including that no escaping is done and no separators are added even if context [array, object] would otherwise require such).

Usage

From source file:org.ccntgrid.extend.spring.http.converter.json.MappingJackson2HttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);
    try {//w  w w.j  a  v  a  2s.  co  m
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:tuwien.aic.crowdsourcing.util.MappingJackson2HttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getFactory().createJsonGenerator(outputMessage.getBody(),
            encoding);//from   w w  w  .  jav a  2  s  .c om
    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        if (this.prettyPrint) {
            jsonGenerator.useDefaultPrettyPrinter();
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:webFramework.MappingJackson2PrettyJsonView.java

@Override
protected void writePrefix(JsonGenerator generator, Object object) throws IOException {
    if (this.jsonPrefix != null) {
        generator.writeRaw(this.jsonPrefix);
    }//from w w w.  j av  a  2 s.  c  o m

    String jsonpFunction = null;
    if (object instanceof MappingJacksonValue) {
        jsonpFunction = ((MappingJacksonValue) object).getJsonpFunction();
    }
    if (jsonpFunction != null) {
        generator.writeRaw("/**/");
        generator.writeRaw(jsonpFunction + "(");
    }
}

From source file:webFramework.MappingJackson2PrettyJsonView.java

@Override
protected void writeSuffix(JsonGenerator generator, Object object) throws IOException {
    String jsonpFunction = null;//from www .ja v  a2  s  .c o m
    if (object instanceof MappingJacksonValue) {
        jsonpFunction = ((MappingJacksonValue) object).getJsonpFunction();
    }
    if (jsonpFunction != null) {
        generator.writeRaw(");");
    }
}

From source file:com.predic8.membrane.core.interceptor.apimanagement.statistics.AMStatisticsCollector.java

private String combineJsons(String name, ArrayList<String> jsonStatisticsForRequests) throws IOException {
    JsonGenerator gen = getAndResetJsonGenerator();

    try {/*from  ww w  .j ava2  s .co m*/
        gen.writeStartObject();
        gen.writeArrayFieldStart(name);
        if (!jsonStatisticsForRequests.isEmpty())
            gen.writeRaw(jsonStatisticsForRequests.get(0));
        for (int i = 1; i < jsonStatisticsForRequests.size(); i++) {
            gen.writeRaw("," + jsonStatisticsForRequests.get(i));
        }
        gen.writeEndArray();
        gen.writeEndObject();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return getStringFromJsonGenerator();
}

From source file:net.opentsdb.contrib.tsquare.web.view.AbstractJsonResponseWriter.java

@Override
public void beginResponse(final ResponseContext context) throws IOException {
    context.getResponse().setContentType(contentType);

    final OutputStream out = context.getResponse().getOutputStream();
    final JsonGenerator json = new JsonFactory().createGenerator(out);
    context.putProperty("jsonGenerator", json);

    // By default.
    context.putProperty("isJsonpResponse", Boolean.FALSE);

    if (jsonpAllowed) {
        final boolean hasParams = context.getRequest().getParameterMap().containsKey(jsonpRequestParam);

        if (hasParams) {
            context.putProperty("isJsonpResponse", Boolean.TRUE);

            final String jsonpValue = context.getRequest().getParameter(jsonpRequestParam);
            if (!Strings.isNullOrEmpty(jsonpValue)) {
                json.writeRaw(jsonpValue);
            }/* w  ww .j  a v a 2s. c o  m*/

            json.writeRaw('('); // START of jsonp response wrapper.
        }
    }
}

From source file:org.resthub.web.converter.MappingJackson2JsonHttpMessageConverter.java

@Override
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }// w  w w  . j a  va 2s .c o m

    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("{} && ");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:com.wq.common.web.springmvc.MappingJackson2HttpMessageConverter.java

@SuppressWarnings("deprecation")
@Override/*  w  w  w. ja  va2s.c o m*/
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }

    try {
        if (this.prefixJson) {
            jsonGenerator.writeRaw("[] && ");
        }
        // jsonp??
        boolean isJSONP = !ObjectUtil.isEmpty(WebContext.getRequest().getParameter(DEFAULT_CALLBACK));
        if (isJSONP) {
            WebContext.getResponse().setContentType("application/x-javascript");
            jsonGenerator.writeRaw(WebContext.getRequest().getParameter(DEFAULT_CALLBACK) + "(");
        }
        this.objectMapper.writeValue(jsonGenerator, object);
        if (isJSONP) {
            outputMessage.getBody().write(')');
        }
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}

From source file:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.java

@SuppressWarnings("deprecation")
@Override/* w w w  .j a v  a2  s. co m*/
protected void writeInternal(Object object, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getJsonEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator = this.objectMapper.getJsonFactory()
            .createJsonGenerator(outputMessage.getBody(), encoding);

    // A workaround for JsonGenerators not applying serialization features
    // https://github.com/FasterXML/jackson-databind/issues/12
    if (this.objectMapper.isEnabled(SerializationFeature.INDENT_OUTPUT)) {
        jsonGenerator.useDefaultPrettyPrinter();
    }

    try {
        if (this.jsonPrefix != null) {
            jsonGenerator.writeRaw(this.jsonPrefix);
        }
        this.objectMapper.writeValue(jsonGenerator, object);
    } catch (JsonProcessingException ex) {
        throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
}