|
4 | 4 |
|
5 | 5 | import com.fasterxml.jackson.core.*; |
6 | 6 |
|
7 | | -import com.fasterxml.jackson.core.io.CharacterEscapes; |
8 | 7 | import com.fasterxml.jackson.databind.*; |
9 | 8 | import com.fasterxml.jackson.databind.jsontype.TypeSerializer; |
10 | 9 |
|
|
14 | 13 | * <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> wrapping. |
15 | 14 | * |
16 | 15 | * @see com.fasterxml.jackson.databind.util.JSONWrappedObject |
17 | | - * |
18 | | - * @author tatu |
19 | 16 | */ |
20 | 17 | public class JSONPObject |
21 | 18 | implements JsonSerializable |
@@ -56,42 +53,45 @@ public JSONPObject(String function, Object value, JavaType asType) |
56 | 53 | */ |
57 | 54 |
|
58 | 55 | @Override |
59 | | - public void serializeWithType(JsonGenerator jgen, SerializerProvider provider, TypeSerializer typeSer) |
60 | | - throws IOException, JsonProcessingException |
| 56 | + public void serializeWithType(JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer) |
| 57 | + throws IOException |
61 | 58 | { |
62 | 59 | // No type for JSONP wrapping: value serializer will handle typing for value: |
63 | | - serialize(jgen, provider); |
| 60 | + serialize(gen, provider); |
64 | 61 | } |
65 | 62 |
|
66 | 63 | @Override |
67 | | - public void serialize(JsonGenerator jgen, SerializerProvider provider) |
68 | | - throws IOException, JsonProcessingException |
| 64 | + public void serialize(JsonGenerator gen, SerializerProvider provider) |
| 65 | + throws IOException |
69 | 66 | { |
70 | | - CharacterEscapes currentCharacterEscapes = jgen.getCharacterEscapes(); |
| 67 | + // First, wrapping: |
| 68 | + gen.writeRaw(_function); |
| 69 | + gen.writeRaw('('); |
71 | 70 |
|
72 | | - // NOTE: Escape line-separator characters that break JSONP only if no custom character escapes are set. |
73 | | - // If custom escapes are in place JSONP-breaking characters will not be escaped and it is recommended to |
74 | | - // add escaping for those (see JsonpCharacterEscapes class). |
75 | | - if (currentCharacterEscapes == null) { |
76 | | - jgen.setCharacterEscapes(JsonpCharacterEscapes.instance()); |
77 | | - } |
| 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 | + } |
78 | 81 |
|
79 | | - try { |
80 | | - // First, wrapping: |
81 | | - jgen.writeRaw(_function); |
82 | | - jgen.writeRaw('('); |
83 | | - if (_value == null) { |
84 | | - provider.defaultSerializeNull(jgen); |
85 | | - } else if (_serializationType != null) { |
86 | | - provider.findTypedValueSerializer(_serializationType, true, null).serialize(_value, jgen, provider); |
87 | | - } else { |
88 | | - Class<?> cls = _value.getClass(); |
89 | | - provider.findTypedValueSerializer(cls, true, null).serialize(_value, jgen, provider); |
| 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 | + } |
90 | 92 | } |
91 | | - jgen.writeRaw(')'); |
92 | | - } finally { |
93 | | - jgen.setCharacterEscapes(currentCharacterEscapes); |
94 | 93 | } |
| 94 | + gen.writeRaw(')'); |
95 | 95 | } |
96 | 96 |
|
97 | 97 | /* |
|
0 commit comments