1 package com.fasterxml.jackson.databind.node;
2
3 import java.io.IOException;
4 import java.io.ObjectInput;
5 import java.io.ObjectOutput;
6
7 /**
8  * Helper value class only used during JDK serialization: contains JSON as `byte[]`
9  *
10  * @since 2.10
11  */

12 class NodeSerialization implements java.io.Serializable,
13     java.io.Externalizable
14 {
15     private static final long serialVersionUID = 1L;
16
17     public byte[] json;
18
19     public NodeSerialization() { }
20
21     public NodeSerialization(byte[] b) { json = b; }
22
23     protected Object readResolve() {
24         try {
25             return InternalNodeMapper.bytesToNode(json);
26         } catch (IOException e) {
27             throw new IllegalArgumentException("Failed to JDK deserialize `JsonNode` value: "+e.getMessage(), e);
28         }
29     }    
30
31     public static NodeSerialization from(Object o) {
32         try {
33             return new NodeSerialization(InternalNodeMapper.valueToBytes(o));
34         } catch (IOException e) {
35             throw new IllegalArgumentException("Failed to JDK serialize `"+o.getClass().getSimpleName()+"` value: "+e.getMessage(), e);
36         }
37     }
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     }
51 }
52