letter Q
letter Q

Reputation: 15405

Saving Java objects into files

I have the following class in Java. I would like to be able to save it into a common file format that would be able to moved across different pc's. I know about object serialization but I was wondering what are my other options and what are their respective pros and cons. Thanks! Like for example a serialized file is not human readable and therefore a con.

public class NervousSystem {
    private CentralNervousSystem CNS;
    private PeripheralNervousSystem PNS;

    public NervousSystem(Neocortex neocortex, LateralGeniculateNucleus LGN, Retina retina) {
        this.CNS = new CentralNervousSystem(neocortex, LGN);
        this.PNS = new PeripheralNervousSystem(retina);
    }

    public CentralNervousSystem getCNS() {
        return this.CNS;
    }

    public PeripheralNervousSystem getPNS() {
        return this.PNS;
    }
}

Upvotes: 4

Views: 2717

Answers (5)

raffian
raffian

Reputation: 32106

For Json, use GSON...

It supports arbitrarily complex objects and you don't need setters or getters. Gson just figures it all out.

Convert to JSON

Gson gson = new Gson();
String myObjectJson = gson.toJson( myObj);
println myObjectJson 

Convert from JSON

MyObj obj = gson.fromJson(myObjectJson, MyObj.class)

Upvotes: 5

Adriaan Koster
Adriaan Koster

Reputation: 16209

Have a look at xstream, a simple and commonly used Java XML serialization library. In a nutshell it looks like this (example nicked from the xstream website):

public class Person {
  private String firstname;
  private String lastname;
  private PhoneNumber phone;
  private PhoneNumber fax;
  // getters & setters
}

public class PhoneNumber {
  private int code;
  private String number;
  // getters & setters
}

XStream xstream = new XStream();
Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));
String xml = xstream.toXML(joe);

The XML contents look like this:

<mypackage.Person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</mypackage.Person>

This is the simplest example, you can do a lot of customizations for more complex situations.

Upvotes: 1

Andy Thomas
Andy Thomas

Reputation: 86509

JAXB marshalls and unmarshalls objects according to annotations. The annotations allow you to:

  • Control the serialization format.
  • Insulate your serialization format from some refactorings of your code.
  • Identify fields to be excluded from serialization.

Here's an Oracle tutorial on JAXB.

For example, an annotated class might look like this:

@XmlRootElement(name="foo")
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

    @XmlTransient
    private String m_temp; 

    @XmlAttribute(name="fieldA")
    private String fieldA;

    ...
}

Upvotes: 1

fmodos
fmodos

Reputation: 4568

You can generate a XML of your javabean using the java.beans.XMLEncoder. Check one tutorial here.

Example of one xml generated:

<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_10" class="java.beans.XMLDecoder">
 <object class="com.test.MyBean">
  <void property="myBoolean">
   <boolean>true</boolean>
  </void>
  <void property="myString">
   <string>xml is cool</string>
  </void>
  <void property="myVector">
   <object class="java.util.Vector">
    <void method="add">
     <string>one</string>
    </void>
    <void method="add">
     <string>two</string>
    </void>
    <void method="add">
     <string>three</string>
    </void>
   </object>
  </void>
 </object>
</java>

Upvotes: 4

Zim-Zam O&#39;Pootertoot
Zim-Zam O&#39;Pootertoot

Reputation: 18158

You can serialize the objects to JSON using e.g. Jackson, which will greatly improve their human readability

Upvotes: 8

Related Questions