-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathTransactionTargetDeserializer.java
More file actions
75 lines (66 loc) · 3.11 KB
/
TransactionTargetDeserializer.java
File metadata and controls
75 lines (66 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.casper.sdk.jackson.deserializer;
import com.casper.sdk.exception.DeserializationException;
import com.casper.sdk.exception.NoSuchTypeException;
import com.casper.sdk.model.transaction.target.*;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.syntifi.crypto.key.encdec.Hex;
import java.io.IOException;
import static com.casper.sdk.model.transaction.target.TargetConstants.*;
/**
* Deserializer for {@link TransactionTarget} types.
*
* @author ian@meywood.com
*/
public class TransactionTargetDeserializer extends JsonDeserializer<TransactionTarget> {
private static final String NATIVE_JSON = "\"Native\"";
@Override
public TransactionTarget deserialize(final JsonParser p, final DeserializationContext ctxt) throws IOException {
if (p.getCurrentToken() == JsonToken.START_OBJECT) {
final ObjectNode treeNode = p.readValueAsTree();
final String fieldName = treeNode.fieldNames().next();
if (SESSION.equals(fieldName)) {
return createSession(treeNode.get(fieldName));
} else if (STORED.equals(fieldName)) {
return createStored(treeNode.get(fieldName), ctxt);
} else {
throw new IllegalArgumentException("Unknown transaction target type: " + fieldName);
}
} else if (p.getCurrentToken() == JsonToken.VALUE_STRING && NATIVE_JSON.equals(p.readValueAsTree().toString())) {
return new Native();
} else {
throw new IllegalArgumentException("Unknown transaction target type: " + p.readValueAsTree());
}
}
private Stored createStored(final JsonNode node, final DeserializationContext ctx) throws IOException {
try {
return new Stored(
createInvocationTarget(node, ctx),
TransactionRuntime.fromJson(node.get(RUNTIME))
);
} catch (NoSuchTypeException e) {
throw new DeserializationException("Unable to find 'runtime'", e);
}
}
private TransactionInvocationTarget createInvocationTarget(final JsonNode node, final DeserializationContext ctx) throws IOException {
try (final JsonParser parser = node.get(ID).traverse()) {
parser.setCodec(ctx.getParser().getCodec());
return parser.readValueAs(TransactionInvocationTarget.class);
}
}
private Session createSession(final JsonNode node) throws DeserializationException {
try {
return new Session(
node.has(IS_INSTALL_UPGRADE) && node.get(IS_INSTALL_UPGRADE).asBoolean(),
TransactionRuntime.fromJson(node.get(RUNTIME)),
Hex.decode(node.get(MODULE_BYTES).asText())
);
} catch (NoSuchTypeException e) {
throw new DeserializationException("Unable to find required fields", e);
}
}
}