Serialisasi object adalah sebuah proses mengubah object menjadi byte stream yang di representasikan dalam sebuah string. Proses serialisasi object digunakan untuk menyimpan suatu object kedalam file atau memori dalam komputer atau untuk keperluan transmisi object. Kebalikan dari proses serialisasi adalah proses deserialisasi. Show Untuk melakukan proses serialisasi dalam PHP digunakan fungsi serialize() dan untuk melakukan proses deserialisasi digunakan fungsi unserialize(). Fungsi serialize mengembalikan nilai string yang berisi representasi dari nilai sebuah object yang dapat disimpan dalam PHP. Sedangkan fungsi unserialize() digunakan untuk membuat ulang object dari string yang dibuat oleh fungsi serialize(). Berikut ini contoh program menggunakan serialize() dan unserialize()1. Buat sebuah sebuah class dengan nama A berikut ini, lalu simpan dalam file “classa.inc”<?php Pada class A terdapat sebuah properti $text dan sebuah method tampilkan() serta sebuah constructor untuk menentuan nilai properti $text saat instantisasi class kedalam object. 2. Buat sebuah program untuk men-serialisasi object lalu simpan dengan nama page1.php<?php Pada program diatas kita membuat sebuah form dengan input text untuk memasukkan nilai dan sebuah tombol submit unruk mengeksekusi proses serialisasi object. Baca Juga: Membuka dan Menampilkan Isi Direktori dengan PHP Ada tiga tampilan pada program ini, yaitu saat kondisi awal akan tampil form input lalu saat dijalankan form akan disembunyikan dan sebagai gantinya akan tampil pesan bahwa proses serialisasi berhasil. Yang ketiga jika teks kosong pesan akan berubah menjadi peringatan bahwa teks kosong dan proses serialisasi dibatalkan. JIka proses serialisasi berhasil kita bisa membuka file page2.php melalui link untuk melihat hasil serialisasi melalui proses unserialize. 3. Buat sebuah program untuk men-unserialize lalu simpan dengan nama page2.php<?php pada program diatas kita mengambil hasil proses serialisasi dari file page1.php dengan fungsi unserialize() lalu menginstantisasi menjadi object dan menampilkan nilainya dengan method tampilkan(). Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory. Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform. Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object. The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out − public final void writeObject(Object x) throws IOException The above method serializes an Object and sends it to the output stream. Similarly, the ObjectInputStream class contains the following method for deserializing an object − public final Object readObject() throws IOException, ClassNotFoundException This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so you will need to cast it to its appropriate data type. To demonstrate how serialization works in Java, I am going to use the Employee class that we discussed early on in the book. Suppose that we have the following Employee class, which implements the Serializable interface − Examplepublic class Employee implements java.io.Serializable { public String name; public String address; public transient int SSN; public int number; public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address); } } Notice that for a class to be serialized successfully, two conditions must be met −
If you are curious to know if a Java Standard Class is serializable or not, check the documentation for the class. The test is simple: If the class implements java.io.Serializable, then it is serializable; otherwise, it's not. Serializing an ObjectThe ObjectOutputStream class is used to serialize an Object. The following SerializeDemo program instantiates an Employee object and serializes it to a file. When the program is done executing, a file named employee.ser is created. The program does not generate any output, but study the code and try to determine what the program is doing. Note − When serializing an object to a file, the standard convention in Java is to give the file a .ser extension. Exampleimport java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "Reyan Ali"; e.address = "Phokka Kuan, Ambehta Peer"; e.SSN = 11122333; e.number = 101; try { FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in /tmp/employee.ser"); } catch (IOException i) { i.printStackTrace(); } } } Deserializing an ObjectThe following DeserializeDemo program deserializes the Employee object created in the SerializeDemo program. Study the program and try to determine its output − Exampleimport java.io.*; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("/tmp/employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); return; } catch (ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); } } This will produce the following result − OutputDeserialized Employee... Name: Reyan Ali Address:Phokka Kuan, Ambehta Peer SSN: 0 Number:101 Here are following important points to be noted −
|