|
13 | 13 | * <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> wrapping.
|
14 | 14 | *
|
15 | 15 | * @see com.fasterxml.jackson.databind.util.JSONWrappedObject
|
16 |
| - * |
17 |
| - * @author tatu |
18 | 16 | */
|
19 | 17 | public class JSONPObject
|
20 | 18 | implements JsonSerializable
|
@@ -55,29 +53,45 @@ public JSONPObject(String function, Object value, JavaType asType)
|
55 | 53 | */
|
56 | 54 |
|
57 | 55 | @Override
|
58 |
| - public void serializeWithType(JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) |
59 |
| - throws IOException, JsonProcessingException |
| 56 | + public void serializeWithType(JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer) |
| 57 | + throws IOException |
60 | 58 | {
|
61 | 59 | // No type for JSONP wrapping: value serializer will handle typing for value:
|
62 |
| - serialize(jgen, provider); |
| 60 | + serialize(gen, provider); |
63 | 61 | }
|
64 | 62 |
|
65 | 63 | @Override
|
66 |
| - public void serialize(JsonGenerator jgen, SerializerProvider provider) |
67 |
| - throws IOException, JsonProcessingException |
| 64 | + public void serialize(JsonGenerator gen, SerializerProvider provider) |
| 65 | + throws IOException |
68 | 66 | {
|
69 | 67 | // First, wrapping:
|
70 |
| - jgen.writeRaw(_function); |
71 |
| - jgen.writeRaw('('); |
| 68 | + gen.writeRaw(_function); |
| 69 | + gen.writeRaw('('); |
| 70 | + |
72 | 71 | if (_value == null) {
|
73 |
| - provider.defaultSerializeNull(jgen); |
74 |
| - } else if (_serializationType != null) { |
75 |
| - provider.findTypedValueSerializer(_serializationType, true, null).serialize(_value, jgen, provider); |
| 72 | + provider.defaultSerializeNull(gen); |
76 | 73 | } else {
|
77 |
| - Class<?> cls = _value.getClass(); |
78 |
| - provider.findTypedValueSerializer(cls, true, null).serialize(_value, jgen, provider); |
| 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 | + } |
79 | 93 | }
|
80 |
| - jgen.writeRaw(')'); |
| 94 | + gen.writeRaw(')'); |
81 | 95 | }
|
82 | 96 |
|
83 | 97 | /*
|
|
0 commit comments