0

I have the list of instances of a class which kind of complicated: it have many val's including nested classes those also have many val's. I want to write in a file in either json or xml format.

I found a simple example which seems to do what I want (in Java, though, but it really doesn't matter):

#Main.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class Main {

    public static void main(String args[]) {
        try {
            Employee employee = new Employee("0001", "Robert", "Newyork City", "IT");

            FileOutputStream fos = new FileOutputStream("my-data.txt");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(employee);

            oos.flush();
            oos.close();
            System.out.println("Employee Object written to file employee.dat");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

#Employee.java
import java.io.Serializable;


public class Employee implements Serializable {
    private String empId;
    private String Name;
    private String Address;
    private String Dept;

    public Employee(String empId, String name, String address, String dept) {
        this.empId = empId;
        this.Name = name;
        this.Address = address;
        this.Dept = dept;
    }

    public String toString() {
        return "[Employee: " + empId + ", " + ", " + Name + ", " + Address + ", " + Dept + "]";
    }
}

And what I had as an output was a binary data written in a file. Moreover, toString() methods enumerates all fields manually, which is unacceptable for my real class.

So how do I do that? I'd really appreciate if you give even a small example of it, instead of saying "there is a library called X, use it". I've already failed using all these libraries in my previous question about serialization. I'm not sure yet if what I need is serialization, because I need it to be stored in not binary, but in xml or json format.

Alan Coromano
  • 24,958
  • 53
  • 135
  • 205
  • Add an example of how the result text file should look like – 4lex1v Jul 10 '13 at 07:36
  • @om-nom-nom as i understood he doesn't want to serialize it, but to write in human readable format in file, but not in binary – 4lex1v Jul 10 '13 at 07:37
  • @AlexIv xml and json are relatively human readable, that questions I've linked above are not about *binary* serialization, but about serializing to json/xml – om-nom-nom Jul 10 '13 at 07:39

2 Answers2

2

Example using Jackson:

final ObjectMapper mapper = new ObjectMapper();
final String s = mapper.writeValueAsString(yourEmployee);

s contains the JSON as a string.

You can then read back that value and convert back to a POJO as well. Javadoc for ObjectMapper.

As @veritas mentions in the comment, you can extend Jackson, customize {de,}serialization etc -- heck, you can even have it brew coffee. But defaults are pretty good already, so you may not even need to delve into its depths.

fge
  • 119,121
  • 33
  • 254
  • 329
  • Jackson api can be extended to achieve any mapping between java objects and json. also see the online tutorial http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/ – veritas Jul 10 '13 at 07:31
0

I suggest you'll use a library that knows how to serialize a POJO object into XML or JSon. Take a look at JAXB or XStream. Jackson is also an option.

Elad Tabak
  • 2,317
  • 4
  • 23
  • 33