blob: 9f7807d7ffdb9cf130960c1a93fd52a8c5d60127 [file] [log] [blame]
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08001package com.fasterxml.jackson.databind.util;
2
3import java.io.IOException;
4
5import com.fasterxml.jackson.core.*;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08006
7import com.fasterxml.jackson.databind.*;
Tatu Salorantadf6302f2011-12-23 20:05:35 -08008import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
Tatu Salorantae4f23bb2011-12-23 00:31:35 -08009
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 Salorantae4f23bb2011-12-23 00:31:35 -080018 */
19public class JSONPObject
Tatu Salorantafcb1c252011-12-23 18:34:27 -080020 implements JsonSerializable
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080021{
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 Salorantae4f23bb2011-12-23 00:31:35 -080050
51 /*
52 /**********************************************************
53 /* JsonSerializable(WithType) implementation
54 /**********************************************************
55 */
56
Tatu Salorantaf1c79d42012-04-17 07:57:41 -070057// @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080058 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 Salorantaf1c79d42012-04-17 07:57:41 -070065// @Override
Tatu Salorantae4f23bb2011-12-23 00:31:35 -080066 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}