Skip to content

Commit

Permalink
More tweaks for #18
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 5, 2019
1 parent 92ca9f4 commit 1af00fc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 31 deletions.
2 changes: 1 addition & 1 deletion release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Project: jackson-databind

2.10.0 (not yet released)

#18: Make `ObjectNode` and `ArrayNode` serializable
#18: Make `JsonNode` serializable
#1675: Remove "impossible" `IOException` in `readTree()` and `readValue()` `ObjectMapper`
methods which accept Strings
(requested by matthew-pwnieexpress@github)
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/com/fasterxml/jackson/databind/node/ArrayNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -845,17 +845,6 @@ public int hashCode() {
return _children.hashCode();
}

/*
/**********************************************************
/* JDK Serialization support
/**********************************************************
*/

// Simplest way is by using a helper
Object writeReplace() {
return NodeSerialization.from(this);
}

/*
/**********************************************************
/* Internal methods (overridable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@
* The main addition here is that we declare that sub-classes must
* implement {@link JsonSerializable}.
* This simplifies object mapping aspects a bit, as no external serializers are needed.
*<p>
* Since 2.10, all implements have been {@link java.io.Serializable}.
*/
public abstract class BaseJsonNode
extends JsonNode
implements java.io.Serializable
{
private static final long serialVersionUID = 1L;

// Simplest way is by using a helper
Object writeReplace() {
return NodeSerialization.from(this);
}

protected BaseJsonNode() { }

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package com.fasterxml.jackson.databind.node;

import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

/**
* Helper value class only used during JDK serialization: contains JSON as `byte[]`
*
* @since 2.10
*/
class NodeSerialization implements java.io.Serializable {
class NodeSerialization implements java.io.Serializable,
java.io.Externalizable
{
private static final long serialVersionUID = 1L;

public byte[] json;

public NodeSerialization() { }

public NodeSerialization(byte[] b) { json = b; }

protected Object readResolve() {
Expand All @@ -29,4 +35,17 @@ public static NodeSerialization from(Object o) {
throw new IllegalArgumentException("Failed to JDK serialize `"+o.getClass().getSimpleName()+"` value: "+e.getMessage(), e);
}
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeInt(json.length);
out.write(json);
}

@Override
public void readExternal(ObjectInput in) throws IOException {
final int len = in.readInt();
json = new byte[len];
in.readFully(json, 0, len);
}
}
11 changes: 0 additions & 11 deletions src/main/java/com/fasterxml/jackson/databind/node/ObjectNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -875,17 +875,6 @@ public int hashCode()
return _children.hashCode();
}

/*
/**********************************************************
/* JDK Serialization support
/**********************************************************
*/

// Simplest way is by using a helper
Object writeReplace() {
return NodeSerialization.from(this);
}

/*
/**********************************************************
/* Internal methods (overridable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ public void testObjectNodeSerialization() throws Exception
ObjectNode misc = root.with("misc");
misc.put("value", 0.25);

byte[] ser = jdkSerialize(root);
JsonNode result = jdkDeserialize(ser);
assertEquals(root, result);
testNodeRoundtrip(root);
}

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

byte[] ser = jdkSerialize(root);
JsonNode result = jdkDeserialize(ser);
assertEquals(root, result);
testNodeRoundtrip(root);
}

// and then also some scalar types
public void testScalarSerialization() throws Exception
{
testNodeRoundtrip(MAPPER.getNodeFactory().nullNode());

testNodeRoundtrip(MAPPER.getNodeFactory().textNode("Foobar"));

testNodeRoundtrip(MAPPER.getNodeFactory().booleanNode(true));
testNodeRoundtrip(MAPPER.getNodeFactory().booleanNode(false));

testNodeRoundtrip(MAPPER.getNodeFactory().numberNode(123));
testNodeRoundtrip(MAPPER.getNodeFactory().numberNode(-12345678901234L));
}

/*
/**********************************************************
/* Helper methods
/**********************************************************
*/


protected void testNodeRoundtrip(JsonNode input) throws Exception
{
byte[] ser = jdkSerialize(input);
JsonNode result = jdkDeserialize(ser);
assertEquals(input, result);
}

protected byte[] jdkSerialize(Object o) throws IOException
{
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000);
Expand Down

0 comments on commit 1af00fc

Please sign in to comment.