Skip to content

Commit d849d3b

Browse files
committed
more test additions, code cleanup
1 parent 489659c commit d849d3b

File tree

10 files changed

+156
-58
lines changed

10 files changed

+156
-58
lines changed

src/main/java/com/fasterxml/jackson/databind/ObjectReader.java

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ObjectReader
4242
extends ObjectCodec
4343
implements Versioned, java.io.Serializable // since 2.1
4444
{
45-
private static final long serialVersionUID = 1L; // since 2.5
45+
private static final long serialVersionUID = 2L; // since 2.9
4646

4747
private final static JavaType JSON_NODE_TYPE = SimpleType.constructUnsafe(JsonNode.class);
4848

@@ -180,9 +180,6 @@ protected ObjectReader(ObjectMapper mapper, DeserializationConfig config,
180180
_parserFactory = mapper._jsonFactory;
181181
_valueType = valueType;
182182
_valueToUpdate = valueToUpdate;
183-
if (valueToUpdate != null && valueType.isArrayType()) {
184-
throw new IllegalArgumentException("Can not update an array value");
185-
}
186183
_schema = schema;
187184
_injectableValues = injectableValues;
188185
_unwrapRoot = config.useRootWrapping();
@@ -209,9 +206,6 @@ protected ObjectReader(ObjectReader base, DeserializationConfig config,
209206
_valueType = valueType;
210207
_rootDeserializer = rootDeser;
211208
_valueToUpdate = valueToUpdate;
212-
if (valueToUpdate != null && valueType.isArrayType()) {
213-
throw new IllegalArgumentException("Can not update an array value");
214-
}
215209
_schema = schema;
216210
_injectableValues = injectableValues;
217211
_unwrapRoot = config.useRootWrapping();
@@ -1015,7 +1009,7 @@ public <T> T readValue(JsonParser p, TypeReference<?> valueTypeRef) throws IOExc
10151009
*/
10161010
@Override
10171011
@SuppressWarnings("unchecked")
1018-
public <T> T readValue(JsonParser p, ResolvedType valueType) throws IOException, JsonProcessingException {
1012+
public <T> T readValue(JsonParser p, ResolvedType valueType) throws IOException {
10191013
return (T) forType((JavaType)valueType).readValue(p);
10201014
}
10211015

@@ -1181,8 +1175,7 @@ public void writeTree(JsonGenerator g, TreeNode rootNode) {
11811175
* was specified with {@link #withValueToUpdate(Object)}.
11821176
*/
11831177
@SuppressWarnings("unchecked")
1184-
public <T> T readValue(InputStream src)
1185-
throws IOException, JsonProcessingException
1178+
public <T> T readValue(InputStream src) throws IOException
11861179
{
11871180
if (_dataFormatReaders != null) {
11881181
return (T) _detectBindAndClose(_dataFormatReaders.findFormat(src), false);
@@ -1198,8 +1191,7 @@ public <T> T readValue(InputStream src)
11981191
* was specified with {@link #withValueToUpdate(Object)}.
11991192
*/
12001193
@SuppressWarnings("unchecked")
1201-
public <T> T readValue(Reader src)
1202-
throws IOException, JsonProcessingException
1194+
public <T> T readValue(Reader src) throws IOException
12031195
{
12041196
if (_dataFormatReaders != null) {
12051197
_reportUndetectableSource(src);
@@ -1215,8 +1207,7 @@ public <T> T readValue(Reader src)
12151207
* was specified with {@link #withValueToUpdate(Object)}.
12161208
*/
12171209
@SuppressWarnings("unchecked")
1218-
public <T> T readValue(String src)
1219-
throws IOException, JsonProcessingException
1210+
public <T> T readValue(String src) throws IOException
12201211
{
12211212
if (_dataFormatReaders != null) {
12221213
_reportUndetectableSource(src);
@@ -1232,8 +1223,7 @@ public <T> T readValue(String src)
12321223
* was specified with {@link #withValueToUpdate(Object)}.
12331224
*/
12341225
@SuppressWarnings("unchecked")
1235-
public <T> T readValue(byte[] src)
1236-
throws IOException, JsonProcessingException
1226+
public <T> T readValue(byte[] src) throws IOException
12371227
{
12381228
if (_dataFormatReaders != null) {
12391229
return (T) _detectBindAndClose(src, 0, src.length);
@@ -1250,7 +1240,7 @@ public <T> T readValue(byte[] src)
12501240
*/
12511241
@SuppressWarnings("unchecked")
12521242
public <T> T readValue(byte[] src, int offset, int length)
1253-
throws IOException, JsonProcessingException
1243+
throws IOException
12541244
{
12551245
if (_dataFormatReaders != null) {
12561246
return (T) _detectBindAndClose(src, offset, length);
@@ -1262,7 +1252,7 @@ public <T> T readValue(byte[] src, int offset, int length)
12621252

12631253
@SuppressWarnings("unchecked")
12641254
public <T> T readValue(File src)
1265-
throws IOException, JsonProcessingException
1255+
throws IOException
12661256
{
12671257
if (_dataFormatReaders != null) {
12681258
return (T) _detectBindAndClose(_dataFormatReaders.findFormat(_inputStream(src)), true);
@@ -1279,7 +1269,7 @@ public <T> T readValue(File src)
12791269
*/
12801270
@SuppressWarnings("unchecked")
12811271
public <T> T readValue(URL src)
1282-
throws IOException, JsonProcessingException
1272+
throws IOException
12831273
{
12841274
if (_dataFormatReaders != null) {
12851275
return (T) _detectBindAndClose(_dataFormatReaders.findFormat(_inputStream(src)), true);
@@ -1297,7 +1287,7 @@ public <T> T readValue(URL src)
12971287
*/
12981288
@SuppressWarnings("unchecked")
12991289
public <T> T readValue(JsonNode src)
1300-
throws IOException, JsonProcessingException
1290+
throws IOException
13011291
{
13021292
if (_dataFormatReaders != null) {
13031293
_reportUndetectableSource(src);
@@ -1395,7 +1385,7 @@ public JsonNode readTree(DataInput src) throws IOException
13951385
* to the token following it.
13961386
*/
13971387
public <T> MappingIterator<T> readValues(JsonParser p)
1398-
throws IOException, JsonProcessingException
1388+
throws IOException
13991389
{
14001390
DeserializationContext ctxt = createDeserializationContext(p);
14011391
// false -> do not close as caller gave parser instance
@@ -1423,7 +1413,7 @@ public <T> MappingIterator<T> readValues(JsonParser p)
14231413
* <code>START_ARRAY</code> which is part of the first element).
14241414
*/
14251415
public <T> MappingIterator<T> readValues(InputStream src)
1426-
throws IOException, JsonProcessingException
1416+
throws IOException
14271417
{
14281418
if (_dataFormatReaders != null) {
14291419
return _detectBindAndReadValues(_dataFormatReaders.findFormat(src), false);
@@ -1437,7 +1427,7 @@ public <T> MappingIterator<T> readValues(InputStream src)
14371427
*/
14381428
@SuppressWarnings("resource")
14391429
public <T> MappingIterator<T> readValues(Reader src)
1440-
throws IOException, JsonProcessingException
1430+
throws IOException
14411431
{
14421432
if (_dataFormatReaders != null) {
14431433
_reportUndetectableSource(src);
@@ -1456,7 +1446,7 @@ public <T> MappingIterator<T> readValues(Reader src)
14561446
*/
14571447
@SuppressWarnings("resource")
14581448
public <T> MappingIterator<T> readValues(String json)
1459-
throws IOException, JsonProcessingException
1449+
throws IOException
14601450
{
14611451
if (_dataFormatReaders != null) {
14621452
_reportUndetectableSource(json);
@@ -1472,7 +1462,7 @@ public <T> MappingIterator<T> readValues(String json)
14721462
* Overloaded version of {@link #readValue(InputStream)}.
14731463
*/
14741464
public <T> MappingIterator<T> readValues(byte[] src, int offset, int length)
1475-
throws IOException, JsonProcessingException
1465+
throws IOException
14761466
{
14771467
if (_dataFormatReaders != null) {
14781468
return _detectBindAndReadValues(_dataFormatReaders.findFormat(src, offset, length), false);
@@ -1485,15 +1475,15 @@ public <T> MappingIterator<T> readValues(byte[] src, int offset, int length)
14851475
* Overloaded version of {@link #readValue(InputStream)}.
14861476
*/
14871477
public final <T> MappingIterator<T> readValues(byte[] src)
1488-
throws IOException, JsonProcessingException {
1478+
throws IOException {
14891479
return readValues(src, 0, src.length);
14901480
}
14911481

14921482
/**
14931483
* Overloaded version of {@link #readValue(InputStream)}.
14941484
*/
14951485
public <T> MappingIterator<T> readValues(File src)
1496-
throws IOException, JsonProcessingException
1486+
throws IOException
14971487
{
14981488
if (_dataFormatReaders != null) {
14991489
return _detectBindAndReadValues(
@@ -1508,7 +1498,7 @@ public <T> MappingIterator<T> readValues(File src)
15081498
* @param src URL to read to access JSON content to parse.
15091499
*/
15101500
public <T> MappingIterator<T> readValues(URL src)
1511-
throws IOException, JsonProcessingException
1501+
throws IOException
15121502
{
15131503
if (_dataFormatReaders != null) {
15141504
return _detectBindAndReadValues(
@@ -1542,12 +1532,12 @@ public <T> T treeToValue(TreeNode n, Class<T> valueType) throws JsonProcessingEx
15421532
} catch (JsonProcessingException e) {
15431533
throw e;
15441534
} catch (IOException e) { // should not occur, no real i/o...
1545-
throw new IllegalArgumentException(e.getMessage(), e);
1535+
throw JsonMappingException.fromUnexpectedIOE(e);
15461536
}
15471537
}
15481538

15491539
@Override
1550-
public void writeValue(JsonGenerator gen, Object value) throws IOException, JsonProcessingException {
1540+
public void writeValue(JsonGenerator gen, Object value) throws IOException {
15511541
throw new UnsupportedOperationException("Not implemented for ObjectReader");
15521542
}
15531543

@@ -1761,7 +1751,7 @@ protected Object _detectBindAndClose(DataFormatReaders.Match match, boolean forc
17611751

17621752
@SuppressWarnings("resource")
17631753
protected <T> MappingIterator<T> _detectBindAndReadValues(DataFormatReaders.Match match, boolean forceClosing)
1764-
throws IOException, JsonProcessingException
1754+
throws IOException
17651755
{
17661756
if (!match.hasMatch()) {
17671757
_reportUnkownFormat(_dataFormatReaders, match);
@@ -1792,7 +1782,8 @@ protected JsonNode _detectBindAndCloseAsTree(InputStream in) throws IOException
17921782
* Method called to indicate that format detection failed to detect format
17931783
* of given input
17941784
*/
1795-
protected void _reportUnkownFormat(DataFormatReaders detector, DataFormatReaders.Match match) throws JsonProcessingException
1785+
protected void _reportUnkownFormat(DataFormatReaders detector, DataFormatReaders.Match match)
1786+
throws JsonProcessingException
17961787
{
17971788
// 17-Aug-2015, tatu: Unfortunately, no parser/generator available so:
17981789
throw new JsonParseException(null, "Can not detect format from input, does not look like any of detectable formats "
@@ -1827,13 +1818,6 @@ protected DefaultDeserializationContext createDeserializationContext(JsonParser
18271818
return _context.createInstance(_config, p, _injectableValues);
18281819
}
18291820

1830-
protected void _reportUndetectableSource(Object src) throws JsonProcessingException
1831-
{
1832-
// 17-Aug-2015, tatu: Unfortunately, no parser/generator available so:
1833-
throw new JsonParseException(null, "Can not use source of type "
1834-
+src.getClass().getName()+" with format auto-detection: must be byte- not char-based");
1835-
}
1836-
18371821
protected InputStream _inputStream(URL src) throws IOException {
18381822
return src.openStream();
18391823
}
@@ -1842,6 +1826,13 @@ protected InputStream _inputStream(File f) throws IOException {
18421826
return new FileInputStream(f);
18431827
}
18441828

1829+
protected void _reportUndetectableSource(Object src) throws JsonProcessingException
1830+
{
1831+
// 17-Aug-2015, tatu: Unfortunately, no parser/generator available so:
1832+
throw new JsonParseException(null, "Can not use source of type "
1833+
+src.getClass().getName()+" with format auto-detection: must be byte- not char-based");
1834+
}
1835+
18451836
/*
18461837
/**********************************************************
18471838
/* Helper methods, locating deserializers etc

src/main/java/com/fasterxml/jackson/databind/PropertyMetadata.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public PropertyMetadata withDefaultValue(String def) {
180180
return this;
181181
}
182182
def = null;
183-
} else if (_defaultValue.equals(def)) {
183+
} else if (def.equals(_defaultValue)) {
184184
return this;
185185
}
186186
return new PropertyMetadata(_required, _description, _index, def,
@@ -197,10 +197,8 @@ public PropertyMetadata withRequired(Boolean b) {
197197
if (_required == null) {
198198
return this;
199199
}
200-
} else {
201-
if (_required != null && _required.booleanValue() == b.booleanValue()) {
202-
return this;
203-
}
200+
} else if (b.equals(_required)) {
201+
return this;
204202
}
205203
return new PropertyMetadata(b, _description, _index, _defaultValue,
206204
_mergeInfo, _valueNulls, _contentNulls);

src/main/java/com/fasterxml/jackson/databind/PropertyName.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.core.io.SerializedString;
55
import com.fasterxml.jackson.core.util.InternCache;
66
import com.fasterxml.jackson.databind.cfg.MapperConfig;
7+
import com.fasterxml.jackson.databind.util.ClassUtil;
78

89
/**
910
* Simple value class used for containing names of properties as defined
@@ -62,17 +63,23 @@ public PropertyName(String simpleName) {
6263

6364
public PropertyName(String simpleName, String namespace)
6465
{
65-
_simpleName = (simpleName == null) ? "" : simpleName;
66+
_simpleName = ClassUtil.nonNullString(simpleName);
6667
_namespace = namespace;
6768
}
6869

6970
// To support JDK serialization, recovery of Singleton instance
7071
protected Object readResolve() {
71-
if (_simpleName == null || _USE_DEFAULT.equals(_simpleName)) {
72-
return USE_DEFAULT;
73-
}
74-
if (_simpleName.equals(_NO_NAME) && _namespace == null) {
75-
return NO_NAME;
72+
if (_namespace == null) {
73+
if (_simpleName == null || _USE_DEFAULT.equals(_simpleName)) {
74+
return USE_DEFAULT;
75+
}
76+
// 30-Oct-2016, tatu: I don't see how this could ever occur...
77+
// or how to distinguish USE_DEFAULT/NO_NAME from serialized
78+
/*
79+
if (_simpleName.equals(_NO_NAME)) {
80+
return NO_NAME;
81+
}
82+
*/
7683
}
7784
return this;
7885
}
@@ -182,10 +189,8 @@ public boolean hasSimpleName() {
182189
* @since 2.3
183190
*/
184191
public boolean hasSimpleName(String str) {
185-
if (str == null) {
186-
return _simpleName == null;
187-
}
188-
return str.equals(_simpleName);
192+
// _simpleName never null so...
193+
return _simpleName.equals(str);
189194
}
190195

191196
public boolean hasNamespace() {

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/AsWrapperTypeSerializer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import com.fasterxml.jackson.databind.BeanProperty;
99
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver;
10+
import com.fasterxml.jackson.databind.util.ClassUtil;
1011

1112
/**
1213
* Type wrapper that tries to use an extra JSON Object, with a single
@@ -222,7 +223,7 @@ public void writeCustomTypeSuffixForScalar(Object value, JsonGenerator g, String
222223
* @since 2.6
223224
*/
224225
protected String _validTypeId(String typeId) {
225-
return (typeId == null) ? "" : typeId;
226+
return ClassUtil.nonNullString(typeId);
226227
}
227228

228229
// @since 2.9

src/main/java/com/fasterxml/jackson/databind/jsontype/impl/TypeDeserializerBase.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ protected TypeDeserializerBase(JavaType baseType, TypeIdResolver idRes,
7474
{
7575
_baseType = baseType;
7676
_idResolver = idRes;
77-
// 22-Dec-2015, tatu: as per [databind#1055], avoid NPE
78-
_typePropertyName = (typePropertyName == null) ? "" : typePropertyName;
77+
_typePropertyName = ClassUtil.nonNullString(typePropertyName);
7978
_typeIdVisible = typeIdVisible;
8079
// defaults are fine, although shouldn't need much concurrency
8180
_deserializers = new ConcurrentHashMap<String, JsonDeserializer<Object>>(16, 0.75f, 2);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,16 @@ public static String nullOrToString(Object value) {
642642
return value.toString();
643643
}
644644

645+
/**
646+
* @since 2.9
647+
*/
648+
public static String nonNullString(String str) {
649+
if (str == null) {
650+
return "";
651+
}
652+
return str;
653+
}
654+
645655
/**
646656
* @since 2.9
647657
*/

src/test/java/com/fasterxml/jackson/databind/ObjectReaderTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ public void testFeatureSettings() throws Exception
140140
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE);
141141
assertTrue(r.isEnabled(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES));
142142
assertTrue(r.isEnabled(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE));
143+
144+
// alternative method too... can't recall why two
145+
assertSame(r, r.with(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES,
146+
DeserializationFeature.FAIL_ON_INVALID_SUBTYPE));
143147
}
144148

145149
public void testMiscSettings() throws Exception

src/test/java/com/fasterxml/jackson/databind/ObjectWriterTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ public void testMiscSettings() throws Exception
201201
newW = w.withRootName("foo");
202202
assertNotSame(w, newW);
203203
assertSame(newW, newW.withRootName(PropertyName.construct("foo")));
204+
205+
FormatSchema schema = new BogusSchema();
206+
try {
207+
newW = w.with(schema);
208+
fail("Should not pass");
209+
} catch (IllegalArgumentException e) {
210+
verifyException(e, "Can not use FormatSchema");
211+
}
204212
}
205213

206214
public void testFeatureSettings() throws Exception

0 commit comments

Comments
 (0)