Skip to content

Commit 56cb61e

Browse files
committed
Minor improvement wrt #743: ensure that RawValue does get correctly propagated, improve POJONode.toString() a bit
1 parent 775634d commit 56cb61e

File tree

5 files changed

+67
-10
lines changed

5 files changed

+67
-10
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.fasterxml.jackson.databind.*;
77
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
88
import com.fasterxml.jackson.databind.node.*;
9+
import com.fasterxml.jackson.databind.util.RawValue;
910

1011
/**
1112
* Deserializer that can build instances of {@link JsonNode} from any
@@ -225,6 +226,9 @@ protected final ObjectNode deserializeObject(JsonParser p, DeserializationContex
225226
case JsonTokenId.ID_START_ARRAY:
226227
value = deserializeArray(p, ctxt, nodeFactory);
227228
break;
229+
case JsonTokenId.ID_EMBEDDED_OBJECT:
230+
value = _fromEmbedded(p, ctxt, nodeFactory);
231+
break;
228232
case JsonTokenId.ID_STRING:
229233
value = nodeFactory.textNode(p.getText());
230234
break;
@@ -270,6 +274,8 @@ protected final ArrayNode deserializeArray(JsonParser p, DeserializationContext
270274
break;
271275
case JsonTokenId.ID_END_ARRAY:
272276
return node;
277+
case JsonTokenId.ID_EMBEDDED_OBJECT:
278+
node.add(_fromEmbedded(p, ctxt, nodeFactory));
273279
case JsonTokenId.ID_STRING:
274280
node.add(nodeFactory.textNode(p.getText()));
275281
break;
@@ -376,7 +382,11 @@ protected final JsonNode _fromEmbedded(JsonParser p, DeserializationContext ctxt
376382
if (type == byte[].class) { // most common special case
377383
return nodeFactory.binaryNode((byte[]) ob);
378384
}
379-
if (JsonNode.class.isAssignableFrom(type)) {
385+
// [databind#743]: Don't forget RawValue
386+
if (ob instanceof RawValue) {
387+
return nodeFactory.rawValueNode((RawValue) ob);
388+
}
389+
if (ob instanceof JsonNode) {
380390
// [Issue#433]: but could also be a JsonNode hiding in there!
381391
return (JsonNode) ob;
382392
}

src/main/java/com/fasterxml/jackson/databind/node/POJONode.java

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.core.*;
66
import com.fasterxml.jackson.databind.JsonSerializable;
77
import com.fasterxml.jackson.databind.SerializerProvider;
8+
import com.fasterxml.jackson.databind.util.RawValue;
89

910
/**
1011
* Value node that contains a wrapped POJO, to be serialized as
@@ -158,6 +159,13 @@ protected boolean _pojoEquals(POJONode other)
158159
@Override
159160
public String toString()
160161
{
162+
// [databind#743]: Let's try indicating content type, for debugging purposes
163+
if (_value instanceof byte[]) {
164+
return String.format("(binary value of %d bytes)", ((byte[]) _value).length);
165+
}
166+
if (_value instanceof RawValue) {
167+
return String.format("(raw value '%s')", ((RawValue) _value).toString());
168+
}
161169
return String.valueOf(_value);
162170
}
163171
}

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

+23
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,27 @@ protected void _serialize(JsonGenerator gen) throws IOException
9999
gen.writeRawValue(String.valueOf(_value));
100100
}
101101
}
102+
103+
@Override
104+
public boolean equals(Object o) {
105+
if (o == this) return true;
106+
if (!(o instanceof RawValue)) return false;
107+
RawValue other = (RawValue) o;
108+
109+
if (_value == other._value) {
110+
return true;
111+
}
112+
return (_value != null) && _value.equals(other._value);
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
return (_value == null) ? 0 : _value.hashCode();
118+
}
119+
120+
@Override
121+
public String toString() {
122+
return String.format("[RawValue of type %s]",
123+
(_value == null) ? "NULL" : _value.getClass().getName());
124+
}
102125
}

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ public TokenBuffer append(TokenBuffer other) throws IOException
260260
}
261261
_mayHaveNativeIds = _hasNativeTypeIds | _hasNativeObjectIds;
262262

263-
JsonParser jp = other.asParser();
264-
while (jp.nextToken() != null) {
265-
copyCurrentStructure(jp);
263+
JsonParser p = other.asParser();
264+
while (p.nextToken() != null) {
265+
copyCurrentStructure(p);
266266
}
267267
return this;
268268
}
@@ -405,10 +405,10 @@ public void serialize(JsonGenerator gen) throws IOException
405405
*
406406
* @since 2.3
407407
*/
408-
public TokenBuffer deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException
408+
public TokenBuffer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException
409409
{
410-
if (jp.getCurrentTokenId() != JsonToken.FIELD_NAME.id()) {
411-
copyCurrentStructure(jp);
410+
if (p.getCurrentTokenId() != JsonToken.FIELD_NAME.id()) {
411+
copyCurrentStructure(p);
412412
return this;
413413
}
414414
/* 28-Oct-2014, tatu: As per #592, need to support a special case of starting from
@@ -418,15 +418,15 @@ public TokenBuffer deserialize(JsonParser jp, DeserializationContext ctxt) throw
418418
JsonToken t;
419419
writeStartObject();
420420
do {
421-
copyCurrentStructure(jp);
422-
} while ((t = jp.nextToken()) == JsonToken.FIELD_NAME);
421+
copyCurrentStructure(p);
422+
} while ((t = p.nextToken()) == JsonToken.FIELD_NAME);
423423
if (t != JsonToken.END_OBJECT) {
424424
throw ctxt.mappingException("Expected END_OBJECT after copying contents of a JsonParser into TokenBuffer, got "+t);
425425
}
426426
writeEndObject();
427427
return this;
428428
}
429-
429+
430430
@Override
431431
@SuppressWarnings("resource")
432432
public String toString()

src/test/java/com/fasterxml/jackson/databind/ser/RawValueTest.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package com.fasterxml.jackson.databind.ser;
22

3+
import java.util.HashMap;
4+
import java.util.Map;
5+
36
import com.fasterxml.jackson.annotation.*;
47
import com.fasterxml.jackson.databind.JsonNode;
58
import com.fasterxml.jackson.databind.ObjectMapper;
9+
import com.fasterxml.jackson.databind.util.RawValue;
610

711
/**
812
* This unit test suite tests functioning of {@link JsonRawValue}
@@ -80,4 +84,16 @@ public void testWithValueToTree() throws Exception
8084
assertNotNull(w);
8185
assertEquals("{\"json\":{ }}", MAPPER.writeValueAsString(w));
8286
}
87+
88+
// for [databind#743]
89+
public void testRawFromMapToTree() throws Exception
90+
{
91+
RawValue myType = new RawValue("Jackson");
92+
93+
Map<String, Object> object = new HashMap<String, Object>();
94+
object.put("key", myType);
95+
JsonNode jsonNode = MAPPER.valueToTree(object);
96+
String json = MAPPER.writeValueAsString(jsonNode);
97+
assertEquals("{\"key\":Jackson}", json);
98+
}
8399
}

0 commit comments

Comments
 (0)