@@ -112,7 +112,57 @@ public void testMapperFailOnTrailing() throws Exception
112112
113113 public void testMapperFailOnTrailingWithNull () throws Exception
114114 {
115- // !!! TODO
115+ final ObjectMapper strict = newObjectMapper ()
116+ .enable (DeserializationFeature .FAIL_ON_TRAILING_TOKENS );
117+
118+ // some still ok
119+ JsonNode n = strict .readTree (JSON_OK_NULL );
120+ assertNotNull (n );
121+ assertTrue (n .isNull ());
122+
123+ // but if real content exists, will fail
124+ try {
125+ strict .readTree (JSON_FAIL_NULL );
126+ fail ("Should not have passed" );
127+ } catch (MismatchedInputException e ) {
128+ verifyException (e , "Trailing token (of type VALUE_FALSE)" );
129+ verifyException (e , "value (bound as `com.fasterxml.jackson.databind.JsonNode`)" );
130+ }
131+
132+ try {
133+ strict .readValue (JSON_FAIL_NULL , List .class );
134+ fail ("Should not have passed" );
135+ } catch (MismatchedInputException e ) {
136+ verifyException (e , "Trailing token (of type VALUE_FALSE)" );
137+ verifyException (e , "value (bound as `java.util.List`)" );
138+ }
139+
140+ // others fail conditionally: will fail on comments unless enabled
141+
142+ try {
143+ strict .readValue (JSON_OK_NULL_WITH_COMMENT , Object .class );
144+ fail ("Should not have passed" );
145+ } catch (JsonParseException e ) {
146+ verifyException (e , "Unexpected character" );
147+ verifyException (e , "maybe a (non-standard) comment" );
148+ }
149+ try {
150+ strict .readTree (JSON_OK_NULL_WITH_COMMENT );
151+ fail ("Should not have passed" );
152+ } catch (JsonParseException e ) {
153+ verifyException (e , "Unexpected character" );
154+ verifyException (e , "maybe a (non-standard) comment" );
155+ }
156+
157+ ObjectReader strictWithComments = strict .reader ()
158+ .with (JsonReadFeature .ALLOW_JAVA_COMMENTS );
159+ n = strictWithComments .readTree (JSON_OK_NULL );
160+ assertNotNull (n );
161+ assertTrue (n .isNull ());
162+
163+ Object ob = strictWithComments .forType (List .class )
164+ .readValue (JSON_OK_NULL_WITH_COMMENT );
165+ assertNull (ob );
116166 }
117167
118168 public void testReaderAcceptTrailing () throws Exception
@@ -187,6 +237,54 @@ public void testReaderFailOnTrailing() throws Exception
187237 _verifyArray (strictRWithComments .readTree (JSON_OK_ARRAY_WITH_COMMENT ));
188238 }
189239
240+ public void testReaderFailOnTrailingWithNull () throws Exception
241+ {
242+ ObjectReader strictR = MAPPER .reader ().with (DeserializationFeature .FAIL_ON_TRAILING_TOKENS );
243+ ObjectReader strictRForList = strictR .forType (List .class );
244+ JsonNode n = strictR .readTree (JSON_OK_NULL );
245+ assertTrue (n .isNull ());
246+
247+ // Will fail hard if there is a trailing token
248+ try {
249+ strictRForList .readValue (JSON_FAIL_NULL );
250+ fail ("Should not have passed" );
251+ } catch (MismatchedInputException e ) {
252+ verifyException (e , "Trailing token (of type VALUE_FALSE)" );
253+ verifyException (e , "value (bound as `java.util.List`)" );
254+ }
255+
256+ try {
257+ strictR .readTree (JSON_FAIL_NULL );
258+ fail ("Should not have passed" );
259+ } catch (MismatchedInputException e ) {
260+ verifyException (e , "Trailing token (of type VALUE_FALSE)" );
261+ verifyException (e , "value (bound as `com.fasterxml.jackson.databind.JsonNode`)" );
262+ }
263+
264+ // others conditionally: will fail on comments unless enabled
265+
266+ try {
267+ strictRForList .readValue (JSON_OK_NULL_WITH_COMMENT );
268+ fail ("Should not have passed" );
269+ } catch (JsonParseException e ) {
270+ verifyException (e , "Unexpected character" );
271+ verifyException (e , "maybe a (non-standard) comment" );
272+ }
273+ try {
274+ strictR .readTree (JSON_OK_NULL_WITH_COMMENT );
275+ fail ("Should not have passed" );
276+ } catch (JsonParseException e ) {
277+ verifyException (e , "Unexpected character" );
278+ verifyException (e , "maybe a (non-standard) comment" );
279+ }
280+
281+ // but works if comments enabled etc
282+
283+ ObjectReader strictRWithComments = strictR .with (JsonReadFeature .ALLOW_JAVA_COMMENTS );
284+ Object ob = strictRWithComments .forType (List .class ).readValue (JSON_OK_NULL_WITH_COMMENT );
285+ assertNull (ob );
286+ }
287+
190288 private void _verifyArray (JsonNode n ) throws Exception
191289 {
192290 assertTrue (n .isArray ());
0 commit comments