• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * 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 public class JSONPObject
18     implements JsonSerializable
19 {
20     /**
21      * JSONP function name to use for serialization
22      */
23     protected final String _function;
24 
25     /**
26      * Value to be serialized as JSONP padded; can be null.
27      */
28     protected final Object _value;
29 
30     /**
31      * Optional static type to use for serialization; if null, runtime
32      * type is used. Can be used to specify declared type which defines
33      * serializer to use, as well as aspects of extra type information
34      * to include (if any).
35      */
36     protected final JavaType _serializationType;
37 
JSONPObject(String function, Object value)38     public JSONPObject(String function, Object value) {
39         this(function, value, (JavaType) null);
40     }
41 
JSONPObject(String function, Object value, JavaType asType)42     public JSONPObject(String function, Object value, JavaType asType)
43     {
44         _function = function;
45         _value = value;
46         _serializationType = asType;
47     }
48 
49     /*
50     /**********************************************************
51     /* JsonSerializable(WithType) implementation
52     /**********************************************************
53      */
54 
55     @Override
serializeWithType(JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer)56     public void serializeWithType(JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer)
57             throws IOException
58     {
59         // No type for JSONP wrapping: value serializer will handle typing for value:
60         serialize(gen, provider);
61     }
62 
63     @Override
serialize(JsonGenerator gen, SerializerProvider provider)64     public void serialize(JsonGenerator gen, SerializerProvider provider)
65             throws IOException
66     {
67         // First, wrapping:
68         gen.writeRaw(_function);
69         gen.writeRaw('(');
70 
71         if (_value == null) {
72             provider.defaultSerializeNull(gen);
73         } else {
74             // NOTE: Escape line-separator characters that break JSONP only if no custom character escapes are set.
75             // If custom escapes are in place JSONP-breaking characters will not be escaped and it is recommended to
76             // add escaping for those (see JsonpCharacterEscapes class).
77             boolean override = (gen.getCharacterEscapes() == null);
78             if (override) {
79                 gen.setCharacterEscapes(JsonpCharacterEscapes.instance());
80             }
81 
82             try {
83                 if (_serializationType != null) {
84                     provider.findTypedValueSerializer(_serializationType, true, null).serialize(_value, gen, provider);
85                 } else {
86                     provider.findTypedValueSerializer(_value.getClass(), true, null).serialize(_value, gen, provider);
87                 }
88             } finally {
89                 if (override) {
90                     gen.setCharacterEscapes(null);
91                 }
92             }
93         }
94         gen.writeRaw(')');
95     }
96 
97     /*
98     /**************************************************************
99     /* Accessors
100     /**************************************************************
101      */
102 
getFunction()103     public String getFunction() { return _function; }
getValue()104     public Object getValue() { return _value; }
getSerializationType()105     public JavaType getSerializationType() { return _serializationType; }
106 }
107