Skip to content

Commit 1af00fc

Browse files
committed
More tweaks for #18
1 parent 92ca9f4 commit 1af00fc

File tree

6 files changed

+55
-31
lines changed

6 files changed

+55
-31
lines changed

release-notes/VERSION-2.x

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Project: jackson-databind
66

77
2.10.0 (not yet released)
88

9-
#18: Make `ObjectNode` and `ArrayNode` serializable
9+
#18: Make `JsonNode` serializable
1010
#1675: Remove "impossible" `IOException` in `readTree()` and `readValue()` `ObjectMapper`
1111
methods which accept Strings
1212
(requested by matthew-pwnieexpress@github)

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

-11
Original file line numberDiff line numberDiff line change
@@ -845,17 +845,6 @@ public int hashCode() {
845845
return _children.hashCode();
846846
}
847847

848-
/*
849-
/**********************************************************
850-
/* JDK Serialization support
851-
/**********************************************************
852-
*/
853-
854-
// Simplest way is by using a helper
855-
Object writeReplace() {
856-
return NodeSerialization.from(this);
857-
}
858-
859848
/*
860849
/**********************************************************
861850
/* Internal methods (overridable)

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

+10
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,20 @@
1414
* The main addition here is that we declare that sub-classes must
1515
* implement {@link JsonSerializable}.
1616
* This simplifies object mapping aspects a bit, as no external serializers are needed.
17+
*<p>
18+
* Since 2.10, all implements have been {@link java.io.Serializable}.
1719
*/
1820
public abstract class BaseJsonNode
1921
extends JsonNode
22+
implements java.io.Serializable
2023
{
24+
private static final long serialVersionUID = 1L;
25+
26+
// Simplest way is by using a helper
27+
Object writeReplace() {
28+
return NodeSerialization.from(this);
29+
}
30+
2131
protected BaseJsonNode() { }
2232

2333
/*

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package com.fasterxml.jackson.databind.node;
22

33
import java.io.IOException;
4+
import java.io.ObjectInput;
5+
import java.io.ObjectOutput;
46

57
/**
68
* Helper value class only used during JDK serialization: contains JSON as `byte[]`
79
*
810
* @since 2.10
911
*/
10-
class NodeSerialization implements java.io.Serializable {
12+
class NodeSerialization implements java.io.Serializable,
13+
java.io.Externalizable
14+
{
1115
private static final long serialVersionUID = 1L;
1216

1317
public byte[] json;
1418

19+
public NodeSerialization() { }
20+
1521
public NodeSerialization(byte[] b) { json = b; }
1622

1723
protected Object readResolve() {
@@ -29,4 +35,17 @@ public static NodeSerialization from(Object o) {
2935
throw new IllegalArgumentException("Failed to JDK serialize `"+o.getClass().getSimpleName()+"` value: "+e.getMessage(), e);
3036
}
3137
}
38+
39+
@Override
40+
public void writeExternal(ObjectOutput out) throws IOException {
41+
out.writeInt(json.length);
42+
out.write(json);
43+
}
44+
45+
@Override
46+
public void readExternal(ObjectInput in) throws IOException {
47+
final int len = in.readInt();
48+
json = new byte[len];
49+
in.readFully(json, 0, len);
50+
}
3251
}

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

-11
Original file line numberDiff line numberDiff line change
@@ -875,17 +875,6 @@ public int hashCode()
875875
return _children.hashCode();
876876
}
877877

878-
/*
879-
/**********************************************************
880-
/* JDK Serialization support
881-
/**********************************************************
882-
*/
883-
884-
// Simplest way is by using a helper
885-
Object writeReplace() {
886-
return NodeSerialization.from(this);
887-
}
888-
889878
/*
890879
/**********************************************************
891880
/* Internal methods (overridable)

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

+24-7
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public void testObjectNodeSerialization() throws Exception
2525
ObjectNode misc = root.with("misc");
2626
misc.put("value", 0.25);
2727

28-
byte[] ser = jdkSerialize(root);
29-
JsonNode result = jdkDeserialize(ser);
30-
assertEquals(root, result);
28+
testNodeRoundtrip(root);
3129
}
3230

3331
// [databind#18]: Allow JDK serialization of `ArrayNode`
@@ -39,17 +37,36 @@ public void testArrayNodeSerialization() throws Exception
3937
props.put("answer", 42);
4038
root.add(137);
4139

42-
byte[] ser = jdkSerialize(root);
43-
JsonNode result = jdkDeserialize(ser);
44-
assertEquals(root, result);
40+
testNodeRoundtrip(root);
41+
}
42+
43+
// and then also some scalar types
44+
public void testScalarSerialization() throws Exception
45+
{
46+
testNodeRoundtrip(MAPPER.getNodeFactory().nullNode());
47+
48+
testNodeRoundtrip(MAPPER.getNodeFactory().textNode("Foobar"));
49+
50+
testNodeRoundtrip(MAPPER.getNodeFactory().booleanNode(true));
51+
testNodeRoundtrip(MAPPER.getNodeFactory().booleanNode(false));
52+
53+
testNodeRoundtrip(MAPPER.getNodeFactory().numberNode(123));
54+
testNodeRoundtrip(MAPPER.getNodeFactory().numberNode(-12345678901234L));
4555
}
4656

4757
/*
4858
/**********************************************************
4959
/* Helper methods
5060
/**********************************************************
5161
*/
52-
62+
63+
protected void testNodeRoundtrip(JsonNode input) throws Exception
64+
{
65+
byte[] ser = jdkSerialize(input);
66+
JsonNode result = jdkDeserialize(ser);
67+
assertEquals(input, result);
68+
}
69+
5370
protected byte[] jdkSerialize(Object o) throws IOException
5471
{
5572
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000);

0 commit comments

Comments
 (0)