Mapping a fixed-size list to an POJO? (@JsonFormat(shape=JsonFormat.Shape.ARRAY))
#6038
-
|
Hi, I have a vague memory that it is possible to map items of a list to POJO properties. But I can't seem to find anything about it, so I begin to wonder whether I hallucinated that or not... To be clear, our use-case is a 3D object (bounding box) expressed as a list of 3 elements (each element is an "interval", we have a custom deserializer to turn Serialized form looks like this (YAML): @JsonExplodeList // idk
class BoundingBoxParams {
// Instead of:
//public List<IntegerInterval> xyz;
// Fields "exploded" automatically:
@JsonIndex(0)
public IntegerInterval x;
@JsonIndex(1)
public IntegerInterval y;
@JsonIndex(2)
public IntegerInterval z;
public BoundingBox create() {
// Instead of having to validate list size and extract elements,
// it would be much more direct code with x, y, z here...
}
}Here, If such feature does exist, then I'd be happy to find out I'm not crazy. Otherwise, that's really not a big deal, our current code works perfectly, but my colleagues are gonna call me crazy for telling them it was possible hahaha 😄 Thx |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
AI suggests that there is no Jackson feature like this and you would be best served writing a custom deserializer (and serializer). One thing that it did suggest that might work would look like: This example would need work and would only work for deserialization but you might be able to extend it to serialize as you like as well. |
Beta Was this translation helpful? Give feedback.
-
|
This is why AI doesn't yet replace library authors. :-D The thing @indyteo is looking for is, I think, and for more examples see f.ex https://github.com/FasterXML/jackson-databind/blob/3.x/src/test/java/tools/jackson/databind/struct/POJOAsArrayTest.java#L64 NOTE: works particularly well for Records since they define property order by declaration order. |
Beta Was this translation helpful? Give feedback.
This is why AI doesn't yet replace library authors. :-D
The thing @indyteo is looking for is, I think,
@JsonFormat(shape=JsonFormat.Shape.ARRAY)like so:and for more examples see f.ex https://github.com/FasterXML/jackson-databind/blob/3.x/src/test/java/tools/jackson/databind/struct/POJOAsArrayTest.java#L64
NOTE: works particularly well for Records since they define property order by declaration order.