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

Support for RangeSet missing #90

Open
mittalnanu opened this issue Oct 18, 2016 · 7 comments
Open

Support for RangeSet missing #90

mittalnanu opened this issue Oct 18, 2016 · 7 comments

Comments

@mittalnanu
Copy link

Are there any alternatives, I don't see any utility in the current module?

@cowtowncoder
Copy link
Member

@mittalnanu um, what?

@mittalnanu
Copy link
Author

mittalnanu commented Oct 20, 2016

Sorry, should have been more elaborate.
Let me try to explain using code

//Code 1
ObjectMapper mapper = new ObjectMapper();
 mapper.registerModule(new GuavaModule());
 System.out.println(mapper.writeValueAsString(Range.open(1,23)));

//Output 1
{"lowerEndpoint":1,"lowerBoundType":"OPEN","upperEndpoint":23,"upperBoundType":"OPEN"}

//Code 2

RangeSet<Integer> rangeSet= TreeRangeSet.create();
rangeSet.add(Range.open(1,5));
rangeSet.add(Range.openClosed(7, 1000));
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new GuavaModule());
System.out.println(mapper.writeValueAsString(rangeSet));

//Output 2
{"empty":false}

My question is RangeSet, doesn't seem to be supported out of the box. Is there something I am missing or need to write own Jackson Serializer / Deserializer ?

@cowtowncoder
Copy link
Member

Module supports a subset of Guava datatypes, based on what users have needed, and contributed handlers for. Due to sheer size of Guava (very much monolithic collection of tons of things), coverage is not complete.

I do know that Range is supported, but I don't think RangeSet is.

@cowtowncoder cowtowncoder reopened this Oct 24, 2016
@cowtowncoder cowtowncoder changed the title Jackson Processor for RangeSet Support for RangeSet missing Oct 24, 2016
@mittalnanu
Copy link
Author

mittalnanu commented Oct 25, 2016

Thanks for the revert @cowtowncoder
I found an alternative way to do it, is that how it should be done or we need to have explicit support for Rangeset?

//Input
RangeSet rangeSet= TreeRangeSet.create(); rangeSet.add(Range.open(1,5)); rangeSet.add(Range.openClosed(7, 1000)); ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new GuavaModule()); System.out.println(mapper.writerFor(new TypeReference<Set>() {}).writeValueAsString(rangeSet.asRanges()));

//Output
[{"lowerEndpoint":1,"lowerBoundType":"OPEN","upperEndpoint":5,"upperBoundType":"OPEN"},{"lowerEndpoint":7,"lowerBoundType":"OPEN","upperEndpoint":1000,"upperBoundType":"CLOSED"}]

@cowtowncoder
Copy link
Member

@mittalnanu RangeSet is a type that does not seem to implement any of standard JDK collection types (java.util.Collection or java.util.Set), so the only way Jackson can handle it would be as regular POJO via getters and setters. And since it doesn't seem to define getters/setters, that's unlikely to work. This is why specific serializer and/or deserializer would be needed.

If RangeSet did actually implement Set<Range>, it would be handled automatically without any extra work.

@mittalnanu
Copy link
Author

Yes so , rangeSet.asRanges() returns a Set and it works automatically. So I think that is a neat workaround. Do you see any scenario where a specific serializer is needed given I can get Set from RangeSet ?

@cowtowncoder
Copy link
Member

@mittalnanu Right if and when you can get a regular Set, that works nice. Or you could even use mix-in annotations to attach @JsonValue to method asRanges() -- that would add automatic support for serialization.

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