Embedded adapters

Odin contains some adapters to simplify the use of most used Java objects like dates and UUID. They write the object as a String instead of writing Object fields like any other object.

Dates

All of the java date objects use an inline adapter to serialize the date as a formatted String.

You can change the date format by giving Odin the new format.



To change Date you have to give your new DateFormat.

odin.setDefaultDateFormat(new SimpleDateFormat("yyyy-MM-dd"));

The default format for Date is "yyyy-MM-dd HH:mm:ss".


The type alias for Date is <date>.



To change the format of new java time objects you need to add the class of the desired date time object:

odin.setDefaultDateFormat(LocalDate.class, new DateTimeFormatterBuilder().appendPattern("yyyy_MM_dd").toFormatter());

Every possible objects are:

  • LocalDate


    The default format is: "yyyy-MM-dd".


    Type alias: <local date>.



  • LocalTime


    The default format is: "HH:mm:ss[.nnnnnnnnn]", the nano seconds are optional and added only if the value is greater than 0.


    Type alias: <local time>.



  • LocalDateTime


    The default format is: "yyyy-MM-dd HH:mm:ss[.nnnnnnnnn]", the nano seconds are optional and added only if the value is greater than 0.


    Type alias: <local datetime>.



  • ZonedDateTime


    The default format is: "yyyy-MM-dd HH:mm:ss[.nnnnnnnnn] [zone]", the nano seconds are optional and added only if the value is greater than 0.

    The zone can be a time-zone id name like 'Europe/Paris', or an offset like 'UTC+01:00'.


    Type alias: <zoned datetime>.

Collections

Odin contains custom adapters that build collection using the add or put method. These adapters are registered by default for the main implementations.


If you need to add an implementation to these adapters, you can use the register method associated to the type of the collection you want to add.

Any collections that is not registered with these adapters use the default object adapter, and will be serialized using its fields.


For example, if you want a custom list to use the default list adapter, you can register your custom list type as a list:

odin.registerAsList("custom list", CustomList.class);
<custom list> {
    elements = [
       "foo 1"
       "foo 2"
       null
       null
    ]
    size = 2
}
<custom list> {
    "foo 1"
    "foo 2"
}
List

The default List adapters are:

  • ArrayList


    Type alias: <array list>.

  • LinkedList


    Type alias: <linked list>.

  • Vector


    Type alias: <vector>.

Set

The syntax between list and set are the same, so if no type definition is present, a list can be read as a set and the other way around.


The default Set adapters are:

  • HashSet


    Type alias: <hash set>.

  • LinkedHashSet


    Type alias: <linked hash set>.

  • TreeSet


    Type alias: <tree set>.

  • EnumSet


    Type alias: <enum set>.

Map

Map uses row to save entries, so any kind of key and value can be used.

<hash map> {
    {
        x = 5
        y = 2
    } : {
        name = "foo"
    }
}

The default Map adapters are:

  • HashMap


    Type alias: <hash map>.

  • LinkedHashMap


    Type alias: <linked hash map>.

  • TreeMap


    Type alias: <tree map>.

  • EnumMap


    Type alias: <enum map>.

Locale

Locale are serialized as String with the language tag.

<locale> "en-US"

UUID

UUID also be serialized to String.

<uuid> "b5899ed4-8a9b-4d2e-9a10-4dd7514bda2d"

Class

Class are serialized as String that contains the getName() of the class.


Class is read using Class.forName().

<class> "com.newpixelcoffee.Odin"