FasterXML / jackson-annotations

Core annotations (annotations that only depend on jackson-core) for Jackson data processor
https://github.com/FasterXML/jackson
Apache License 2.0
1.03k stars 330 forks source link

Force root wrapping per class #33

Open chrylis opened 10 years ago

chrylis commented 10 years ago

I'm working on mapping an external service that sends postbacks in wrapped JSON. I have set the @JsonRootName on the class and can read objects with a specially-configured mapper, but this wrapping behavior belongs to this service (i.e., a certain set of classes) specifically, and not to the overall system (Spring MVC, which expects a single mapping configuration).

Since it's known that this class will always need to be unwrapped, and request classes wrapped, it would be much clearer to be able to force root wrapping per class, perhaps with an alwaysWrap field on the @JsonRootName annotation. Is this a feasible feature?

cowtowncoder commented 10 years ago

Yes, that sounds reasonable the way you suggest.

cowtowncoder commented 10 years ago

Added as suggested; need to add support in jackson-databind next.

cowtowncoder commented 10 years ago

Hmmh. Actually, may need to re-think this one... unfortunately there is bit of a speed bump that I ignored earlier. Problem being that to find the flag, a lookup is needed. I need to think about this a bit more.

cowtowncoder commented 10 years ago

Ok, I will have to revert this, since I don't think I can figure it out before 2.4.0 release. Will leave this open so that hopefully it can be included in 2.5.

cowtowncoder commented 9 years ago

Note: was not included in 2.5.

odrotbohm commented 8 years ago

You might wanna clean up the JavaDoc on @JsonRootName accordingly as it still indicate the functionality might be (or better: have been, which it hasn't) introduced in 2.5.

Other than that, +1 for adding this :).

ghost commented 8 years ago

Is there a plan to include this one in near future? We are quite depending on it, so we need to plan at least :)

cowtowncoder commented 8 years ago

@jbm4sa No one working actively on this that I am aware of. Contributions welcome. But I will move this to jackson-databind as it is implement there, even if a tiny part may be relevant here; issues reported here are easier to ignore as well.

cowtowncoder commented 8 years ago

Moved to https://github.com/FasterXML/jackson-databind/issues/1022

mdv27 commented 8 years ago

Is there any way to remove default root wrapping? I want to send json without root element and I am not giving @JsonRootName still its wrapping with class name? Please suggest something. @chrylis @cowtowncoder

cowtowncoder commented 8 years ago

@mv07517 Couple of ways. ObjectWriter has withoutRootName() method that can be used to do this for specific calls. Or, you may sub-class JacksonAnnotationIntrospector to sort of hide the annotation.

As to why root wrapping seems to be enabled in your case, I do not know. For questions, please use the mailing list in future; or, if you do have what you think is a bug, file a new issue with reproduction. Tagging questions to existing issues tends to make discussions more convoluted and harder to read.

OndraZizka commented 5 years ago

Any progress on this? I am looking at JacksonAnnotationIntrospector but see no way to do this. findRootName() doesn't affect this behavior.

cxl-todd commented 4 years ago

Seems like this is still commented out. Any thoughts on reintroducing this functionality? Would be great to do on a per-class basis rather than affecting the entire object mapper.

cowtowncoder commented 4 years ago

@cxl-todd Unfortunately support was never implemented, even though annotation declaration was accidentally released. If someone has time to look into this, I could help. But last I checked it seemed difficult to achieve. Esp. compared to relative ease with which one can just wrap root object in a simple Map to achieve the same effect.

malloc32 commented 4 years ago

I need this functionally to, I need that object mapper only wrap root when class has the annotation. Any advance? Thank you.

cowtowncoder commented 4 years ago

@malloc32 for help with usage etc, mailing lists would be the place to ask, not issue. I can't think of anything simple that would do this automatically just based on annotation.