Jackson-databind: Add `@JsonWrapped`

Created on 3 Aug 2014  路  26Comments  路  Source: FasterXML/jackson-databind

It would be nice to have the inverse of @JsonUnwrapped.

This would enable serializing this class:

class Gene {
  String symbol;
  String chrId;
  String chrName;
}

To this:

{
  "symbol" : "X",
  "chr" : { 
    "id" : "Y",
    "name": "chr1"
  }
}
most-wanted

All 26 comments

+1

+1

I think use of JsonPointer expression would work well in expressing wrapping in JSON.
But how would one define grouping for fields to wrap? Something like..

@JsonWrapped({ @JsonWrapped.Group(path="/extra", fields={"b","c"}),
    @JsonWrapped.Group(path="/main", fields={"a"} })
public class Stuff {
   public int a, c, b;
}

to produce

{
   "main" : {
      "a" : 1
   },
   "extra" : {
     "b": 2,
     "c" : 3
   }
}

or such?

Can it be built up as we go along?

class Gene {
  String symbol;
  @JsonWraped(path="/chr", name="id")
  String chrId;
  @JsonWraped(path="/chr", name="name")
  String chrName;
}

This would make it simpler to refactor code if we don't have to worry about updating the field name in the annotation.

I'd have to think about that, but my first thought was that it should be fine to use both @JsonProperty and @JsonWrapped to get this effect.

+1

I really love JsonUnwrapped concept and find quite useful. JsonWrapped would be a great asset for project.

+1

+1
would love to have the inverse of JsonUnwrapped.

+1

+1

+1

+1

+1

+1

+1

+1

This probably belongs in jackson-annotations and is a duplicate of #42

@Dru89 Actually this is the better place just because while annotations package is where annotation is added, databind implements the behavior. So one is needed for databind regardless. Having two different issues also works from release-notes perspective.

+1

+1

+1 for reducing boilerplate

+1

+1

+1

Pointless to add more +1s here; I take it there are users who would like to see this feature, so further "votes" have little additional value. Hence locking comments.

Note: added new "most-wanted" label to make it easier to find these Top Requests.

Was this page helpful?
0 / 5 - 0 ratings