I wanted to implement some simple persistence support for my objects that probably contain at most 5-7 fields and the total number of objects would be at most 20-30. I didn’t want to put database and ORM in place for such a small task.

So I thought I’m going to develop something like:

#persons.properties
name = John
lastName = Smith
age = 28

Where I can:

PropertiesX properties = new PropertiesX().load("persons.properties");
Person person = properties.mapTo(Person.class);

My Person class:

@PropertyObject
public class Person {
	private String name;
	private String lastName;
	private int age;
	
	@PropertyMethod
	public void setName(@Property(key="name") String name) {
		this.name = name;
	}
	......
}

Or if I want to get all objects I would do something like:

#persons.properties

# all person's ids
persons = 0, 1, 2

person.0.name = James
person.0.lastName = Smith
person.0.age = 28

person.1.name = John
person.1.lastName = Smith
person.1.age = 23

person.2.name = Joe
person.2.lastName = Doe
person.2.age = 34

And java code would look like

// where second argument is the property prefix and the third is the array of person ids from the property file.
Person[] persons = properties.mapToArray(Person.class, "person", "persons");

//or I could do something like the following where ids is arbitrary list of ids :
Person[] persons = properties.mapToArray(Person.class, "person", ids);

Then I realized that property file with 30 objects might look a kind of ugly and googled for “Java JSON”.
And then I lost fun I was going to have for today. I found this – a simple library to serialize objects to Xml or JSON and back again.

Advertisements