4

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;
    }
}
1

5 Answers 5

8

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

Sign up to request clarification or add additional context in comments.

Comments

5

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)

2 Comments

Thanks so much! But now that it's a string should I save it into a text file or something?
You can do whatever you want, serialize to file as json, it will be portable and readable, in that form.
4

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>

Comments

1

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;

    ...
}

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.