Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind.util; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import com.fasterxml.jackson.core.*; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 6 | |
| 7 | import com.fasterxml.jackson.databind.*; |
Tatu Saloranta | df6302f | 2011-12-23 20:05:35 -0800 | [diff] [blame] | 8 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Container class that can be used to wrap any Object instances (including |
| 12 | * nulls), and will serialize embedded in |
| 13 | * <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> wrapping. |
| 14 | * |
| 15 | * @see com.fasterxml.jackson.databind.util.JSONWrappedObject |
| 16 | * |
| 17 | * @author tatu |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 18 | */ |
| 19 | public class JSONPObject |
Tatu Saloranta | fcb1c25 | 2011-12-23 18:34:27 -0800 | [diff] [blame] | 20 | implements JsonSerializable |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 21 | { |
| 22 | /** |
| 23 | * JSONP function name to use for serialization |
| 24 | */ |
| 25 | protected final String _function; |
| 26 | |
| 27 | /** |
| 28 | * Value to be serialized as JSONP padded; can be null. |
| 29 | */ |
| 30 | protected final Object _value; |
| 31 | |
| 32 | /** |
| 33 | * Optional static type to use for serialization; if null, runtime |
| 34 | * type is used. Can be used to specify declared type which defines |
| 35 | * serializer to use, as well as aspects of extra type information |
| 36 | * to include (if any). |
| 37 | */ |
| 38 | protected final JavaType _serializationType; |
| 39 | |
| 40 | public JSONPObject(String function, Object value) { |
| 41 | this(function, value, (JavaType) null); |
| 42 | } |
| 43 | |
| 44 | public JSONPObject(String function, Object value, JavaType asType) |
| 45 | { |
| 46 | _function = function; |
| 47 | _value = value; |
| 48 | _serializationType = asType; |
| 49 | } |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 50 | |
| 51 | /* |
| 52 | /********************************************************** |
| 53 | /* JsonSerializable(WithType) implementation |
| 54 | /********************************************************** |
| 55 | */ |
| 56 | |
Tatu Saloranta | f1c79d4 | 2012-04-17 07:57:41 -0700 | [diff] [blame^] | 57 | // @Override |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 58 | public void serializeWithType(JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) |
| 59 | throws IOException, JsonProcessingException |
| 60 | { |
| 61 | // No type for JSONP wrapping: value serializer will handle typing for value: |
| 62 | serialize(jgen, provider); |
| 63 | } |
| 64 | |
Tatu Saloranta | f1c79d4 | 2012-04-17 07:57:41 -0700 | [diff] [blame^] | 65 | // @Override |
Tatu Saloranta | e4f23bb | 2011-12-23 00:31:35 -0800 | [diff] [blame] | 66 | public void serialize(JsonGenerator jgen, SerializerProvider provider) |
| 67 | throws IOException, JsonProcessingException |
| 68 | { |
| 69 | // First, wrapping: |
| 70 | jgen.writeRaw(_function); |
| 71 | jgen.writeRaw('('); |
| 72 | if (_value == null) { |
| 73 | provider.defaultSerializeNull(jgen); |
| 74 | } else if (_serializationType != null) { |
| 75 | provider.findTypedValueSerializer(_serializationType, true, null).serialize(_value, jgen, provider); |
| 76 | } else { |
| 77 | Class<?> cls = _value.getClass(); |
| 78 | provider.findTypedValueSerializer(cls, true, null).serialize(_value, jgen, provider); |
| 79 | } |
| 80 | jgen.writeRaw(')'); |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | /************************************************************** |
| 85 | /* Accessors |
| 86 | /************************************************************** |
| 87 | */ |
| 88 | |
| 89 | public String getFunction() { return _function; } |
| 90 | public Object getValue() { return _value; } |
| 91 | public JavaType getSerializationType() { return _serializationType; } |
| 92 | } |