-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efa6044
commit 92ca9f4
Showing
9 changed files
with
159 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/com/fasterxml/jackson/databind/node/NodeSerialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.fasterxml.jackson.databind.node; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Helper value class only used during JDK serialization: contains JSON as `byte[]` | ||
* | ||
* @since 2.10 | ||
*/ | ||
class NodeSerialization implements java.io.Serializable { | ||
private static final long serialVersionUID = 1L; | ||
|
||
public byte[] json; | ||
|
||
public NodeSerialization(byte[] b) { json = b; } | ||
|
||
protected Object readResolve() { | ||
try { | ||
return InternalNodeMapper.bytesToNode(json); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Failed to JDK deserialize `JsonNode` value: "+e.getMessage(), e); | ||
} | ||
} | ||
|
||
public static NodeSerialization from(Object o) { | ||
try { | ||
return new NodeSerialization(InternalNodeMapper.valueToBytes(o)); | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Failed to JDK serialize `"+o.getClass().getSimpleName()+"` value: "+e.getMessage(), e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/test/java/com/fasterxml/jackson/databind/TestNodeJDKSerialization.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.fasterxml.jackson.databind; | ||
|
||
import java.io.*; | ||
|
||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
|
||
public class TestNodeJDKSerialization extends BaseMapTest | ||
{ | ||
private final ObjectMapper MAPPER = newObjectMapper(); | ||
|
||
/* | ||
/********************************************************** | ||
/* Then something bit different; serialize `JsonNode`(s) | ||
/********************************************************** | ||
*/ | ||
|
||
// [databind#18]: Allow JDK serialization of `ObjectNode` | ||
public void testObjectNodeSerialization() throws Exception | ||
{ | ||
ObjectNode root = MAPPER.createObjectNode(); | ||
root.put("answer", 42); | ||
ArrayNode arr = root.withArray("matrix"); | ||
arr.add(1).add(12345678901L).add(true).add("..."); | ||
ObjectNode misc = root.with("misc"); | ||
misc.put("value", 0.25); | ||
|
||
byte[] ser = jdkSerialize(root); | ||
JsonNode result = jdkDeserialize(ser); | ||
assertEquals(root, result); | ||
} | ||
|
||
// [databind#18]: Allow JDK serialization of `ArrayNode` | ||
public void testArrayNodeSerialization() throws Exception | ||
{ | ||
ArrayNode root = MAPPER.createArrayNode(); | ||
root.add(false); | ||
ObjectNode props = root.addObject(); | ||
props.put("answer", 42); | ||
root.add(137); | ||
|
||
byte[] ser = jdkSerialize(root); | ||
JsonNode result = jdkDeserialize(ser); | ||
assertEquals(root, result); | ||
} | ||
|
||
/* | ||
/********************************************************** | ||
/* Helper methods | ||
/********************************************************** | ||
*/ | ||
|
||
protected byte[] jdkSerialize(Object o) throws IOException | ||
{ | ||
ByteArrayOutputStream bytes = new ByteArrayOutputStream(1000); | ||
ObjectOutputStream obOut = new ObjectOutputStream(bytes); | ||
obOut.writeObject(o); | ||
obOut.close(); | ||
return bytes.toByteArray(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
protected <T> T jdkDeserialize(byte[] raw) throws IOException | ||
{ | ||
ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(raw)); | ||
try { | ||
return (T) objIn.readObject(); | ||
} catch (ClassNotFoundException e) { | ||
fail("Missing class: "+e.getMessage()); | ||
return null; | ||
} finally { | ||
objIn.close(); | ||
} | ||
} | ||
} |