Manual serialization of objects in Java

I have my own INIFile class, which I wrote to read / write INI files containing fields under the header. I have several classes that I want to serialize using this class, but I'm a bit confused to do this best. I considered two possible approaches.

Method 1. Define an interface, such as ObjectPersistent, using two methods:

public interface ObjectPersistent { public void save(INIFile ini); public void load(INIFile ini); } 

Each class will be responsible for using the INIFile class to output all properties to a file.

Method 2: expose all the properties of classes that require serialization through getters / setters so that savings can be processed in one centralized place:

 public void savePlayer(Player p) { INIFile i = new INIFile(p.getName() + ".ini"); i.put("general", "name", p.getName()); i.put("stats", "str", p.getSTR()); // and so on } 

The best part of method 1 is that not all properties must be open, so encapsulation is preserved. What is wrong with method 1 is that saving is not technically what the player “did”. It also links me to flat files through the ini object passed to the method, so switching to a relational database later will be a huge pain.

The best part of method 2 is that all the I / O operations are centralized in one place and the actual saving process is completely hidden from you. This can be a save to a flat file or database. What's wrong with method 2 is that I have to fully expose the internal members of the classes so that the centralized serializer can get all the data from the class.

I want it to be as simple as possible. I prefer to do it manually without using a framework. I'm also definitely not interested in using the built-in serialization provided in Java. Is something missing here? Any suggestions on which template is best suited for this, I would appreciate it. Thanks.

+4
source share
3 answers

Since you do not want (for some reason) to use Java serialization, you can use XML serialization. The easiest way is XStream :

XStream is a simple library for serializing objects in XML and vice versa.

If you are really sure that you do not want to use any kind of serialization structure, you can, of course, use reflection. Important points:

  • getClass().getDeclaredFields() returns all the fields of the class - both public and private
  • field.setAccessible(true) - makes a private (or protected) field available through reflection
  • Modifier.isTransient(field.getModifiers()) indicates whether the field has been marked with the transient keyword, i.e. not eligible for serialization.
  • structures of nested objects can be represented, for example, by a dotted notation - team.coach.name .

All serialization libraries use reflection (or introspection ) to achieve their goals.

+4
source

I would choose method 1.

This may not be the most object-oriented way, but in my experience it is simpler, less error prone and easier to maintain than method 2. If you agree to provide multiple implementations for your own serialization, you can use the interfaces for the save and load methods.

 public interface ObjectSerializer { public void writeInt(String key, int value); ... } public interface ObjectPersistent { public void save(ObjectSerializer serializer); public void load(ObjectDeserializer deserializer); } 

You can enhance these ObjectSerializer / Deserializer interfaces to have enough methods and parameters to cover both flat files and databases.

+1
source

This task is for the visitor template.

0
source

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


All Articles