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

added support for generic and custom data types #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -214,6 +216,7 @@ public static class Builder implements ITransactionAttributes, IBuild {
* The metadata.
*/
private Object metadata = null;
private Class metadataClass = null;

/**
* The assets.
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MetaDataSerializer implements JsonSerializer<MetaData>
public JsonElement serialize( MetaData src, Type typeOfSrc, JsonSerializationContext context )
{
Gson gson = JsonUtils.getGson();
JsonElement metadata = gson.toJsonTree( src.getMetadata(), new TypeToken<Map<String, String>>() { }.getType() );
JsonElement metadata = gson.toJsonTree( src.getMetaData(), src.getDataClass() );//new TypeToken<Map<String, String>>() { }.getType() );
return metadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() ) {
Expand Down
74 changes: 52 additions & 22 deletions src/main/java/com/bigchaindb/model/MetaData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String,String> metadata = new TreeMap<String, String>();
/** 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<String, String> 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;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/bigchaindb/model/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down