Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Unable to Serialize RangeSet using asRanges() method after upgrading to jackson 2.8 #93

Open
nisarakash opened this issue Aug 21, 2017 · 5 comments

Comments

@nisarakash
Copy link

Hi,
I am trying to serialize RangeSet using jackson 2.8 libraries. I am getting the following exception:

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.time.LocalDateTime and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.akash.test.Range.RangeInterval["rangeSet"]->com.google.common.collect.TreeRangeSet$AsRanges[0])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:284)
at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1110)
at com.fasterxml.jackson.databind.SerializerProvider.reportMappingProblem(SerializerProvider.java:1135)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
at com.fasterxml.jackson.datatype.guava.ser.RangeSerializer._writeContents(RangeSerializer.java:133)
at com.fasterxml.jackson.datatype.guava.ser.RangeSerializer.serialize(RangeSerializer.java:90)
at com.fasterxml.jackson.datatype.guava.ser.RangeSerializer.serialize(RangeSerializer.java:20)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContentsUsing(CollectionSerializer.java:176)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:120)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:112)
at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:25)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:704)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3697)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3073)
at com.akash.test.Range.RangeInterval.main(RangeInterval.java:38)

As per my understanding, it is trying to access the first element (i.e. TreeRangeSet$AsRanges[0]) of the set using index which I guess is not a valid option in the set.

It works great with jackson 2.6 libraries. Can anyone help me out?

@nisarakash nisarakash changed the title Unable to Serialize RangeSet after upgrading to jackson 2.8 Unable to Serialize RangeSet using asRanges() method after upgrading to jackson 2.8 Aug 21, 2017
@cowtowncoder
Copy link
Member

Exception message indicates that you have not added Java 8 date/time module (jackson-datatype-jsr310) -- this module is necessary for serializing java.time.LocalDateTime. If so, you need to add it.

If this is not enough, I would need a test case to reproduce the behavior.

Also: now that 2.9.0 is out, it would make sense to test with that; or, if using 2.8, make sure to use the latest patch version (2.8.9).

@nisarakash
Copy link
Author

nisarakash commented Aug 21, 2017

Hi cowtowncoder,
I had use jackson-datatype-jsr310 - 2.8 in my test. I can serialize the Range, but it fails when I try to use TreeRangeSet. It also fails when I tried TreeRangeSet.
Below is the code I am using to serialize the RangeSet:

public class RangeInterval {
	private RangeSet<LocalDateTime> rangeSet;
	public RangeInterval() {
		rangeSet = TreeRangeSet.create();
		rangeSet.add(Range.closed(LocalDateTime.now().minusDays(5), LocalDateTime.now()));
		rangeSet.add(Range.closed(LocalDateTime.now().minusDays(10), LocalDateTime.now().minusDays(8)));
	}
	
	public Set<Range<LocalDateTime>> getRangeSet() {
		return rangeSet.asRanges();
	}
	public static void main(String[] args) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.registerModule(new GuavaModule());
		mapper.registerModule(new JavaTimeModule());
		System.out.println(mapper.writeValueAsString(new RangeInterval()));		
	}	
}

@cowtowncoder
Copy link
Member

And just to make sure: are you using 2.8.9, and not some earlier patch version?

@nisarakash
Copy link
Author

I am using
jackson-core 2.8
jackson-databind 2.8
jackson-annotaions 2.8
jackson-datatype-guava 2.6
jackson-datatype-jsr310 2.6

Can this create dependency issues? If yes can you explain me?

@cowtowncoder
Copy link
Member

@nisarakash Yes, it could cause issues: minor versions of components should match. Often some combinations of "close enough" minor versions do work, and I can't say for sure this is what is causing problems, but I would try upgrading all components to 2.8.9.

Note, too, that Jackson releases always use patch version, so while I sometimes refer to 2.8.x to mean all patch versions, there is no Maven version 2.8. Since there are many important fixes throughout minor versions, I would strongly recommend trying upgrade to 2.8.9, beyond going from 2.6.x to 2.8.x.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants