1

I have a user defined class, say

import java.util.Calendar;

public class Employee{
    private String name;
    private int age;
    private Calendar dob;
    private Address address;
    private boolean married;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public Calendar getDob() {
        return dob;
    }
    public void setDob(Calendar dob) {
        this.dob = dob;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public boolean isMarried() {
        return married;
    }
    public void setMarried(boolean married) {
        this.married = married;
    }
}
class Address{
    private int doorNo;
    private String streetName;
    private String city;
    public int getDoorNo() {
        return doorNo;
    }
    public void setDoorNo(int doorNo) {
        this.doorNo = doorNo;
    }
    public String getStreetName() {
        return streetName;
    }
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
}

I am creating an object of Employee and populating it with setters. I have to represent the above object to string (encrypted or human-readable) and parse back to get similar object. Actually, I want to save the string equivalent of java object in a file and to read back them to get a java object. I know we have object writing, but they are sensitive to edit. I would prefer if a java object can be converted to String of human readable form. Thanks.

Ahamed
  • 39,245
  • 13
  • 40
  • 68

4 Answers4

3

You should override toString() to convert instances of your class to string. As for recreating instances based on their string representation you can define a static factory method for this.

public class Employee {
  ...
  @Override
  public String toString() {
    ...
  }

  public static Employee fromString(String str) {
    ...
  }
}

You use these methods like this:

To obtain string representation of an instance to string:

Employee john = ...
String johnString = john.toString();

Note that your toString() method will also be called implicitly whenever there is a need to obtain string representation of one of the instances.

To recreate an instance from string:

Employee john = Employee.fromString(johnString);

If you often need to store instances of the class in a file and read them back, you may also consider serialization. See documentation for Serializable interface as well as ObjectInputStream and ObjectOutputStream. You may also want to familiarize yourself with caveats surrounding serialization by reading the last chapter ("Serialization") in Effective Java, second edition. Most importantly be aware that the serialized form of your class becomes part of your public API.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92
  • The toString() method is okay for representing an object, but for reading them back? Moreover, I have Calendar object, and another user defined object in it. So its difficult to implement our own methods. Is there any available API to do this? Thanks a lot. – Ahamed Jan 02 '12 at 10:59
  • For recreating an instance back from string form I have suggested a static factory method `fromString()`. I have added a demonstration of how to use both methods. – Adam Zalcman Jan 02 '12 at 11:07
  • As for the Calendar instances, you can recreate them based on what is stored in the string produced by `toString()` (you must put enough information to allow this to your string representation). Also, Calendar implements Serializable so you may give consideration to serialization. – Adam Zalcman Jan 02 '12 at 11:10
3

To keep your flattened object human readable and hand editable consider encoding your object into a JSON string using one of the popular JSON libraries. Same JSON library will also provide you APIs to decode a JSON string into your object.

One of the popular JSON library is Gson. Here's an use example: Converting JSON to Java

Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
2

You might be looking for the toString method:

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

In your case you would be doing something of the sort (to be added in each of your classes):

@Override
public String toString()
{
  return "Name = " + name + ...
}

The string can be of any format you wish. To save the object, all that you need to do is to write the text that the toString method returns to a file.

To read them back, however, you will have to implement your own logic. On the other hand, what you can do, is to use something such as XStream (instructions here) which will automatically convert your object to XML.

XML is human readable so that your users can modify whatever they need. Once this is done, you can re-use XStream to read back your object.

npinti
  • 51,780
  • 5
  • 72
  • 96
1

Try this

Employee em = new Employee;

//Your code

str obj= JavaScriptSerializer.Serialize();

// whenever you want to get object again

Employee emp = (Employee)JavaScriptSerializer.Deserialize();
Mujassir Nasir
  • 1,640
  • 4
  • 31
  • 52