Saving Java Objects in Files

I have the following class in Java. I would like to be able to save it in a common file format that could be moved across different computers. I know about serializing objects, but I was wondering what other options I have and what their pros and cons are. Thank you For example, a serialized file is not human readable and therefore 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; } } 
+4
source share
5 answers

For Json use GSON ...

It supports arbitrarily complex objects, and you don't need setters or getters. Mr. just sets 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) 
+5
source

You can serialize objects in JSON using, for example, Jackson , which will significantly improve their readability.

+8
source

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

An example of one generated XML:

 <?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> 
+4
source

JAXB sorts and sorts objects by annotation. Annotations allow you to:

  • Manage serialization format.
  • Isolate the serialization format from some refactoring of your code.
  • Define fields to exclude from serialization.

Here's the Oracle JAXB tutorial .

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; ... } 
+1
source

Check out xstream , a simple and frequently used Java XML serialization library. In a nutshell, it looks like this (example 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 content is as follows:

 <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 make many settings for more complex situations.

+1
source

Source: https://habr.com/ru/post/1485728/


All Articles