|
28 | 28 | import com.fasterxml.jackson.databind.node.ObjectNode; |
29 | 29 | import org.xrpl.xrpl4j.model.transactions.Transaction; |
30 | 30 | import org.xrpl.xrpl4j.model.transactions.TransactionType; |
31 | | -import org.xrpl.xrpl4j.model.transactions.UnknownTransaction; |
32 | 31 | import org.xrpl.xrpl4j.model.transactions.UnlModify; |
33 | 32 |
|
34 | 33 | import java.io.IOException; |
35 | | -import java.util.Objects; |
36 | 34 |
|
37 | 35 | /** |
38 | 36 | * Custom deserializer for {@link Transaction}s, which deserializes to a specific {@link Transaction} type based on the |
39 | 37 | * TransactionType JSON field. |
40 | 38 | */ |
41 | 39 | public class TransactionDeserializer<T extends Transaction> extends StdDeserializer<T> { |
42 | | - |
43 | | - private final Class<T> clazz; |
44 | | - |
| 40 | + |
45 | 41 | /** |
46 | 42 | * No-args constructor. |
47 | 43 | */ |
48 | 44 | protected TransactionDeserializer() { |
49 | 45 | super(Transaction.class); |
50 | | - this.clazz = (Class<T>) Transaction.class; |
51 | 46 | } |
52 | | - |
53 | | - /** |
54 | | - * No-args constructor. |
55 | | - */ |
56 | | - protected TransactionDeserializer(final Class<T> clazz) { |
57 | | - super(Transaction.class); |
58 | | - this.clazz = Objects.requireNonNull(clazz); |
59 | | - } |
60 | | - |
| 47 | + |
61 | 48 | @Override |
62 | 49 | public T deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException { |
63 | 50 | ObjectMapper objectMapper = (ObjectMapper) jsonParser.getCodec(); |
64 | 51 | ObjectNode objectNode = objectMapper.readTree(jsonParser); |
65 | | - |
| 52 | + |
66 | 53 | JsonNode node = objectNode.get("TransactionType"); |
67 | 54 | TransactionType transactionType = TransactionType.forValue(node.asText()); |
| 55 | + @SuppressWarnings("unchecked") |
68 | 56 | Class<T> transactionTypeClass = (Class<T>) Transaction.typeMap.inverse().get(transactionType); |
69 | | - |
70 | | - // TODO: DELETE (no longer needed |
71 | | - // If the transaction is of type `UnknownTransaction`, the `TransactionType` property must _not_ be removed. Thi |
72 | | - // is so that the derived functions related to `TransactionType` in that class operate properly. However, for all |
73 | | - // _other_ transaction types, the `TransactionType` property _must_ be removed so that it doesn't errantly show up |
74 | | - // in the `unknownTransactionType` map. This approach works because every subclass of `Transaction` has a derived |
75 | | - // Java method that specifies the type (thus allowing us to ignore this fiele in the general case). |
76 | | -// if (!UnknownTransaction.class.isAssignableFrom(transactionTypeClass)) { |
77 | | -// objectNode.remove("TransactionType"); |
78 | | -// } |
79 | | - |
80 | | - // If the transaction is of type `UnlModify`, then remove the `Account` property. This is because the JSON returned |
81 | | - // by the rippled/clio API v1 has a bug where the account value is often an empty string. For this particular |
82 | | - // transaction type (i.e., `UnlModify`) the Java value for the account is always set to ACCOUNT_ZERO via a default |
83 | | - // method, so we remove this value from the JSON before deserialization because (1) it's not needed (the default |
84 | | - // method handles population in Java) and (2) if not removed, this field will end up in the `unknownFields` |
85 | | - // property, which is incorrect. |
86 | | -// if (UnlModify.class.isAssignableFrom(transactionTypeClass)) { |
87 | | -// // if (objectNode.get("Account").isEmpty()) { |
88 | | -// if (objectNode.has("Account")) { |
89 | | -// //objectNode.remove("Account"); |
90 | | -// } |
91 | | -// } |
92 | | - |
93 | | - // TODO: Verify, and document if keeping. |
| 57 | + |
| 58 | + // Remove the `Account` property from any incoming UnlModify JSON about to be deserialized. This is because the JSON |
| 59 | + // returned by the rippled/clio API v1 has a bug where the account value is an empty string. For this particular |
| 60 | + // For `UnlModify` only, the Java value for the Account is always set to ACCOUNT_ZERO via a default |
| 61 | + // method, so this value is removed from the JSON before deserialization because it's not needed (the default |
| 62 | + // method handles population in Java) and if not removed, this field will end up in the `unknownFields` |
| 63 | + // map of the ultimate Java object, which is incorrect. |
94 | 64 | if (UnlModify.class.isAssignableFrom(transactionTypeClass)) { |
95 | 65 | objectNode.remove("Account"); |
96 | 66 | } |
|
0 commit comments