Skip to content

Serialization and Deserialization in Java for Hyperledger v1.3. Object to Byte[] Array and Byte[] Array to Object

Notifications You must be signed in to change notification settings

iridescent995/Serialization_javachaincode_hyperledger1.3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 

Repository files navigation

Serialization of Java Object for Hyperledger v1.3

Serialization and Deserialization in Java. Object to Byte[] Array and Byte[] Array to Object to use in Hyperledger v1.3 to store state as a byte[] array instead of a String:

By:

putState(java.lang.String key, byte[] value)

instead of

putStringState(java.lang.String key, java.lang.String value)

Code

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Bytearray {

		public static void main(String[] args) {
			
			test obj = new test();
			obj.id="123";
			obj.Name="Harry";
			
			byte[] data = getbytes(obj);
			System.out.println("========");
			System.out.println(data);
			
			test obj1 = getobj(data);
			System.out.println(obj1.Name);
		}

		private static test getobj(byte[] data) {
			test obj = null;
			try {
			ByteArrayInputStream in = new ByteArrayInputStream(data);
			ObjectInputStream is = new ObjectInputStream(in);
			obj = (test) is.readObject();
			}catch(Exception e) {
				System.out.println(e);
			}
			return obj;
		}

		private static byte[] getbytes(test obj) {
			byte[] data= null;
			
			try {
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				ObjectOutputStream oos =  new ObjectOutputStream(bos);
				oos.writeObject(obj);
				data = bos.toByteArray();
				System.out.println(data);
			}catch(Exception e) {
				System.out.println(e);
			}
			
			return data;
		}
}


class test implements Serializable{
	String id;
	String Name;
}

About

Serialization and Deserialization in Java for Hyperledger v1.3. Object to Byte[] Array and Byte[] Array to Object

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages