Skip to content

Commit 3455cd6

Browse files
committed
Fix #762
1 parent e1e0c0b commit 3455cd6

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

release-notes/VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Project: jackson-databind
2222
#737: Add support for writing raw values in TokenBuffer
2323
(suggested by Guillaume S, gsmet@github)
2424
#743: Add `RawValue` helper type, for piping raw values through `TokenBuffer`
25+
#762: Add `ObjectWriter.withoutRootName()`, `ObjectReader.withoutRootName()`
2526
#765: `SimpleType.withStaticTyping()` impl incorrect
2627
- Remove old cglib compatibility tests; cause problems in Eclipse
2728

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

+14
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,20 @@ public ObjectReader withRootName(String rootName) {
537537
return _with(_config.withRootName(rootName));
538538
}
539539

540+
/**
541+
* Convenience method that is same as calling:
542+
*<code>
543+
* withRootName("")
544+
*</code>
545+
* which will forcibly prevent use of root name wrapping when writing
546+
* values with this {@link ObjectReader}.
547+
*
548+
* @since 2.6
549+
*/
550+
public ObjectReader withoutRootName() {
551+
return _with(_config.withRootName(""));
552+
}
553+
540554
/**
541555
* Method for constructing a new instance with configuration that
542556
* passes specified {@link FormatSchema} to {@link JsonParser} that

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

+18
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,30 @@ public ObjectWriter with(PrettyPrinter pp) {
402402
*<p>
403403
* Note that method does NOT change state of this reader, but
404404
* rather construct and returns a newly configured instance.
405+
*
406+
* @param rootName Root name to use, if non-empty; `null` for "use defaults",
407+
* and empty String ("") for "do NOT add root wrapper"
405408
*/
406409
public ObjectWriter withRootName(String rootName) {
407410
SerializationConfig newConfig = _config.withRootName(rootName);
408411
return (newConfig == _config) ? this : _new(this, newConfig);
409412
}
410413

414+
/**
415+
* Convenience method that is same as calling:
416+
*<code>
417+
* withRootName("")
418+
*</code>
419+
* which will forcibly prevent use of root name wrapping when writing
420+
* values with this {@link ObjectWriter}.
421+
*
422+
* @since 2.6
423+
*/
424+
public ObjectWriter withoutRootName() {
425+
SerializationConfig newConfig = _config.withRootName("");
426+
return (newConfig == _config) ? this : _new(this, newConfig);
427+
}
428+
411429
/**
412430
* Method that will construct a new instance that uses specific format schema
413431
* for serialization.

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

+14
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,22 @@ public void testRootUsingExplicitConfig() throws Exception
9898
json = wrapping.writer().withRootName("").writeValueAsString(new Bean());
9999
assertEquals("{\"a\":3}", json);
100100

101+
// 21-Apr-2015, tatu: Alternative available with 2.6 as well:
102+
json = wrapping.writer().withoutRootName().writeValueAsString(new Bean());
103+
assertEquals("{\"a\":3}", json);
104+
101105
bean = wrapping.reader(Bean.class).withRootName("").readValue(json);
102106
assertNotNull(bean);
107+
assertEquals(3, bean.a);
108+
109+
bean = wrapping.reader(Bean.class).withoutRootName().readValue("{\"a\":4}");
110+
assertNotNull(bean);
111+
assertEquals(4, bean.a);
112+
113+
// and back to defaults
114+
bean = wrapping.reader(Bean.class).readValue("{\"rudy\":{\"a\":7}}");
115+
assertNotNull(bean);
116+
assertEquals(7, bean.a);
103117
}
104118

105119
/*

0 commit comments

Comments
 (0)