Skip to content

Cannot serialize org.joda.time.Days #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bestbugwriter opened this issue Mar 28, 2025 · 6 comments · Fixed by #161
Closed

Cannot serialize org.joda.time.Days #160

bestbugwriter opened this issue Mar 28, 2025 · 6 comments · Fixed by #161
Milestone

Comments

@bestbugwriter
Copy link

bestbugwriter commented Mar 28, 2025

I found jackson-datatype-joda is also has a serializer com.fasterxml.jackson.datatype.joda.deser.PeriodDeserializer,and Days does implements the ReadablePeriod interface.
but I don't known why it is not work.

https://stackoverflow.com/questions/79535991/joda-date-time-type-org-joda-time-days-not-supported-by-default?noredirect=1#comment140269178_79535991

I used joda 2.10.14 time and jackson 2.18.3,

jackson-core-2.18.3.jar
jackson-databind-2.18.3.jar
jackson-datatype-joda-2.18.3.jar
joda-time-2.10.14.jar

I have register JodaModule, but it also throw Exception, my code is

public static void main(String[] args) throws JsonProcessingException {
    DateTime dateTime = new DateTime();
    Days days = Days.days(1);
    Map<String, Object> map = new HashMap<>();
    map.put("dateTime", dateTime);
    map.put("days", days);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JodaModule());
    String json = objectMapper.writeValueAsString(map);
    System.out.println(json);
}

the exception

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Joda date/time type `org.joda.time.Days` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-joda" to enable handling (through reference chain: java.util.HashMap["days"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1340)
at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:808)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeWithoutTypeInfo(MapSerializer.java:764)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:720)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:35)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:502)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:341)
at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4819)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:4060)
at azkaban.utils.JSONUtils.main(JSONUtils.java:96)

I checked JodaModule code, found that it is not add Days deserializer, but has PeriodDeserializer. So, how should I resolve this issue?

Image
@cowtowncoder
Copy link
Member

There is some confusion here: what you are trying to do is Serialize (write as json) not Deserialize. I will fix the title.

But aside from that, yes, does look like some handling may be missing.
Thank you for reporting the issue.

@cowtowncoder
Copy link
Member

cc @JooHyukKim -- date/time issue if you are interested :)

@cowtowncoder cowtowncoder changed the title com.fasterxml.jackson.datatype.joda.deser.PeriodDeserializer Not work Cannot serialize org.joda.time.Days Mar 29, 2025
cowtowncoder pushed a commit that referenced this issue Mar 29, 2025
cowtowncoder added a commit that referenced this issue Mar 29, 2025
@bestbugwriter
Copy link
Author

@cowtowncoder Sorry, I just checked your reply. I found that some other classes similar to Days, such as Hours and Years, also don't work. Do I need to write a serialization and deserialization class for each of them separately?

this is my code:

    public static void main(String[] args) throws IOException {

        Map<String, Object> map = new HashMap<>();
        map.put("dateTime",  new DateTime());
        map.put("years", Years.years(1));
        map.put("months", Months.months(2));
        map.put("weeks", Weeks.weeks(3));
        map.put("days", Days.days(4));
        map.put("hours", Hours.hours(5));
        map.put("minutes", Minutes.minutes(6));
        map.put("seconds", Seconds.seconds(7));


        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JodaModule());

        String json = objectMapper.writeValueAsString(map);
        System.out.println(json);

        String json1 = "{\"dateTime\":1743059899339,\"hours\":\"5h\",\"seconds\":\"7s\",\"months\":\"2M\",\"weeks\":\"3w\",\"minutes\":\"6m\",\"days\":\"4d\",\"years\":\"1y\"}";
        System.out.println(objectMapper.readValue(json1, Map.class));
    }

and this is my exception:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Joda date/time type `org.joda.time.Hours` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-joda" to enable handling (through reference chain: java.util.HashMap["hours"])
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
	at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1340)
	at com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer.serialize(UnsupportedTypeSerializer.java:35)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:808)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeWithoutTypeInfo(MapSerializer.java:764)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:720)
	at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:35)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:502)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:341)
	at com.fasterxml.jackson.databind.ObjectMapper._writeValueAndClose(ObjectMapper.java:4819)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:4060)
	at azkaban.utils.JSONUtils.main(JSONUtils.java:353)

@JooHyukKim
Copy link
Member

@bestbugwriter Could you open another issue regarding those?
Implementation should be simliar to #161, best if you could implement serializer for the missing ones as well, but I will try to do it when I find time.

@bestbugwriter
Copy link
Author

bestbugwriter commented Mar 31, 2025

@bestbugwriter您能针对这些问题再开一个问题吗?实现方式应该与#161 类似,最好能为缺失的那些也实现序列化器,不过我会在有时间的时候尝试这样做。

ok #162

@cowtowncoder
Copy link
Member

@bestbugwriter Like @JooHyukKim said, we do need issues to add support for types with missing support. Looks like you already did that, thanks!

@cowtowncoder cowtowncoder added this to the 2.18.4 milestone Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants