Skip to content

Commit d89cfa1

Browse files
committed
Merge branch '2.8'
2 parents f3d729a + 513ab6e commit d89cfa1

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

release-notes/CREDITS

+5-1
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,11 @@ Joshua Jones
603603
Ivo Studens ([email protected])
604604
* Contributed #1585: Invoke ServiceLoader.load() inside of a privileged block
605605
when loading modules using `ObjectMapper.findModules()`
606-
(2.8.9)
606+
(2.8.9)
607+
608+
Marco Catania ([email protected])
609+
* Contributed #1597: Escape JSONP breaking characters
610+
(2.8.9)
607611

608612
Connor Kuhn (ckuhn@github)
609613
* Contributed #1341: FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY

release-notes/VERSION

+5
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ Project: jackson-databind
7777

7878
2.8.9 (not yet released)
7979

80+
#1597: Escape JSONP breaking characters
81+
(contributed by Marco C)
82+
83+
2.8.8.1 (19-Apr-2017)
84+
8085
#1585: Invoke ServiceLoader.load() inside of a privileged block when loading
8186
modules using `ObjectMapper.findModules()`
8287
(contributed by Ivo S)

src/main/java/com/fasterxml/jackson/databind/util/JSONPObject.java

+29-29
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import com.fasterxml.jackson.core.*;
66

7-
import com.fasterxml.jackson.core.io.CharacterEscapes;
87
import com.fasterxml.jackson.databind.*;
98
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
109

@@ -14,8 +13,6 @@
1413
* <a href="http://en.wikipedia.org/wiki/JSONP">JSONP</a> wrapping.
1514
*
1615
* @see com.fasterxml.jackson.databind.util.JSONWrappedObject
17-
*
18-
* @author tatu
1916
*/
2017
public class JSONPObject
2118
implements JsonSerializable
@@ -56,42 +53,45 @@ public JSONPObject(String function, Object value, JavaType asType)
5653
*/
5754

5855
@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
6158
{
6259
// No type for JSONP wrapping: value serializer will handle typing for value:
63-
serialize(jgen, provider);
60+
serialize(gen, provider);
6461
}
6562

6663
@Override
67-
public void serialize(JsonGenerator jgen, SerializerProvider provider)
68-
throws IOException, JsonProcessingException
64+
public void serialize(JsonGenerator gen, SerializerProvider provider)
65+
throws IOException
6966
{
70-
CharacterEscapes currentCharacterEscapes = jgen.getCharacterEscapes();
67+
// First, wrapping:
68+
gen.writeRaw(_function);
69+
gen.writeRaw('(');
7170

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+
}
7881

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+
}
9092
}
91-
jgen.writeRaw(')');
92-
} finally {
93-
jgen.setCharacterEscapes(currentCharacterEscapes);
9493
}
94+
gen.writeRaw(')');
9595
}
9696

9797
/*

0 commit comments

Comments
 (0)