File tree 2 files changed +28
-1
lines changed
main/java/com/fasterxml/jackson/databind
test/java/com/fasterxml/jackson/databind/node 2 files changed +28
-1
lines changed Original file line number Diff line number Diff line change @@ -3521,7 +3521,8 @@ public <T extends JsonNode> T valueToTree(Object fromValue)
3521
3521
3522
3522
// inlined 'writeValue' with minor changes:
3523
3523
// first: disable wrapping when writing
3524
- final SerializationConfig config = getSerializationConfig ().without (SerializationFeature .WRAP_ROOT_VALUE );
3524
+ // [databind#4047] ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE
3525
+ final SerializationConfig config = getSerializationConfig ();
3525
3526
final DefaultSerializerProvider context = _serializerProvider (config );
3526
3527
3527
3528
// Then create TokenBuffer to use as JsonGenerator
Original file line number Diff line number Diff line change 1
1
package com .fasterxml .jackson .databind .node ;
2
2
3
+ import com .fasterxml .jackson .annotation .JsonRootName ;
3
4
import java .io .IOException ;
4
5
import java .math .BigDecimal ;
5
6
import java .util .*;
@@ -108,6 +109,13 @@ public void serializeWithType(JsonGenerator g,
108
109
}
109
110
}
110
111
112
+ // [databind#4047]
113
+ @ JsonRootName ("event" )
114
+ static class Event {
115
+ public Long id ;
116
+ public String name ;
117
+ }
118
+
111
119
/*
112
120
/**********************************************************
113
121
/* Unit tests
@@ -363,4 +371,22 @@ public void testNodeConvert() throws Exception
363
371
result = MAPPER .treeToValue (node , MAPPER .constructType (ObjectNode .class ));
364
372
assertSame (src , result );
365
373
}
374
+
375
+ // [databind#4047] : ObjectMapper.valueToTree will ignore the configuration SerializationFeature.WRAP_ROOT_VALUE
376
+ public void testValueToTree () throws Exception
377
+ {
378
+ // Arrange
379
+ Event value = new Event ();
380
+ value .id = 1L ;
381
+ value .name = "foo" ;
382
+
383
+ ObjectMapper wrapRootMapper = jsonMapperBuilder ()
384
+ .enable (SerializationFeature .WRAP_ROOT_VALUE )
385
+ .build ();
386
+
387
+ // Act & Assert
388
+ String expected = "{\" event\" :{\" id\" :1,\" name\" :\" foo\" }}" ;
389
+ assertEquals (expected , wrapRootMapper .writeValueAsString (value ));
390
+ assertEquals (expected , wrapRootMapper .valueToTree (value ).toString ());
391
+ }
366
392
}
You can’t perform that action at this time.
0 commit comments