Skip to content

Commit 7785dff

Browse files
committed
Fix #348
1 parent de1acae commit 7785dff

File tree

5 files changed

+43
-16
lines changed

5 files changed

+43
-16
lines changed

release-notes/CREDITS

+5
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,8 @@ John Meyer (jpmeyer@github)
229229
Charles Allen (drcrallen@github):
230230
* Reported #696: Copy constructor does not preserve `_injectableValues`
231231
(2.6.0)
232+
233+
Chris Pimlott (pimlottc@github):
234+
* Suggested #348: ObjectMapper.valueToTree does not work with @JsonRawValue
235+
(2.6.0)
236+

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ Project: jackson-databind
77
2.6.0 (not yet released)
88

99
#312: Support Type Id mappings where two ids map to same Class
10+
#348: ObjectMapper.valueToTree does not work with @JsonRawValue
11+
(reported by Chris P, pimlottc@github)
1012
#649: Make `BeanDeserializer` use new `parser.nextFieldName()` and `.hasTokenId()` methods
1113
#696: Copy constructor does not preserve `_injectableValues`
1214
(reported by Charles A)

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -2351,12 +2351,14 @@ public <T> T treeToValue(TreeNode n, Class<T> valueType)
23512351
* to serializing value into JSON and parsing JSON as tree, but
23522352
* more efficient.
23532353
*<p>
2354-
* NOTE: one known difference from actual serialization is that so-called
2355-
* "raw values" are not supported -- since they are opaque sequence of
2356-
* bytes to include (which may or may not be supported by the backend)
2357-
* they can not be converted using this method. It may be possible to
2358-
* support conversions using full serialization, if raw values must be
2359-
* preserved.
2354+
* NOTE: while results are usually identical to that of serialization followed
2355+
* by deserialization, this is not always the case. In some cases serialization
2356+
* into intermediate representation will retain encapsulation of things like
2357+
* raw value ({@link com.fasterxml.jackson.databind.util.RawValue}) or basic
2358+
* node identity ({@link JsonNode}). If so, result is a valid tree, but values
2359+
* are not re-constructed through actual JSON representation. So if transformation
2360+
* requires actual materialization of JSON (or other data format that this mapper
2361+
* produces), it will be necessary to do actual serialization.
23602362
*
23612363
* @param <T> Actual node type; usually either basic {@link JsonNode} or
23622364
* {@link com.fasterxml.jackson.databind.node.ObjectNode}

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,9 @@ public PropertyName findNameForSerialization(Annotated a)
631631
JsonProperty pann = _findAnnotation(a, JsonProperty.class);
632632
if (pann != null) {
633633
name = pann.value();
634-
} else if (_hasAnnotation(a, JsonSerialize.class) || _hasAnnotation(a, JsonView.class)) {
634+
} else if (_hasAnnotation(a, JsonSerialize.class)
635+
|| _hasAnnotation(a, JsonView.class)
636+
|| _hasAnnotation(a, JsonRawValue.class)) {
635637
name = "";
636638
} else {
637639
return null;

src/test/java/com/fasterxml/jackson/databind/ser/TestJsonRawValue.java renamed to src/test/java/com/fasterxml/jackson/databind/ser/RawValueTest.java

+25-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.fasterxml.jackson.databind.ser;
22

33
import com.fasterxml.jackson.annotation.*;
4-
4+
import com.fasterxml.jackson.databind.JsonNode;
55
import com.fasterxml.jackson.databind.ObjectMapper;
66

77
/**
88
* This unit test suite tests functioning of {@link JsonRawValue}
99
* annotation with bean serialization.
1010
*/
11-
public class TestJsonRawValue
11+
public class RawValueTest
1212
extends com.fasterxml.jackson.databind.BaseMapTest
1313
{
1414
/*
@@ -31,37 +31,53 @@ final static class ClassGetter<T>
3131

3232
@JsonProperty @JsonRawValue protected T value() { return _value; }
3333
}
34-
34+
35+
// [databind#348]
36+
static class RawWrapped
37+
{
38+
@JsonRawValue
39+
private final String json;
40+
41+
public RawWrapped(String str) {
42+
json = str;
43+
}
44+
}
45+
3546
/*
3647
/*********************************************************
3748
/* Test cases
3849
/*********************************************************
3950
*/
4051

52+
private final ObjectMapper MAPPER = objectMapper();
53+
4154
public void testSimpleStringGetter() throws Exception
4255
{
43-
ObjectMapper m = new ObjectMapper();
4456
String value = "abc";
45-
String result = m.writeValueAsString(new ClassGetter<String>(value));
57+
String result = MAPPER.writeValueAsString(new ClassGetter<String>(value));
4658
String expected = String.format("{\"nonRaw\":\"%s\",\"raw\":%s,\"value\":%s}", value, value, value);
4759
assertEquals(expected, result);
4860
}
4961

5062
public void testSimpleNonStringGetter() throws Exception
5163
{
52-
ObjectMapper m = new ObjectMapper();
5364
int value = 123;
54-
String result = m.writeValueAsString(new ClassGetter<Integer>(value));
65+
String result = MAPPER.writeValueAsString(new ClassGetter<Integer>(value));
5566
String expected = String.format("{\"nonRaw\":%d,\"raw\":%d,\"value\":%d}", value, value, value);
5667
assertEquals(expected, result);
5768
}
5869

5970
public void testNullStringGetter() throws Exception
6071
{
61-
ObjectMapper m = new ObjectMapper();
62-
String result = m.writeValueAsString(new ClassGetter<String>(null));
72+
String result = MAPPER.writeValueAsString(new ClassGetter<String>(null));
6373
String expected = "{\"nonRaw\":null,\"raw\":null,\"value\":null}";
6474
assertEquals(expected, result);
6575
}
6676

77+
public void testWithValueToTree() throws Exception
78+
{
79+
JsonNode w = MAPPER.valueToTree(new RawWrapped("{ }"));
80+
assertNotNull(w);
81+
assertEquals("{\"json\":{ }}", MAPPER.writeValueAsString(w));
82+
}
6783
}

0 commit comments

Comments
 (0)