+ @Override
+ Object writeReplace() {
+ return new Proxy(this);
+ }
+
+ static @Nullable ContainerNode unmaskContainer(final Optional<NormalizedNode> optNode)
+ throws InvalidObjectException {
+ if (optNode.isEmpty()) {
+ return null;
+ }
+ final var node = optNode.orElseThrow();
+ if (node instanceof ContainerNode container) {
+ return container;
+ }
+ throw new InvalidObjectException("Unexpected data " + node.contract().getSimpleName());
+ }
+
+ private static class Proxy implements Externalizable {
+ private static final long serialVersionUID = 1L;
+
+ private RpcResponse rpcResponse;
+
+ // checkstyle flags the public modifier as redundant which really doesn't make sense since it clearly isn't
+ // redundant. It is explicitly needed for Java serialization to be able to create instances via reflection.
+ @SuppressWarnings("checkstyle:RedundantModifier")
+ public Proxy() {
+ }
+
+ Proxy(final RpcResponse rpcResponse) {
+ this.rpcResponse = rpcResponse;
+ }
+
+ @Override
+ public void writeExternal(final ObjectOutput out) throws IOException {
+ SerializationUtils.writeNormalizedNode(out, rpcResponse.getOutput());
+ }
+
+ @Override
+ public void readExternal(final ObjectInput in) throws IOException {
+ rpcResponse = new RpcResponse(unmaskContainer(SerializationUtils.readNormalizedNode(in)));
+ }
+
+ private Object readResolve() {
+ return rpcResponse;
+ }
+ }