Menu

Tree [ce9d55] default tip /
 History

Read Only access


File Date Author Commit
 .settings 2022-06-21 oddwarg oddwarg [ce9d55] Set project encoding to utf-8
 src 2021-10-25 oddwarg oddwarg [5c98a9] Added a method to copy fields from an object in...
 .classpath 2015-03-28 Oddwarg Oddwarg [9355f9] Initial import.
 .hgignore 2016-02-08 Oddwarg Oddwarg [e71b46] It is now possible to change the key-value sepa...
 .project 2015-03-28 Oddwarg Oddwarg [9355f9] Initial import.
 LICENSE.txt 2016-04-05 Oddwarg Oddwarg [c636d0] License update
 README.md 2017-02-28 Oddwarg Oddwarg [ca59c2] ...
 shallowSerializer.jar 2021-10-25 oddwarg oddwarg [5c98a9] Added a method to copy fields from an object in...
 shallowSerializer.png 2018-02-10 Oddwarg Oddwarg [9541b9] Added logo image

Read Me

ShallowSerializer is a very simple and compact Java library for reading an writing
human readable/human writable configuration files. It focuses on simplicity in
operation, formatting and usage.

ShallowSerializer has no dependencies.
It requires Java 1.6 or greater.
An up-to-date compiled jar is included in the repository.
It is licensed under the wtfpl.

ShallowSerializer takes a plain old java object as input and generates a text file
with key = value pairs for each non-transient, non-static field. It is important to
understand that the serializer generally cannot write nested objects! If a human
readable/human writable format with this feature is required, please use JSON
instead.

For example, an instance of the following class:

#!java
class Pojo
{
    int a = 0;
    int b = 0;
}

would result in the following text file:

a = 0
b = 0

To use ShallowSerializer, an instance of the serializer must be created:

#!java
ShallowSerializer<Pojo> s = new ShallowSerializer(Pojo.class);

To write an object to a text file:

#!java
Pojo p1 = new Pojo();
p1.a = 34;
p1.b = 42;
s.write("pojo.txt", p1);

To read an object from a text file:

#!java
Pojo p2 = s.read("pojo.txt");

If a field is missing from the text file, or the text file cannot be read at all,
then the read() method will simply leave the default values in the fields of the
returned object.

Any lines that do not contain the equals symbol are ignored by the parser. The
serializer is able to output comments if the Comment annotation is attached to a
field:

#!java
class CommentExample
{
    @Comment("This is the 'a' parameter.")
    int a = 0;
}

ShallowSerializer provides serializers and parsers for the following basic types:

#!java
byte/Byte, short/Short, int/Integer, long/Long, 
float/Float, double/Double, 
char/Character, 
String, Enum

If you need to serialize an object field of some non-basic type, then you have two options.

The first option is to override the toString() method for serialization, and provide
a constructor with a single String argument for parsing. For example:

#!java
public class IntProperty {
    int value;
    public IntProperty(String str) throws IllegalArgumentException {
        value = Integer.parseInt(str);
    }
    @Override
    public String toString() {
        return String.valueOf(value);
    }
}

This has the limitation that null values generally cannot be used, and it requires
that you, the developer, are able to edit the target class.

The second option is to write a separate parser and a single-line serializer for that
type. For example, to facilitate serialization of the java.awt.Rectangle class,
the following code may be used:

#!java
ShallowSerializer.registerInternalSerializer(Rectangle.class, new ToString() {
    public String toString(Rectangle r) {
        return ""+r.x+','+r.y+','+r.width+','+r.height;
    }
});

TypeParser.registerParser(Rectangle.class, new Parser() {
    GenericArrayParser iArrayParser = new GenericArrayParser(Integer.class,
            TypeParser.intParser, 4);
    public Rectangle parse(Class type, String str, Rectangle def) throws 
            IllegalArgumentException {
        Integer[] a = iArrayParser.parse(Integer[].class, str, null);
        if(a == null)
            return null;
        return new Rectangle(a[0],a[1],a[2],a[3]);
    }
});

This functionality is provided for generality, but if you find yourself requiring
numerous custom serializers or parsers, then this library is not for you. It was
designed to write simple, flat configuration files.

The output is similar to ".properties" files. However, ShallowSerializer provides
type safety and allows you to accomplish more with less code, without any
explicit casting or parsing of individual values.

ShallowSerializer files are easy to read and easy to write, unlike other formats
such as XML. However, ShallowSerializer is not a complete serializer, as it
cannot handle nested objects without code written on a per-type basis. If a
complete serializer is required, then there are excellent alternative libraries, such
as Kryo.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.