FasterXML / jackson-datatypes-collections

Jackson project that contains various collection-oriented datatype libraries: Eclipse Collections, Guava, HPPC, PCollections
Apache License 2.0
79 stars 53 forks source link

Support `@JsonFormat(shape=STRING)` for serialization of `Range<T>` #118

Closed chris-dekker closed 11 months ago

chris-dekker commented 1 year ago

I find the standard serialization JSON, while flexible, of Range<T> to be quite verbose:

{
  "lower_endpoint": 12,
  "lower_bound_type": "CLOSED",
  "upper_endpoint": 34,
  "upper_bound_type": "OPEN"
}

When manually processing the results, I prefer to shorter notation produced by, among others, the default toString() invocation: [12..34).

Similar to Instant, where we can use @JsonFormat(shape = ..) to control (de)serialization to use the ISO format or unix milis, it would be great if we could control the shape of the range this way. Keep the default as-is and only when @JsonFormat(shape = STRING) is provided, use the shorter notation.

Granted, this will only work for endpoints having scalar values, but that seems to cover the majority of use cases I've seen so far.

cowtowncoder commented 1 year ago

Sounds like a good idea for someone with time & interest to work on, and solid improvement to usability. Tagged as such.

saydar31 commented 1 year ago

Assign it to me please, Also I have a question: how should range without lower or upper bound look like? (,1] or (-INF, 1], in my humble opinion second one looks better

cowtowncoder commented 1 year ago

@saydar31 I let you decide. But does Guava's Range.toString() offer guidance here?

chris-dekker commented 1 year ago

@saydar31 great to hear you're picking this up! Would prefer to stay consistent with the Range.toString() method indeed, which uses the symbol (\u221e)

JooHyukKim commented 1 year ago

@saydar31 great to hear you're picking this up! Would prefer to stay consistent with the Range.toString() method indeed, which uses the symbol (\u221e)

@chris-dekker now that you mention it, this needs to go on the doc. I will add notes to the PR that whatever symbol Jackson returns, the returned unicode of symbol would also be documented.

cowtowncoder commented 11 months ago

Merged for inclusion in 2.17.0