use of io.nem.symbol.catapult.builders.TransactionBuilder in project nem2-sdk-java by nemtech.
the class BinarySerializationImpl method deserializeToFactory.
/**
* Deserialization of transactions. All the code related to the deserialization is handled in the
* class and its helpers. Transaction Model Objects are not polluted with deserialization
* functionality.
*
* @param payload the byte array payload
* @return the {@link TransactionFactory}.
*/
@Override
public TransactionFactory<?> deserializeToFactory(byte[] payload) {
Validate.notNull(payload, "Payload must not be null");
DataInputStream stream = SerializationUtils.toDataInput(payload);
TransactionBuilder builder = TransactionBuilderHelper.loadFromBinary(stream);
return toTransactionFactory(builder);
}
use of io.nem.symbol.catapult.builders.TransactionBuilder in project nem2-sdk-java by nemtech.
the class TransactionMapperSerializationOkHttpTest method testDtoToModelMapping.
@ParameterizedTest
@MethodSource("transactionJsonFiles")
void testDtoToModelMapping(String jsonFilename) {
String json = TestHelperOkHttp.loadResource(jsonFilename);
TransactionInfoDTO originalTransactionInfo = jsonHelper.parse(json, TransactionInfoDTO.class);
Transaction transactionModel = transactionMapper.mapFromDto(originalTransactionInfo);
Integer version = jsonHelper.getInteger(originalTransactionInfo, "transaction", "version");
Integer type = jsonHelper.getInteger(originalTransactionInfo, "transaction", "type");
Integer network = jsonHelper.getInteger(originalTransactionInfo, "transaction", "network");
Assertions.assertEquals(transactionModel.getVersion(), version);
Assertions.assertEquals(transactionModel.getType().getValue(), type);
Assertions.assertEquals(transactionModel.getNetworkType().getValue(), network);
Assertions.assertNotNull(transactionModel);
TransactionInfoDTO mappedTransactionInfo = (TransactionInfoDTO) transactionMapper.mapToDto(transactionModel);
Map<String, Object> transactionMap = jsonHelper.convert(mappedTransactionInfo.getTransaction(), Map.class);
Map<String, Object> originalTransactionMap = jsonHelper.convert(originalTransactionInfo.getTransaction(), Map.class);
originalTransactionMap.put("size", transactionModel.getSize());
originalTransactionInfo.setTransaction(originalTransactionMap);
// Patching the sort
mappedTransactionInfo.setTransaction(transactionMap);
mappedTransactionInfo.setMeta(jsonHelper.convert(mappedTransactionInfo.getMeta(), Map.class));
BinarySerialization serialization = new BinarySerializationImpl();
String serialized = ConvertUtils.toHex(serialization.serialize(transactionModel));
TransactionBuilder builder = TransactionBuilderHelper.loadFromBinary(SerializationUtils.toDataInput(serialization.serialize(transactionModel)));
System.out.println(ConvertUtils.toHex(builder.serialize()));
serialization.deserialize(serialization.serialize(transactionModel));
Assertions.assertEquals(jsonHelper.prettyPrint(originalTransactionInfo), jsonHelper.prettyPrint(mappedTransactionInfo));
Assertions.assertEquals(serialized, ConvertUtils.toHex(serialization.serialize(transactionMapper.mapFromDto(mappedTransactionInfo))));
originalTransactionInfo.setMeta(null);
Map<String, Object> transactionJson = (Map<String, Object>) originalTransactionInfo.getTransaction();
if (transactionJson.containsKey("transactions")) {
List<Map<String, Object>> transactionsJson = (List<Map<String, Object>>) transactionJson.get("transactions");
transactionsJson.forEach(t -> t.remove("meta"));
}
}
use of io.nem.symbol.catapult.builders.TransactionBuilder in project nem2-sdk-java by nemtech.
the class BinarySerializationImpl method serialize.
/**
* Serialization basic implementation, it just delegates the work to the transactions.
*
* @param transaction the transaction
* @return the serialized transaction.
*/
@Override
public <T extends Transaction> byte[] serialize(T transaction) {
Validate.notNull(transaction, "Transaction must not be null");
TransactionBuilder transactionBuilder = getTransactionBuilder(transaction);
return serializeTransaction(transactionBuilder.serialize(), transaction);
}
Aggregations