Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#357 implemented PlutusDataToUFT8Json converter #358

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.bloxbean.cardano.client.plutus.spec.serializers;

import co.nstant.in.cbor.model.*;
import com.bloxbean.cardano.client.exception.CborSerializationException;
import com.bloxbean.cardano.client.plutus.spec.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
@@ -8,6 +10,10 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.NonNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.bloxbean.cardano.client.plutus.spec.serializers.PlutusDataJsonKeys.*;

/**
@@ -63,4 +69,94 @@ else if (jsonNode.has(LIST)) {
public static String toJson(PlutusData plutusData) throws JsonProcessingException {
return mapper.writeValueAsString(plutusData);
}
}

/**
* Convert {@link PlutusData} to utf8 encoded json String
* @param plutusData data to be encoded
* @return utf8 encoded json
* @throws JsonProcessingException
* @throws CborSerializationException
*/
public static String toUTF8Json(PlutusData plutusData) throws JsonProcessingException, CborSerializationException {
DataItem serializedPlutusData = plutusData.serialize();
Object o = parseItem(serializedPlutusData);
return mapper.writeValueAsString(o);
}

/**
* parsing a {@link DataItem} to utf8 object regarding it's type
* @param item cbor {@link DataItem}
* @return utf8 encoded representation
*/
private static Object parseItem(DataItem item) {
Object value = "";
switch (item.getMajorType()) {
case BYTE_STRING:
value = bytestringItemToString((ByteString) item);
break;
case ARRAY:
value = dataItemArrayToString((Array) item);
break;
case MAP:
value = parseDataItemMap((Map) item);
break;
case UNSIGNED_INTEGER:
value = ((UnsignedInteger)item).getValue();
break;
case NEGATIVE_INTEGER:
value = ((NegativeInteger)item).getValue();
break;
case UNICODE_STRING:
value = ((UnicodeString)item).getString();
break;
case TAG:
value = ((Tag) item).getValue();
break;
default:
throw new UnsupportedOperationException("Unkown type. Not implemented"); // TODO need to implement the other types
}
return value;
}

/**
* {@link ByteString} item parsing to utf8 string
* @param item
* @return
*/
private static String bytestringItemToString(ByteString item) {
String value = "";
byte[] bytes = item.getBytes();
value = new String(bytes);
return value;
}

/**
* {@link Array} item parsing to utf8 representation
* @param array
* @return
*/
private static Object dataItemArrayToString(Array array) {
List<Object> decodedItemList = new ArrayList<>();
for (DataItem dataItem : array.getDataItems()) {
if(!dataItem.getMajorType().equals(MajorType.SPECIAL)){
decodedItemList.add(parseItem(dataItem));
}
}
return decodedItemList.stream().toArray(Object[]::new);
}

/**
* {@link Map} item parsing to utf8 representation
* @param m
* @return
*/
private static HashMap<String, Object> parseDataItemMap(Map m) {
HashMap<String, Object> decodedMap = new HashMap<>();
for (DataItem keyItem : m.getKeys()) {
String keyString = (String) parseItem(keyItem);
Object valueString = parseItem(m.get(keyItem));
decodedMap.put(keyString, valueString);
}
return decodedMap;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bloxbean.cardano.client.plutus.spec.serializers;

import co.nstant.in.cbor.model.ByteString;
import com.bloxbean.cardano.client.plutus.spec.PlutusData;
import com.bloxbean.cardano.client.util.HexUtil;
import org.junit.jupiter.api.Test;
@@ -60,4 +61,14 @@ void serDeser_largePlutusData() throws Exception {
assertThat(plutusData).isNotNull();
assertThat(json).isNotNull();
}

@Test
void utf8_ByteStringdeserializationTest() throws Exception {
String datumHex = "d8799fa6446e616d654c54657374313233546f6b656e45696d6167655835697066733a2f2f516d5763636a566279694d6a394637775278725053743554387953687974714331646f5132585072457838485379496d65646961747970654a696d6167652f6a706567496d65646961547970654a696d6167652f6a7065674b6465736372697074696f6e404566696c65739fa3496d65646961747970654a696d6167652f6a706567446e616d654a696d6167652f6a706567437372635835697066733a2f2f516d5763636a566279694d6a394637775278725053743554387953687974714331646f5132585072457838485379ff01ff";
PlutusData plutusData = PlutusData.deserialize(HexUtil.decodeHexString(datumHex));

String json = PlutusDataJsonConverter.toUTF8Json(plutusData);
assertThat(plutusData).isNotNull();
assertThat(json).isNotNull();
}
}