How can I include raw JSON in an object using Jackson?

@JsonRawValue is intended for serialization-side only, since the reverse direction is a bit trickier to handle. In effect it was added to allow injecting pre-encoded content.

I guess it would be possible to add support for reverse, although that would be quite awkward: content will have to be parsed, and then re-written back to “raw” form, which may or may not be the same (since character quoting may differ).
This for general case. But perhaps it would make sense for some subset of problems.

But I think a work-around for your specific case would be to specify type as ‘java.lang.Object’, since this should work ok: for serialization, String will be output as is, and for deserialization, it will be deserialized as a Map. Actually you might want to have separate getter/setter if so; getter would return String for serialization (and needs @JsonRawValue); and setter would take either Map or Object. You could re-encode it to a String if that makes sense.

Leave a Comment