From 28f578dc7c5c14930682658c162ccdeb54c3128b Mon Sep 17 00:00:00 2001 From: Manan Patel Date: Wed, 26 Sep 2018 12:35:05 +0200 Subject: [PATCH] added support for generic and custom data types --- .../BigchainDbTransactionBuilder.java | 12 ++- .../json/strategy/MetaDataSerializer.java | 2 +- .../strategy/TransactionDeserializer.java | 3 +- .../java/com/bigchaindb/model/MetaData.java | 74 +++++++++++++------ .../com/bigchaindb/model/Transaction.java | 6 +- 5 files changed, 66 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/bigchaindb/builders/BigchainDbTransactionBuilder.java b/src/main/java/com/bigchaindb/builders/BigchainDbTransactionBuilder.java index 5f79f61..15157aa 100644 --- a/src/main/java/com/bigchaindb/builders/BigchainDbTransactionBuilder.java +++ b/src/main/java/com/bigchaindb/builders/BigchainDbTransactionBuilder.java @@ -31,6 +31,7 @@ import com.bigchaindb.model.FulFill; import com.bigchaindb.model.GenericCallback; import com.bigchaindb.model.Input; +import com.bigchaindb.model.MetaData; import com.bigchaindb.model.Output; import com.bigchaindb.model.Transaction; import com.bigchaindb.util.DriverUtils; @@ -110,7 +111,7 @@ public interface ITransactionAttributes { * Adds the assets. * * @param assets the assets - * @param assetsDataClass class if asset data + * @param assetsDataClass class of asset data * @return the i asset meta data */ ITransactionAttributes addAssets(Object assets, Class assetsDataClass); @@ -119,9 +120,10 @@ public interface ITransactionAttributes { * Adds the meta data. * * @param metaData the json object + * @param metaDataClass class of meta data * @return the i asset meta data */ - ITransactionAttributes addMetaData(Object metaData); + ITransactionAttributes addMetaData(Object metaData, Class metaDataClass); /** * Add the class and deserializer for metadata @@ -214,6 +216,7 @@ public static class Builder implements ITransactionAttributes, IBuild { * The metadata. */ private Object metadata = null; + private Class metadataClass = null; /** * The assets. @@ -332,8 +335,9 @@ public ITransactionAttributes addMetaDataClassSerializer(Class metaDataClass, Js return this; } - public ITransactionAttributes addMetaData(Object object) { + public ITransactionAttributes addMetaData(Object object, Class metaDataClass) { this.metadata = object; + this.metadataClass = metaDataClass; return this; } @@ -378,7 +382,7 @@ public IBuild build(EdDSAPublicKey publicKey) throws Exception{ // otherwise it's an asset this.transaction.setAsset(new Asset(this.assets, this.assetsDataClass)); } - this.transaction.setMetaData(this.metadata); + this.transaction.setMetaData(new MetaData(this.metadata, this.metadataClass)); this.transaction.setVersion("2.0"); this.transaction.setId(null); diff --git a/src/main/java/com/bigchaindb/json/strategy/MetaDataSerializer.java b/src/main/java/com/bigchaindb/json/strategy/MetaDataSerializer.java index 188e3b1..dc960b8 100644 --- a/src/main/java/com/bigchaindb/json/strategy/MetaDataSerializer.java +++ b/src/main/java/com/bigchaindb/json/strategy/MetaDataSerializer.java @@ -25,7 +25,7 @@ public class MetaDataSerializer implements JsonSerializer public JsonElement serialize( MetaData src, Type typeOfSrc, JsonSerializationContext context ) { Gson gson = JsonUtils.getGson(); - JsonElement metadata = gson.toJsonTree( src.getMetadata(), new TypeToken>() { }.getType() ); + JsonElement metadata = gson.toJsonTree( src.getMetaData(), src.getDataClass() );//new TypeToken>() { }.getType() ); return metadata; } } diff --git a/src/main/java/com/bigchaindb/json/strategy/TransactionDeserializer.java b/src/main/java/com/bigchaindb/json/strategy/TransactionDeserializer.java index 057fc6a..78b0ed6 100644 --- a/src/main/java/com/bigchaindb/json/strategy/TransactionDeserializer.java +++ b/src/main/java/com/bigchaindb/json/strategy/TransactionDeserializer.java @@ -6,6 +6,7 @@ package com.bigchaindb.json.strategy; import com.bigchaindb.model.Asset; +import com.bigchaindb.model.MetaData; import com.bigchaindb.model.Input; import com.bigchaindb.model.Output; import com.bigchaindb.model.Transaction; @@ -48,7 +49,7 @@ public Transaction deserialize(JsonElement json, Type typeOfT, JsonDeserializati JsonElement jElement = json.getAsJsonObject(); transaction.setAsset( JsonUtils.fromJson(jElement.getAsJsonObject().get("asset").toString(), Asset.class)); - transaction.setMetaData( JsonUtils.fromJson( jElement.getAsJsonObject().get("metadata").toString(), metaDataClass )); + transaction.setMetaData( JsonUtils.fromJson(jElement.getAsJsonObject().get("metadata").toString(), metaDataClass)); transaction.setId(jElement.getAsJsonObject().get("id").toString().replace("\"", "")); for( JsonElement jInputElement: jElement.getAsJsonObject().get("inputs").getAsJsonArray() ) { diff --git a/src/main/java/com/bigchaindb/model/MetaData.java b/src/main/java/com/bigchaindb/model/MetaData.java index e0fa9e6..51b406f 100644 --- a/src/main/java/com/bigchaindb/model/MetaData.java +++ b/src/main/java/com/bigchaindb/model/MetaData.java @@ -5,54 +5,84 @@ */ package com.bigchaindb.model; +import com.bigchaindb.annotations.Exclude; import com.google.gson.annotations.SerializedName; -import java.util.Map; -import java.util.TreeMap; +import java.io.Serializable; - - -/** +/* * The Class MetaData. */ -public class MetaData { +public class MetaData implements Serializable { + /** + * + */ + private static final long serialVersionUID = 1L; + /** The id. */ @SerializedName("id") + @Exclude private String id; - /** The metadata. */ - @SerializedName("metadata") - private Map metadata = new TreeMap(); + /** The data. */ + @SerializedName("data") + private Object data; + + /** the data class the type of the data class needed for serialization/deserialization */ + @Exclude + private Class dataClass = com.google.gson.internal.LinkedTreeMap.class; + + /** + * Instantiates a new metadata. + */ + public MetaData() {} /** - * Gets the id. + * Instantiates a new metadata. * - * @return the id + * @param data the data + * @param dataClass due to type erasure the data class needs to be provided for serialization/deserialization */ - public String getId() { - return id; + public MetaData(Object data, Class dataClass) { + this.data = data; + this.dataClass = dataClass; } /** - * Sets the id. + * Instantiates a new metadata by reference. * - * @param id the new id + * @param id ID of the metadata. */ - public void setId(String id) { + public MetaData(String id) { this.id = id; } + + /** + * Gets the data. + * + * @return the data + */ + public Object getMetaData() { + return data; + } /** - * Gets the metadata. + * return the type of the Asset data class * - * @return the metadata + * @return the data class type */ - public Map getMetadata() { - return metadata; + public Class getDataClass() + { + return dataClass; } - public void setMetaData(String key, String value) { - this.metadata.put(key, value); + /** + * Gets the id. + * + * @return the id + */ + public String getId() { + return id; } } diff --git a/src/main/java/com/bigchaindb/model/Transaction.java b/src/main/java/com/bigchaindb/model/Transaction.java index fea54a7..242fcfb 100644 --- a/src/main/java/com/bigchaindb/model/Transaction.java +++ b/src/main/java/com/bigchaindb/model/Transaction.java @@ -126,11 +126,11 @@ public Object getMetaData() { /** * Set the metaData object * - * @param obj the metadata object + * @param metadata the metadata object */ - public void setMetaData( Object obj ) + public void setMetaData( Object metadata ) { - this.metaData = obj; + this.metaData = metadata; } /**