Skip to content

Cannot serialize org.joda.time.Hours/Minutes/Seconds/Months/Years/Weeks #162

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 31, 2025 · 0 comments
Closed

Comments

@bestbugwriter
Copy link

@cowtowncoder @JooHyukKim

open another issue regarding this problem

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)

please help me fix this issue like this:
issues-160
pr-161

@cowtowncoder cowtowncoder changed the title Cannot serialize org.joda.time.Hours\Minutes\Seconds\Months\Years\Weeks Cannot serialize org.joda.time.Hours/Minutes/Seconds/Months/Years/Weeks 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

No branches or pull requests

1 participant