This builds upon the work in https://jira.codehaus.org/browse/JACKSON-805
A feature to do this on a per-property or per-class (with inheritance) basis.
[JSON-LD|http://www.w3.org/TR/json-ld/] which is a W3C Recommendation, uses this convention where a singular property can be turned into array (retaining the original name) in order to have multiple cardinality.
However, some properties, like http://schema.org/offers are always rendered as array.
While the proper way to handle such cases is a full-blown JSON-LD processor (JSON-LD has lots more features and is very flexible), for common use cases people might just want to use Jackson to read/write JSON-LD, and having this capability will be good.
An annotation is required, probably @JsonCollectionAsScalar (or @JsonScalar? or @JsonSingleElement?), with two fields:
- boolean value = true -> deserialize scalar as collection. This is most important as it allows to read most JSON-LD effortlessly.
- boolean serialize = true -> serialize single-element collection as scalar. If I were a JSON-LD writer, I'd set
serialize=false in order to make it much easier (albeit longer JSON path) for naive clients to read, since the JSON will have a rigid schema.
If boolean is ambiguous then can also use enum.
Note that behavior for nested collection is undefined (and probably should be an error), so the contents of collection must be either primitive or a non-collection object. (note that some objects may be Iterable, which should be okay, still treated as JSON object)
This builds upon the work in https://jira.codehaus.org/browse/JACKSON-805
A feature to do this on a per-property or per-class (with inheritance) basis.
[JSON-LD|http://www.w3.org/TR/json-ld/] which is a W3C Recommendation, uses this convention where a singular property can be turned into array (retaining the original name) in order to have multiple cardinality.
However, some properties, like http://schema.org/offers are always rendered as array.
While the proper way to handle such cases is a full-blown JSON-LD processor (JSON-LD has lots more features and is very flexible), for common use cases people might just want to use Jackson to read/write JSON-LD, and having this capability will be good.
An annotation is required, probably
@JsonCollectionAsScalar(or@JsonScalar? or@JsonSingleElement?), with two fields:serialize=falsein order to make it much easier (albeit longer JSON path) for naive clients to read, since the JSON will have a rigid schema.If boolean is ambiguous then can also use enum.
Note that behavior for nested collection is undefined (and probably should be an error), so the contents of collection must be either primitive or a non-collection object. (note that some objects may be
Iterable, which should be okay, still treated as JSON object)