What's the recommended way to serialize dependent objects, especially when objects are being freshly constructed (to avoid malicious byte streams, or whatever)? For example....

After creating a (genericized, but setup through Spring for my company) fiscal calendar based on JodaTime, I'm creating the next version based on JSR310 (using the back-port for development) in preparation for Java 8.

Currently, I'm working through serialization; how am I supposed to serialize dependent objects? Most of the existing stuff has a 'single' chronology, but I don't get to rely on that (for instance, the fiscal year can start on any day during the ISO year, which means a new FiscalChronology). Something like this:

public class FiscalDate implements Serializable {
    private final long epochDays;
    private final FiscalChronology chronology;

    // Many of the other 'date' instances also have a reference to their 'era':
    private final transient FiscalEra era;

public class FiscalChronology implements Serializable {
    // These two control year/month patterns.
    private final YearPattern yearPattern;
    private final MonthPattern monthPattern;
    // This should be derived during deserialization, for safety reasons.
    private transient final FiscalEra[] eras;

    private Object readResolve() {
        return FiscalChronology.create(yearPattern, monthPattern);

What do I do for the 'derived' era?

public class FiscalEra implements Serializable {
    // So that eras can be singletons, per JSR310 documentation/recommendations.
    private static final ConcurrentHashMap<FiscalChronology, FiscalEra[]>;

    private final int eraValue;
    private final FiscalChronology chronology;

    public static FiscalEra of(FiscalChronology chronology, int era) {
        return putIfAbsent(chronology, makeEras(chronology))[era];

Should I just remove serialization? Only serialize eraValue (and rely on the serialization graph including the Chronology on its own)?


Related to this, what am I supposed to do for the output of toString() - should it just be the name of the era (which I'm linking to the ISO eras), and expect people to include chronology information if it's important, or always include information about the chronology?

1 Answer 1


Maybe you can use XStream:

From their website it supports:

Full object graph support. Duplicate references encountered in the
object-model will be maintained. Supports circular references.

  • Due to the way serialization already works, I'm not concerned about the 'circular' reference. It was more along the lines of, 'what should I store?' May 29, 2013 at 15:54
  • @Clockwork-Muse if your code works you should put it on CodeReview instead. I think that's what you need
    – xybrek
    May 30, 2013 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.