mangstadt / biweekly

biweekly is an iCalendar library written in Java.
BSD 2-Clause "Simplified" License
323 stars 44 forks source link

Recurrence not Serializable #86

Closed Poeschl closed 6 years ago

Poeschl commented 6 years ago

Is there a specific reason to keep the Recurrence class not serializable? In my Project sometimes just the Recurrence is stored in a database. For that hibernate needs the serialization.

If there is no specific reason, I offer you to make a PR to change this. :wink:

I saw that a few classes already get it on #75, but are mixed with test fixes. Those classes might become changes as well if necessary.

mangstadt commented 6 years ago

Thanks for posting @Poeschl. Maybe if enough people ask for it, but I don't want to clutter the code base. If I make Recurrence serializable, then why not make every class serializable? If you can, you should avoid using Java serialization anyway because it's full of security vulnerabilities.

If you need to serialize a Recurrence object, a work-around is to generate an RRULE string, and then parse the string. It's kind of a lot of code, but it should round-trip.

Recurrence sourceObject = new Recurrence.Builder(Frequency.WEEKLY).interval(2).build();

String stringValue;
{
  RecurrenceRule rrule = new RecurrenceRule(recur);
  WriteContext context = new WriteContext(ICalVersion.V2_0, new TimezoneInfo(), null);
  RecurrenceRuleScribe scribe = new RecurrenceRuleScribe();
  stringValue = scribe.writeText(rrule, context); //"FREQ=WEEKLY;INTERVAL=2"
}

Recurrence parsedObject;
{
  RecurrenceRuleScribe scribe = new RecurrenceRuleScribe();
  ParseContext context = new ParseContext();
  context.setVersion(ICalVersion.V2_0);
  RecurrenceRule rrule = scribe.parseText(stringValue, null, new ICalParameters(), context);
  parsedObject = rrule.getValue();
}

assertEquals(sourceObject, parsedObject);

Hope this helps.

HansBrende commented 6 years ago

From my perspective, this whole library, in essence, is already a serialization/deserialization tool. Using Java serialization rather than the standardized ical serialization is ill-advised, as it is guaranteed to be less space/time efficient, and simply muddles the purpose of this library. Also it means that your database would only be interoperable with Java, rather than any language which supports parsing ical properties.

+1 for property scribes -1 for Serializable.

mangstadt commented 6 years ago

@HansBrende Good point!

Poeschl commented 6 years ago

I totally agree that using the serialization feature of Java is bad and its way better to use the standardized ical serialization. I didn't intent to use the java serialization feature to write it in the database. I was thinking that a hibernate entity has to be Serializable to work with the hibernate environment. But I proved me wrong. Only the id type of a entity need to be serializeable, not a whole entity.

So I'm closing this issue

mangstadt commented 6 years ago

@Poeschl Oh nice, glad you figured it out! 😸