Skip to content

Commit 04c9cf5

Browse files
committed
Bit more work for #743: also add support in ContainerNode, JsonNodeFactory (for ObjectNode, ArrayNode)
1 parent 4670d4b commit 04c9cf5

File tree

8 files changed

+74
-22
lines changed

8 files changed

+74
-22
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.databind.JsonNode;
55
import com.fasterxml.jackson.databind.SerializerProvider;
66
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
7+
import com.fasterxml.jackson.databind.util.RawValue;
78

89
import java.io.IOException;
910
import java.math.BigDecimal;
@@ -338,6 +339,20 @@ public ArrayNode addPOJO(Object value)
338339
return this;
339340
}
340341

342+
/**
343+
* @return This array node, to allow chaining
344+
*
345+
* @since 2.6
346+
*/
347+
public ArrayNode addRawValue(RawValue raw) {
348+
if (raw == null) {
349+
addNull();
350+
} else {
351+
_add(rawValueNode(raw));
352+
}
353+
return this;
354+
}
355+
341356
/**
342357
* Method that will add a null value at the end of this array node.
343358
*

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

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.math.BigInteger;
55

66
import com.fasterxml.jackson.core.*;
7-
87
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.fasterxml.jackson.databind.util.RawValue;
99

1010
/**
1111
* This intermediate base class is used for all container nodes,
@@ -22,8 +22,7 @@ public abstract class ContainerNode<T extends ContainerNode<T>>
2222
*/
2323
protected final JsonNodeFactory _nodeFactory;
2424

25-
protected ContainerNode(JsonNodeFactory nc)
26-
{
25+
protected ContainerNode(JsonNodeFactory nc) {
2726
_nodeFactory = nc;
2827
}
2928

@@ -126,12 +125,9 @@ public final NumericNode numberNode(long v) {
126125
@Override
127126
public final ValueNode pojoNode(Object pojo) { return _nodeFactory.pojoNode(pojo); }
128127

129-
/**
130-
* @deprecated Since 2.3 Use {@link #pojoNode} instead.
131-
*/
132-
@Deprecated
133-
public final POJONode POJONode(Object pojo) { return (POJONode) _nodeFactory.pojoNode(pojo); }
134-
128+
@Override
129+
public final ValueNode rawValueNode(RawValue value) { return _nodeFactory.rawValueNode(value); }
130+
135131
/*
136132
/**********************************************************
137133
/* Common mutators

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.math.BigDecimal;
44
import java.math.BigInteger;
55

6+
import com.fasterxml.jackson.databind.util.RawValue;
7+
68
/**
79
* Interface that defines common "creator" functionality implemented
810
* both by {@link JsonNodeFactory} and {@link ContainerNode} (that is,
@@ -34,13 +36,29 @@ public interface JsonNodeCreator
3436
public ValueNode numberNode(Double value);
3537
public ValueNode numberNode(BigDecimal v);
3638

37-
// Textual nodes, other value (non-structured) nodes
39+
// Textual nodes
3840

3941
public ValueNode textNode(String text);
42+
43+
// Other value (non-structured) nodes
44+
4045
public ValueNode binaryNode(byte[] data);
4146
public ValueNode binaryNode(byte[] data, int offset, int length);
4247
public ValueNode pojoNode(Object pojo);
4348

49+
/**
50+
* Factory method to use for adding "raw values"; pre-encoded values
51+
* that are included exactly as-is when node is serialized.
52+
* This may be used, for example, to include fully serialized JSON
53+
* sub-trees.
54+
* Note that the concept may not work with all backends, and since
55+
* no translation of any kinds is done it will not work when converting
56+
* between data formats.
57+
*
58+
* @since 2.6
59+
*/
60+
public ValueNode rawValueNode(RawValue value);
61+
4462
// Structured nodes:
4563
// (bit unkosher, due to forward references... but has to do for now)
4664

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

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.math.BigDecimal;
44
import java.math.BigInteger;
55

6+
import com.fasterxml.jackson.databind.util.RawValue;
7+
68
/**
79
* Base class that specifies methods for getting access to
810
* Node instances (newly constructed, or shared, depending
@@ -15,7 +17,7 @@ public class JsonNodeFactory
1517
,JsonNodeCreator // since 2.3
1618
{
1719
// with 2.2
18-
private static final long serialVersionUID = -3271940633258788634L;
20+
private static final long serialVersionUID = 1L;
1921

2022
private final boolean _cfgBigDecimalExact;
2123

@@ -327,11 +329,10 @@ public BinaryNode binaryNode(byte[] data, int offset, int length) {
327329
@Override
328330
public ValueNode pojoNode(Object pojo) { return new POJONode(pojo); }
329331

330-
/**
331-
* @deprecated Since 2.3 Use {@link #pojoNode} instead.
332-
*/
333-
@Deprecated
334-
public POJONode POJONode(Object pojo) { return new POJONode(pojo); }
332+
@Override
333+
public ValueNode rawValueNode(RawValue value) {
334+
return new POJONode(value);
335+
}
335336

336337
/*
337338
/**********************************************************

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

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.fasterxml.jackson.databind.JsonNode;
55
import com.fasterxml.jackson.databind.SerializerProvider;
66
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
7+
import com.fasterxml.jackson.databind.util.RawValue;
78

89
import java.io.IOException;
910
import java.math.BigDecimal;
@@ -568,6 +569,13 @@ public ObjectNode putPOJO(String fieldName, Object pojo) {
568569
return _put(fieldName, pojoNode(pojo));
569570
}
570571

572+
/**
573+
* @since 2.6
574+
*/
575+
public ObjectNode putRawValue(String fieldName, RawValue raw) {
576+
return _put(fieldName, rawValueNode(raw));
577+
}
578+
571579
/**
572580
* @return This node (to allow chaining)
573581
*/

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import java.io.IOException;
44

55
import com.fasterxml.jackson.core.*;
6+
import com.fasterxml.jackson.databind.JsonSerializable;
67
import com.fasterxml.jackson.databind.SerializerProvider;
78

8-
99
/**
1010
* Value node that contains a wrapped POJO, to be serialized as
1111
* a JSON constructed through data mapping (usually done by
@@ -102,13 +102,14 @@ public double asDouble(double defaultValue)
102102
*/
103103

104104
@Override
105-
public final void serialize(JsonGenerator jg, SerializerProvider provider)
106-
throws IOException, JsonProcessingException
105+
public final void serialize(JsonGenerator gen, SerializerProvider serializers) throws IOException
107106
{
108107
if (_value == null) {
109-
provider.defaultSerializeNull(jg);
108+
serializers.defaultSerializeNull(gen);
109+
} else if (_value instanceof JsonSerializable) {
110+
((JsonSerializable) _value).serialize(gen, serializers);
110111
} else {
111-
jg.writeObject(_value);
112+
gen.writeObject(_value);
112113
}
113114
}
114115

src/test/java/com/fasterxml/jackson/databind/node/TestJsonNode.java

+13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package com.fasterxml.jackson.databind.node;
22

33
import com.fasterxml.jackson.core.*;
4+
import com.fasterxml.jackson.core.io.SerializedString;
45
import com.fasterxml.jackson.databind.*;
6+
import com.fasterxml.jackson.databind.util.RawValue;
57

68
/**
79
* Basic tests for {@link JsonNode} base class and some features
810
* of implementation classes
911
*/
1012
public class TestJsonNode extends NodeTestBase
1113
{
14+
private final ObjectMapper MAPPER = objectMapper();
15+
1216
public void testText()
1317
{
1418
assertNull(TextNode.valueOf(null));
@@ -107,4 +111,13 @@ public void testPOJO()
107111
// but if wrapping actual number, use it
108112
assertNodeNumbers(new POJONode(Integer.valueOf(123)), 123, 123.0);
109113
}
114+
115+
// [databind#743]
116+
public void testRawValue() throws Exception
117+
{
118+
ObjectNode root = MAPPER.createObjectNode();
119+
root.putRawValue("a", new RawValue(new SerializedString("[1, 2, 3]")));
120+
121+
assertEquals("{\"a\":[1, 2, 3]}", MAPPER.writeValueAsString(root));
122+
}
110123
}

src/test/java/com/fasterxml/jackson/databind/node/TestObjectNode.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public DataImpl(JsonNode n) {
4343
/* Test methods
4444
/**********************************************************
4545
*/
46-
46+
4747
private final ObjectMapper MAPPER = objectMapper();
4848

4949
public void testSimpleObject() throws Exception

0 commit comments

Comments
 (0)