EOSIO / eosio-java-android-abieos-serialization-provider

Pluggable serialization provider for EOSIO SDK for Java using ABIEOS
https://eosio.github.io/eosio-java-android-abieos-serialization-provider/
MIT License
11 stars 14 forks source link

How to serialize an action's data only ?? #38

Closed shamtroon closed 2 years ago

shamtroon commented 5 years ago

Hey guys, I want to serialize an action's data. I didn't get any function. But in your document you describe an example using "serializeTransaction(json)", but it accepts only transaction and I am providing only a sample json. Do you have any function which serialize any json ??

try { AbiEos abieos = new AbiEosSerializationProviderImpl() } catch (SerializationProviderError serializationProviderError) { serializationProviderError.printStackTrace(); }

String hex = "1686755CA99DE8E73E1200" // some binary data String json = "{"name": "John"}" // some JSON

try { String jsonToBinaryTransaction = abieos.serializeTransaction(json) } catch (SerializeTransactionError err) { err.printStackTrace(); }

try { String binaryToJsonTransaction = abieos.deserializeTransaction(hex) } catch (DeserializeTransactionError err) { err.printStackTrace(); }

I tried this example but it cause exception that expiration is missing.

jlamarr22 commented 4 years ago

Hi @shamtroon, I would assume you've solved this issue by now, but I just wanted to respond to this in case someone else was having an issue.

The methods serializeTransaction/deserializeTransaction expect JSON/Hex representations of a full transaction, which is why you're seeing an error that expiration was missing (because it's included in a transaction and not in your JSON example above). There are examples of this: serialization and deserialization.

If you want to just serialize/deserialize an action's data, you need to pass the ABI as well to serialize/deserialize. There's an example of this deserialization here.

Lastly, if you want to serialize/deserialize an action's return value (new feature), then you need to pass the return value's type:

String RETURN_VALUE_ABI = "{\"version\":\"eosio::abi/1.2\",\"types\":[],\"structs\":[{\"name\":\"retval.complex\",\"base\":\"\",\"fields\":[{\"name\":\"user\",\"type\":\"name\"}]},{\"name\":\"retval.simple\",\"base\":\"\",\"fields\":[{\"name\":\"user\",\"type\":\"name\"}]},{\"name\":\"retval.null\",\"base\":\"\",\"fields\":[{\"name\":\"user\",\"type\":\"name\"}]},{\"name\":\"returnValue\",\"base\":\"\",\"fields\":[{\"name\":\"id\",\"type\":\"uint32\"},{\"name\":\"name\",\"type\":\"name\"}]}],\"actions\":[{\"name\":\"retval.complex\",\"type\":\"retval.complex\",\"ricardian_contract\":\"\"},{\"name\":\"retval.simple\",\"type\":\"retval.simple\",\"ricardian_contract\":\"\"},{\"name\":\"retval.null\",\"type\":\"retval.null\",\"ricardian_contract\":\"\"}],\"tables\":[],\"ricardian_clauses\":[],\"error_messages\":[],\"abi_extensions\":[],\"variants\":[]}";

String json = "10";
String returnValueType = "uint32";
String hexResult = null;
String jsonResult = null;

try {
    AbiEosSerializationObject serializationObject = new AbiEosSerializationObject("contract", "retval.simple", returnValueType, RETURN_VALUE_ABI);
    serializationObject.setJson(json);
    abieos.serialize(serializationObject);
    hexResult = serializationObject.getHex();
} catch (SerializeError err) {
    err.printStackTrace();
}

try {
    AbiEosSerializationObject serializationObject = new AbiEosSerializationObject("contract", "retval.simple", returnValueType, RETURN_VALUE_ABI);
    serializationObject.setHex(hexResult);
    abieos.deserialize(serializationObject);
    jsonResult = serializationObject.getJson();
} catch (DeserializeError err) {
    err.printStackTrace();
}
shamtroon commented 4 years ago

Thanks @jlamarr22 for your response, Yes you are right, I get some work-around and solved this issue. Actually I need to serialize action data, and got an example from you readme.md file so I tried, but it causes an exception. Kindly update this file as this example is nor working or causing issue.

Again thanks for response.