Skip to content
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

Add a test method for #118 #126

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.fasterxml.jackson.datatype.guava;

import java.io.IOException;
import java.time.Duration;
import java.time.LocalDate;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import com.fasterxml.jackson.core.type.TypeReference;
Expand Down Expand Up @@ -39,6 +42,23 @@ static class Wrapped {
public Wrapped() { }
public Wrapped(Range<Integer> r) { this.r = r; }
}

static class Stringified<T extends Comparable> {

@JsonFormat(shape = JsonFormat.Shape.STRING)
public Range<T> r;

public Stringified() {
}

public Stringified(Range<T> r) {
this.r = r;
}

public Range<T> toRange() {
return r;
}
}

/**
* This test is present so that we know if either Jackson's handling of Range
Expand Down Expand Up @@ -316,4 +336,43 @@ public void testRangeWithDefaultTyping() throws Exception
assertEquals(BoundType.CLOSED, result.lowerBoundType());
assertEquals(BoundType.CLOSED, result.upperBoundType());
}

public void testSerializationToString() throws Exception
{
// Test open range serialization
Range<Integer> openRange = RangeFactory.open(1, 10);
assertEquals(MAPPER.writeValueAsString(new Stringified<>(openRange)), "{\"r\":\"(1..10)\"}");

// Test closed range serialization
Range<Integer> closedRange = RangeFactory.closed(5, 15);
assertEquals(MAPPER.writeValueAsString(new Stringified<>(closedRange)), "{\"r\":\"[5..15]\"}");

// Test open-closed range serialization
Range<Integer> openClosedRange = RangeFactory.openClosed(10, 20);
assertEquals(MAPPER.writeValueAsString(new Stringified<>(openClosedRange)), "{\"r\":\"(10..20]\"}");

// Test closed-open range serialization
Range<Integer> closedOpenRange = RangeFactory.closedOpen(25, 30);
assertEquals(MAPPER.writeValueAsString(new Stringified<>(closedOpenRange)), "{\"r\":\"[25..30)\"}");

// Test single point range serialization
Range<Integer> singletonRange = RangeFactory.singleton(42);
assertEquals(MAPPER.writeValueAsString(new Stringified<>(singletonRange)), "{\"r\":\"[42..42]\"}");

// Test unbounded range serialization
Range<Integer> unboundedRange = RangeFactory.all();
assertEquals(MAPPER.writeValueAsString(new Stringified<>(unboundedRange)), "{\"r\":\"(-∞..+∞)\"}");

// Test open range serialization with Duration
Range<Duration> openDurationRange = RangeFactory.open(Duration.ofMinutes(30), Duration.ofMinutes(60));
assertEquals(MAPPER.writeValueAsString(new Stringified<>(openDurationRange)), "{\"r\":\"(PT30M..PT1H)\"}");

// Test closed range serialization with LocalDate
Range<LocalDate> closedDateRange = RangeFactory.closed(LocalDate.parse("2023-01-01"), LocalDate.parse("2023-01-31"));
assertEquals(MAPPER.writeValueAsString(new Stringified<>(closedDateRange)), "{\"r\":\"[2023-01-01..2023-01-31]\"}");

// Test open-closed range serialization with String
Range<String> openClosedStringRange = RangeFactory.openClosed("abc", "def");
assertEquals(MAPPER.writeValueAsString(new Stringified<>(openClosedStringRange)), "{\"r\":\"(abc..def]\"}");
}
}
Loading