ical4j / ical4j-serializer

Marshalling iCalendar and vCard to XML and JSON formats
http://www.ical4j.org/serializer/
BSD 3-Clause "New" or "Revised" License
2 stars 2 forks source link

Can't get jCal deserialization to work from example #5

Closed rfc2822 closed 3 years ago

rfc2822 commented 3 years ago

Thanks for your jCal implementation! I'm currently playing around to get it into DAVx⁵ (so that it can sync using jCal instead of iCalendar and hopefully avoid some parsing problems).

However, I can't get jCal deserialization to work with the code from ical4j-json's README:

String json = ...;

SimpleModule module = new SimpleModule();
module.addDeserializer(Calendar.class, new JCalMapper())
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(module);

Calendar calendar = mapper.readValue(json, Calendar.class);

I have used:

ical4j 3.1.1 ical4-json 0.1.3

@Test
fun testJCal() {
    val module = SimpleModule()
    module.addDeserializer(Calendar::class.java, JCalMapper(Object::class.java))
    // A class needs to be specified here                   ^^^^^^  (but the example doesn't mention which one; seems that it doesn't matter for the other problem)

    val mapper = ObjectMapper()
    mapper.registerModule(module)

    // example from RFC 7265 B.1.2
    val jsonString = "[\"vcalendar\",\n" +
            " [\n" +
            "   [\"calscale\", {}, \"text\", \"GREGORIAN\"],\n" +
            "   [\"prodid\", {}, \"text\", \"-//Example Inc.//Example Calendar//EN\"],\n" +
            "   [\"version\", {}, \"text\", \"2.0\"]\n" +
            " ],\n" +
            " [\n" +
            "   [\"vevent\",\n" +
            "     [\n" +
            "       [\"dtstamp\", {}, \"date-time\", \"2008-02-05T19:12:24Z\"],\n" +
            "       [\"dtstart\", {}, \"date\", \"2008-10-06\"],\n" +
            "       [\"summary\", {}, \"text\", \"Planning meeting\"],\n" +
            "       [\"uid\", {}, \"text\", \"4088E990AD89CB3DBB484909\"]\n" +
            "     ],\n" +
            "     []\n" +
            "   ]\n" +
            " ]\n" +
            "]\n"
    val calendar = mapper.readValue(jsonString, Calendar::class.java)
}

That throws at runtime:

'net.fortuna.ical4j.model.PropertyBuilder net.fortuna.ical4j.model.PropertyBuilder.factories(java.util.List)'
java.lang.NoSuchMethodError: 'net.fortuna.ical4j.model.PropertyBuilder net.fortuna.ical4j.model.PropertyBuilder.factories(java.util.List)'
    at org.mnode.ical4j.json.JCalMapper.parseProperty(JCalMapper.java:81)
    at org.mnode.ical4j.json.JCalMapper.deserialize(JCalMapper.java:45)
    at org.mnode.ical4j.json.JCalMapper.deserialize(JCalMapper.java:19)
    at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4593)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3548)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3516)
    at at.bitfire.ical4android.Ical4jTest.testJCal(Ical4jTest.kt:139)
…

When I look at PropertyBuilder, factories is a property, but not a method. Maybe this is the problem?

So, to summarize, there are three problems/questions:

  1. Is ical4j-json intended to work with iCal4j 3.x?
  2. Which class should be passed to JCalMapper(...)?
  3. How can the PropertyBuilder exception be avoided?
benfortuna commented 3 years ago

Apologies I removed the PropertyBuilder.factories() method a couple of months ago, where I really should have deprecated it.

I'll fix in both libraries shortly.

benfortuna commented 3 years ago

ical4j-json 0.1.4 just released with support for ical4j 3.1.1.

NOTE: for the JCalMapper constructor just pass null for now, I will probably update this in future, just need to identify the purpose of it in the jackson framework.