예제 #1
0
 @Override
 @SuppressWarnings("deprecation")
 public void serialize(JsonGenerator jgen, SerializerProvider provider)
     throws IOException, JsonProcessingException {
   // First, wrapping:
   if (_prefix != null) jgen.writeRaw(_prefix);
   if (_value == null) {
     provider.defaultSerializeNull(jgen);
   } else if (_serializationType != null) {
     provider
         .findTypedValueSerializer(_serializationType, true, null)
         .serialize(_value, jgen, provider);
   } else {
     Class<?> cls = _value.getClass();
     provider.findTypedValueSerializer(cls, true, null).serialize(_value, jgen, provider);
   }
   if (_suffix != null) jgen.writeRaw(_suffix);
 }
  @Override
  protected void writeInternal(Object o, HttpOutputMessage outputMessage)
      throws IOException, HttpMessageNotWritableException {

    JsonEncoding encoding = getEncoding(outputMessage.getHeaders().getContentType());
    JsonGenerator jsonGenerator =
        this.objectMapper.getJsonFactory().createJsonGenerator(outputMessage.getBody(), encoding);
    try {
      if (this.prefixJson) {
        jsonGenerator.writeRaw("{} && ");
      }
      this.objectMapper.writeValue(jsonGenerator, o);
    } catch (JsonGenerationException ex) {
      throw new HttpMessageNotWritableException("Could not write JSON: " + ex.getMessage(), ex);
    }
  }