1 package com.fasterxml.jackson.databind.util; 2 3 import java.io.IOException; 4 5 import com.fasterxml.jackson.core.*; 6 7 import com.fasterxml.jackson.databind.*; 8 import com.fasterxml.jackson.databind.jsontype.TypeSerializer; 9 10 /** 11 * General-purpose wrapper class that can be used to decorate serialized 12 * value with arbitrary literal prefix and suffix. This can be used for 13 * example to construct arbitrary Javascript values (similar to how basic 14 * function name and parenthesis are used with JSONP). 15 * 16 * @see com.fasterxml.jackson.databind.util.JSONPObject 17 */ 18 public class JSONWrappedObject implements JsonSerializable 19 { 20 /** 21 * Literal String to output before serialized value. 22 * Will not be quoted when serializing value. 23 */ 24 protected final String _prefix; 25 26 /** 27 * Literal String to output after serialized value. 28 * Will not be quoted when serializing value. 29 */ 30 protected final String _suffix; 31 32 /** 33 * Value to be serialized as JSONP padded; can be null. 34 */ 35 protected final Object _value; 36 37 /** 38 * Optional static type to use for serialization; if null, runtime 39 * type is used. Can be used to specify declared type which defines 40 * serializer to use, as well as aspects of extra type information 41 * to include (if any). 42 */ 43 protected final JavaType _serializationType; 44 JSONWrappedObject(String prefix, String suffix, Object value)45 public JSONWrappedObject(String prefix, String suffix, Object value) { 46 this(prefix, suffix, value, (JavaType) null); 47 } 48 49 /** 50 * Constructor that should be used when specific serialization type to use 51 * is important, and needs to be passed instead of just using runtime 52 * (type-erased) type of the value. 53 */ JSONWrappedObject(String prefix, String suffix, Object value, JavaType asType)54 public JSONWrappedObject(String prefix, String suffix, Object value, JavaType asType) 55 { 56 _prefix = prefix; 57 _suffix = suffix; 58 _value = value; 59 _serializationType = asType; 60 } 61 62 /* 63 /************************************************************** 64 /* JsonSerializable(WithType) implementation 65 /************************************************************** 66 */ 67 68 @Override serializeWithType(JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer)69 public void serializeWithType(JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) 70 throws IOException, JsonProcessingException 71 { 72 // No type for JSONP wrapping: value serializer will handle typing for value: 73 serialize(jgen, provider); 74 } 75 76 @Override serialize(JsonGenerator jgen, SerializerProvider provider)77 public void serialize(JsonGenerator jgen, SerializerProvider provider) 78 throws IOException, JsonProcessingException 79 { 80 // First, wrapping: 81 if (_prefix != null) jgen.writeRaw(_prefix); 82 if (_value == null) { 83 provider.defaultSerializeNull(jgen); 84 } else if (_serializationType != null) { 85 provider.findTypedValueSerializer(_serializationType, true, null).serialize(_value, jgen, provider); 86 } else { 87 Class<?> cls = _value.getClass(); 88 provider.findTypedValueSerializer(cls, true, null).serialize(_value, jgen, provider); 89 } 90 if (_suffix != null) jgen.writeRaw(_suffix); 91 } 92 93 /* 94 /************************************************************** 95 /* Accessors 96 /************************************************************** 97 */ 98 getPrefix()99 public String getPrefix() { return _prefix; } getSuffix()100 public String getSuffix() { return _suffix; } getValue()101 public Object getValue() { return _value; } getSerializationType()102 public JavaType getSerializationType() { return _serializationType; } 103 104 } 105